sudippalash / mediauploader
Laravel 媒体上传包
1.1.2
2024-03-29 20:42 UTC
Requires
- intervention/image: ^2.5
README
[![最新版本在 Packagist][ico-version]][link-packagist] ![软件许可][ico-license] [![总下载量][ico-downloads]][link-downloads]
mediauploader
是一个简单的 Laravel 文件处理包,提供多种处理文件的方法,例如图片上传、文件上传、Webp 上传、Base64 图片上传、从 URL 上传,以及上传任何类型的文件内容。
安装
通过 Composer
$ composer require sudippalash/mediauploader
适用于 Laravel <= 5.4
更新 composer 后,将 ServiceProvider 添加到 config/app.php 文件中的 providers 数组中
Sudip\MediaUploader\MediaUploaderServiceProvider::class,
您可以使用 facade 以缩短代码。将以下内容添加到您的别名中
'MediaUploader' => Sudip\MediaUploader\Facades\MediaUploader::class,
发布配置文件
您需要发布配置文件以添加 mediauploader
的全局路径。
php artisan vendor:publish --provider="Sudip\MediaUploader\MediaUploaderServiceProvider" --tag=config
在 config/mediauploader.php
配置文件中,您应设置 mediauploader
的全局路径。
return [ /* |-------------------------------------------------------------------------- | Base Directory |-------------------------------------------------------------------------- | | base_dir stores all other directory inside storage folder of your laravel application by default | if you specify any name. all storage will be done inside that directory or name that you specified | */ 'base_dir' => '', /* |-------------------------------------------------------------------------- | Thumb Directory |-------------------------------------------------------------------------- | | thumb_dir creates another folder inside the directory as a "thumb" by default | you can change the name thumb to any other name you like. */ 'thumb_dir' => 'thumb', /* |-------------------------------------------------------------------------- | Timestamp Prefix |-------------------------------------------------------------------------- | | If timestamp_prefix is true then create a file with a timestamp to ignore the same name image replacement. Example: image-1658562981.png. | If timestamp_prefix is false then the script checks file exists or not if the file exists then add the time() prefix for the new file otherwise leave it as the file | name. */ 'timestamp_prefix' => false, /* |-------------------------------------------------------------------------- | Thumb Image Height Width |-------------------------------------------------------------------------- | | specify the thumb image ratio of height and weight by default it takes 300px X 300px */ 'image_thumb_height' => 300, 'image_thumb_width' => 300, /* |-------------------------------------------------------------------------- | Fake Image Url |-------------------------------------------------------------------------- | | fake_image_url , if you specify a fake image path here. the entire package will use | this image when there is no image found. or you can specify the fake image in the | function parameter as well. | Example: 'fake_image_url' => url('images/fake.png'), */ 'fake_image_url' => null, /* |-------------------------------------------------------------------------- | Folder permission |-------------------------------------------------------------------------- | | path_permission , if you create a folder in your project then you can define your folder permission. | Example: null, 0755, 0777 */ 'path_permission' => 0777, ];
配置重要
在开始使用之前,您必须完成以下操作
- 请确保在使用此包之前连接您的存储
php artisan storage:link
- 更改您的 env 文件系统驱动到此处
FILESYSTEM_DISK=public
使用(文件上传)
- 图片上传
MediaUploader::imageUpload(<UploadedFile image>, <output path>, thumb=false, name=null, $imageResize = [], $thumbResize = [0, 0]);
示例
$file = MediaUploader::imageUpload($request->file, 'images', 1, null, [600, 600]); if ($file) { // File is saved successfully }
- Webp 图片上传
MediaUploader::webpUpload(<UploadedFile image>, <output path>, thumb=false, name=null, $imageResize = [], $thumbResize = [0, 0]);
示例
$file = MediaUploader::webpUpload($request->file, 'webps', 1, null, [600, 600]); if ($file) { // File is saved successfully }
- 任何上传(如果您不确定文件类型,可以使用此功能)
MediaUploader::anyUpload(<UploadedFile file>, <output path>, name=null);
示例
$file = MediaUploader::anyUpload($request->file, 'files', 'filename'); if ($file) { // File is saved successfully }
- 缩略图(要从现有图片创建缩略图,可以使用此功能)
MediaUploader::thumb(<output path>, <file>, $thumbPath = false, $thumbWidth = 0, $thumbHeight = 0);
示例
$file = MediaUploader::thumb('thumbs', $file, 200, 200); if ($file) { // File is saved successfully }
- 从 URL 上传图片
MediaUploader::imageUploadFromUrl(<Valid Image URL>, <output path>, thumb=false, name=null, $imageResize = [], $thumbResize = [0, 0]);
示例
$file = MediaUploader::imageUploadFromUrl($url, 'images', 1, null, [600, 600]); if ($file) { // File is saved successfully }
- Base64 图片上传
MediaUploader::base64ImageUpload(<base64 Content>, <output path>, thumb=false, name=null, $imageResize = [], $thumbResize = [0, 0]);
示例
$file = MediaUploader::base64ImageUpload($content, 'images', 1, null, [600, 600]); if ($file) { // File is saved successfully }
- 内容上传
MediaUploader::contentUpload(<Content>, <output path>, name=null);
示例
$file = MediaUploader::contentUpload($content, 'contents', 'name'); if ($file) { // File is saved successfully }
返回值
每个函数的响应看起来像这样
[ "name" => "example-image.jpg" "originalName" => "example-image.jpg" "size" => 148892 "width" => 600 "height" => 600 "mime_type" => "image/jpeg" "ext" => "jpg" "url" => "https:///storage/test/example-image.jpg" ]
使用(文件删除)
文件删除
MediaUploader::delete(<path>, <file_name>, $thumb = false)
示例
$file = MediaUploader::delete('images', $file_name, true); if ($file) { // File is deleted successfully }
使用(目录删除)
目录删除
MediaUploader::removeDirectory(<path>)
示例
$path = MediaUploader::removeDirectory('images'); if ($path) { // Directory is deleted successfully }
使用(文件/图像存在,文件/图像 URL,文件预览 & 图像预览)
- 文件/图像存在
MediaUploader::fileExists(<path>, <file_name>)
示例
{!! MediaUploader::fileExists('images', $file_name) !!}
- 文件/图像 URL
MediaUploader::showUrl(<path>, <file_name>)
示例
{!! MediaUploader::showUrl('images', $file_name) !!}
- 文件预览
MediaUploader::showFile(<path>, <file_name>)
示例
{!! MediaUploader::showFile('images', $file_name) !!}
- 图像预览
MediaUploader::showImg(<path>, <file_name>, <array options>)
示例
{!! MediaUploader::showImg('images', $file_name, [ 'thumb' => true, // If thumb image store via upload function. 'popup' => true, // Currently support only jQuery fancybox. 'fakeImg' => 'images/avatar.png', // Pass fake image path without url or pass true (if pass true it will generate fake image from config file fake_image_url value). 'id' => 'image', 'class' => 'img-fluid', 'style' => '', 'alt' => 'Nice Image', ]) !!}