unicodeveloper / laravel-cloudinary
Requires
- php: ^7.0
- ext-json: *
- cloudinary/cloudinary_php: 2.0.0-beta6
- illuminate/support: ~5|~6|~7
Requires (Dev)
- mockery/mockery: ^1.1
- orchestra/testbench: ~3|~4
- phpunit/phpunit: ^8.0
- sempro/phpunit-pretty-print: ^1.0
This package is auto-updated.
Last update: 2020-07-15 14:55:12 UTC
README
请现在使用综合的 => Cloudinary Laravel Package!
一个用于上传、优化、转换和通过Cloudinary交付媒体文件的Laravel包。此外,它还提供了一个流畅且易于表达的API,以便轻松将媒体文件附加到Eloquent模型。
使用方法
上传 文件(图片、视频或任何类型的文件)到Cloudinary
/** * Using the Cloudinary Facade */ // Upload an Image File to Cloudinary with One line of Code $uploadedFileUrl = Cloudinary::upload($request->file('file')->getRealPath())->getSecurePath(); // Upload a Video File to Cloudinary with One line of Code $uploadedFileUrl = Cloudinary::uploadVideo($request->file('file')->getRealPath())->getSecurePath(); // Upload any File to Cloudinary with One line of Code $uploadedFileUrl = Cloudinary::uploadFile($request->file('file')->getRealPath())->getSecurePath(); /** * This package also exposes a helper function you can use if you are not a fan of Facades * Shorter, expressive, fluent using the * cloudinary() function */ // Upload an Image File to Cloudinary with One line of Code $uploadedFileUrl = cloudinary()->upload($request->file('file')->getRealPath())->getSecurePath(); // Upload a Video File to Cloudinary with One line of Code $uploadedFileUrl = cloudinary()->uploadVideo($request->file('file')->getRealPath())->getSecurePath(); // Upload any File to Cloudinary with One line of Code $uploadedFileUrl = cloudinary()->uploadFile($request->file('file')->getRealPath())->getSecurePath(); /** * You can also skip the Cloudinary Facade or helper method and laravel-ize your uploads by simply calling the * storeOnCloudinary() method on the file itself */ // Store the uploaded file in the "lambogini" directory on Cloudinary $result = $request->file('image')->store('lambogini', 'cloudinary'); // Store the uploaded file on Cloudinary $result = $request->file('file')->storeOnCloudinary(); // Store the uploaded file on Cloudinary $result = $request->file->storeOnCloudinary(); // Store the uploaded file in the "lambogini" directory on Cloudinary $result = $request->file->storeOnCloudinary('lambogini'); // Store the uploaded file in the "lambogini" directory on Cloudinary with the filename "prosper" $result = $request->file->storeOnCloudinaryAs('lambogini', 'prosper'); $result->getPath(); // Get the url of the uploaded file; http $result->getSecurePath(); // Get the url of the uploaded file; https $result->getSize(); // Get the size of the uploaded file in bytes $result->getReadableSize(); // Get the size of the uploaded file in bytes, megabytes, gigabytes or terabytes. E.g 1.8 MB $result->getFileType(); // Get the type of the uploaded file $result->getFileName(); // Get the file name of the uploaded file $result->getOriginalFileName(); // Get the file name of the file before it was uploaded to Cloudinary $result->getPublicId(); // Get the public_id of the uploaded file $result->getExtension(); // Get the extension of the uploaded file $result->getWidth(); // Get the width of the uploaded file $result->getHeight(); // Get the height of the uploaded file $result->getTimeUploaded(); // Get the time the file was uploaded
将文件附加到Laravel Eloquent模型:
首先,将Unicodeveloper\Cloudinary\MediaAlly
特质导入到模型中,如下所示
<?php namespace App; use Illuminate\Database\Eloquent\Model; use Unicodeveloper\Cloudinary\MediaAlly; class Page extends Model { use MediaAlly; ... }
接下来,使用以下命令发布包的迁移文件
php artisan vendor:publish --provider="Unicodeveloper\Cloudinary\CloudinaryServiceProvider" --tag="laravel-cloudinary-migration"
注意:一旦发布,请运行php artisan migrate
在您的数据库中创建所需的表。
现在您可以像这样将媒体资产附加到模型中
/** * How to attach a file to a Model by model creation */ $page = Page::create($this->request->input()); $page->attachMedia($file); // Example of $file is $request->file('file'); /** * How to attach an existing remote file to a Model by model creation */ $page = Page::create($this->request->input()); $page->attachRemoteMedia($remoteFileUrl); // Example of $remoteFileUrl is https://miro.medium.com/max/4096/1*V1TmCz1GeAQ4T7EWRTWebA.jpeg /** * How to attach a file to a Model by retreiving model records */ $page = Page::find(2); $page->attachMedia($file); // Example of $file is $request->file('file'); /** * How to attach a remote file to a Model by retreiving model records */ $page = Page::find(2); $page->attachRemoteMedia($remoteFileUrl); // Example of $remoteFileUrl is https://miro.medium.com/max/4096/1*V1TmCz1GeAQ4T7EWRTWebA.jpeg /** * How to retrieve files that were attached to a Model */ $filesBelongingToSecondPage = Page::find(2)->fetchAllMedia(); /** * How to retrieve the first file that was attached to a Model */ $fileBelongingToSecondPage = Page::find(2)->fetchFirstMedia(); /** * How to retrieve the last file that was attached to a Model */ $fileBelongingToSecondPage = Page::find(2)->fetchLastMedia(); /** * How to replace/update files attached to a Model */ $page = Page::find(2); $page->updateMedia($file); // Example of $file is $request->file('file'); /** * How to detach a file from a Model */ $page = Page::find(2); $page->detachMedia($file) // Example of $file is $request->file('file');
通过上传小部件上传文件:
使用随此包提供的x-cld-upload-button
Blade上传按钮组件,如下所示
<!DOCTYPE html>
<html>
<head>
...
@cloudinaryJS
</head>
<body>
<x-cld-upload-button>
Upload Files
</x-cld-upload-button>
</body>
</html>
您还可以使用以下其他Blade组件
<x-cld-image public-id="prosper" width="300" height="300"></x-cld-image> // Blade Image Component for displaying images <x-cld-video public-id="awesome"></x-cld-video> // Blade Video Component for displaying videos
通过命令行进行媒体管理:
/** * Back up Files on Cloudinary */ php artisan cloudinary:backup /** * Delete a File on Cloudinary */ php artisan cloudinary:delete /** * Fetch a File from Cloudinary */ php artisan cloudinary:fetch /** * Rename a File from Cloudinary */ php artisan cloudinary:rename /** * Upload a File to Cloudinary */ php artisan cloudinary:upload /** * Generate an archive of a group of files and get the zipped downloadable url */ php artisan cloudinary:archive
安装
要获取Laravel Cloudinary的最新版本,只需要求它
composer require unicodeveloper/laravel-cloudinary
或者将以下行添加到composer.json
文件的require块中。
"unicodeveloper/laravel-cloudinary": "1.0.0-beta"
然后您需要运行composer install
或composer update
来下载它并更新自动加载器。
一旦安装了Laravel Cloudinary,您需要注册服务提供者。打开config/app.php
,将以下内容添加到providers
键中。
'providers' => [ ... Unicodeveloper\Cloudinary\CloudinaryServiceProvider::class, ... ]
注意:如果您使用的是Laravel >= 5.5,可以跳过此步骤(将上面的代码添加到providers键中),并转到
配置
。
同时,以如下方式注册Cloudinary外观
'aliases' => [ ... 'Cloudinary' => Unicodeveloper\Cloudinary\Facades\Cloudinary::class, ... ]
配置
您可以使用以下命令发布配置文件
php artisan vendor:publish --provider="Unicodeveloper\Cloudinary\CloudinaryServiceProvider" --tag="laravel-cloudinary-config"
一个名为cloudinary.php
的配置文件,其中包含一些合理的默认值,将被放置在您的config
目录中
<?php return [ /* |-------------------------------------------------------------------------- | Cloudinary Configuration |-------------------------------------------------------------------------- | | An HTTP or HTTPS URL to notify your application (a webhook) when the process of uploads, deletes, and any API | that accepts notification_url has completed. | | */ 'notification_url' => env('CLOUDINARY_NOTIFICATION_URL'), /* |-------------------------------------------------------------------------- | Cloudinary Configuration |-------------------------------------------------------------------------- | | Here you may configure your Cloudinary settings. Cloudinary is a cloud hosted | media management service for all file uploads, storage, delivery and transformation needs. | | */ 'cloud_url' => env('CLOUDINARY_URL'), /** * Upload Preset From Cloudinary Dashboard * */ 'upload_preset' => env('CLOUDINARY_UPLOAD_PRESET') ];
API密钥
打开您的.env
文件,并添加API环境变量,upload_preset(这是可选的,直到您需要使用小部件)如下所示
CLOUDINARY_URL=xxxxxxxxxxxxx CLOUDINARY_UPLOAD_PRESET=xxxxxxxxxxxxx CLOUDINARY_NOTIFICATION_URL=
注意:您需要从您的Cloudinary仪表板获取这些凭据。
如果您正在使用Heroku、Forge、Digital Ocean等托管服务,请确保将上述详细信息添加到您的配置变量中。
Cloudinary JS
Cloudinary依赖其自带的JavaScript库来初始化Cloudinary上传小部件。您可以通过在应用程序布局的结束标签前放置@cloudinaryJS指令来加载JavaScript库。
<head> ... @cloudinaryJS </head>
注意:只有当您决定使用上传小部件时才加载此库。如果您使用此包作为Laravel API后端,则不需要这样做!
许可协议
MIT许可协议(MIT)。请参阅许可文件获取更多信息。