jovialcore / cake-cloudinary
CakePHP的Cloudinary插件
v1.0.0
2023-04-21 22:51 UTC
Requires
- php: >=7.2
- cakephp/cakephp: ^4.3
- cloudinary/cloudinary_php: ^2.0
Requires (Dev)
- phpunit/phpunit: ^8.5 || ^9.3
This package is auto-updated.
Last update: 2024-09-30 01:20:05 UTC
README
CakePHP Cloudinary插件提供了一个易于使用的包装器,用于Cloudinary PHP SDK,以实现 CakePHP 项目中轻松上传文件到您的 Cloudinary 账户。
使用composer安装插件
安装
composer require jovialcore/cake-cloudinary
您还需要在 app.php
文件中配置 Cloudinary 凭据
// config/app_local.php // In the return block, add: // ... 'Cloudinary' => [ 'default' => [ 'cloud_name' => 'your_cloud_name', 'api_key' => 'your_api_key', 'api_secret' => 'your_api_secret', 'secure' => true // use HTTPS ], ], // ... ];
使用
要在控制器中使用 Cloudinary 组件,您需要在 initialize
方法中加载它
// src/Controller/YourController.php namespace App\Controller; use Cake\Controller\Controller; use Cake\Event\EventInterface; class YourController extends Controller { public function initialize(): void { parent::initialize(); $this->loadComponent('CakeCloudinary.Cloudinary'); } // ... }
上传资产
$file
参数可以是文件路径、File
对象或 $this->request->getUploadedFiles()
| $this->request->getUploadedFile();
对象。
快速上传资产并返回安全URL
$this->Cloudinary->upload($file, ['getUrl' => 'true']);
快速上传视频资产并返回安全URL
$this->Cloudinary->uploadVideo($file, ['getUrl' => 'true']);
快速上传任何文件(例如,pdf、csv等)资产并返回安全URL
$this->Cloudinary->uploadFile($file, ['getUrl' => 'true']);
assets 方法也可以接受一个选项数组作为第二个参数。选项数组可以包括 Cloudinary API 支持的任何选项。有关可用选项的列表,请参阅Cloudinary PHP Image Upload API 文档。示例
$this->Cloudinary->upload($file,[ 'folder' => 'my_cloudinary_folder', 'public_id' => 'my_video_name', ]);
asset 方法/api也返回一个包含上传图像元数据的响应对象,包括其URL和公共ID。您可以使用 Cloudinary 组件的 getUrl
或 getSecureUrl
方法检索上传图像的URL。
$url = $this->Cloudinary->getUrl(); // get url of uploaded file (http) $secureUrl = $this->Cloudinary->getSecureUrl(); // get secureUrl of the uploaded asst $publicId = $this->Cloudinary->getPublicId(); // get public Id of the uploaded asset $originalFIleName = $this->Cloudinary->getOriginalFileName(); // get the asset name before it was uploaded to cloudinary $resourceType = $this->Cloudinary->getUploadedAt(); // get the time the asset as uploaded $extension = $this->Cloudinary->getExtension(); // get file extension of the uploaded asset e.g jpg, .pdf, .png, etc $fileType = $this->Cloudinary->getFileType(); // get asset type. E.g, image, video, etc. $fileSize = $this->Cloudinary->getFileSize(); //get uploaded asset file's size by defaults, it returns a human readable file size like 20MB, 20kb, etc // If you prefer to get just the raw bytes, $fileizeInBytes = this->Cloudinary->getFileSize(['human_readable' => false]); $height = $this->Cloudinary->getHeight(); // get asset height $width = $this->Cloudinary->getWidth(); // get asset width
删除资产
$res = $this->Cloudinary->delete($publidId);
获取远程资产
// get url from an asset $url = $this->Cloudinary->fetchUrl($publicId);
致谢
此插件受到了Laravel Cloudinary 包的启发
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。