cb-techservices / yii2-unsplash
此扩展提供了一种Yii2小部件,用于通过Unsplash图像API显示图像选择器。
dev-master
2018-09-11 18:10 UTC
Requires
- unsplash/unsplash: *
- yiisoft/yii2: ~2.0.0
This package is auto-updated.
Last update: 2024-09-27 09:40:50 UTC
README
此扩展提供了一种Yii2小部件,用于通过Unsplash图像API显示图像选择器。
安装
安装此扩展的首选方式是通过composer。
运行以下命令之一
php composer.phar require --prefer-dist cb-techservices/yii2-unsplash "*"
或
"cb-techservices/yii2-unsplash": "*"
将以下内容添加到您的composer.json文件的require部分。
配置
将以下内容添加到您的Yii项目中的main.php配置文件。
要求: 您必须有一个有效的开发者帐户,并在Unsplash上设置了“应用”,使用Unsplash API。创建后,使用应用的访问密钥和UTM源(也称为应用名称)进行Yii配置。
'modules' => [
'unsplash' => [
'class' => 'cbtech\unsplash\UnsplashImagePickerModule',
// ... other configurations for the module ...
'params'=>[
'applicationId' => '{ACCESS_KEY}',
'utmSource' => '{UTM_SOURCE}',
]
],
],
用法
一旦安装了扩展和模块,只需在您的代码中使用它即可
<?= \cbtech\unsplash\UnsplashImagePicker::widget([ "search"=>"colors", "page"=>1, "per_page"=>16, "orientation"=>"landscape", "button_text"=>"Choose photo from Unsplash", "button_class"=>"btn btn-success", "button_style"=>"margin-right:10px;", ]); ?>
使用JavaScript和jQuery监听'unsplashDownload'事件。
$(document).bind('unsplashDownload',"#unsplash-results",function(event, data){ console.log("Download Url = = " + data.downloadUrl); console.log("All photo view urls = = " + data.urls); $.ajax({ "method":"POST", "url":"/project/save-user-project-media-url-ajax", //This is a custom PHP script that recieves the download URL of the photo and downloads it to the server. "data":{downloadUrl: data.downloadUrl} }).done(function(response){ $(".file-default-preview").empty().html("<img src='" + response.data.media.url + "' style='width:200px;height:200px;object-fit:cover;'/>"); }); });
示例PHP操作,接收下载URL
public function actionSaveUserProjectMediaUrlAjax(){ $downloadUrl = \Yii::$app->request->post("downloadUrl"); //Download URL sent via $_POST //Do something with the downloadUrl, like use it to download a copy of the photo to your server. }
鸣谢
- 官方Unsplash PHP库
- Unsplash开发者API
- Yii2框架
许可证
Yii2 Unsplash扩展可在MIT许可证下使用。有关更多信息,请参阅LICENSE文件。
