Laravel8实现上传图片裁剪功能方法1、 安装扩展composer;require;intervention/image2、 在 app/config/app.php 添加 providers 数组中添加如下代码:Intervention\Image\ImageServiceProvider::class,3、 在 app/co
Laravel8实现上传图片裁剪功能方法
1、 安装扩展
composer require intervention/image
2、 在 app/config/app.php 添加 providers 数组中添加如下代码:
Intervention\Image\ImageServiceProvider::class,
3、 在 app/config/app.php 添加 aliases 数组中添加如下代码:
'Image' => Intervention\Image\Facades\Image::class,
4、发布扩展生成 config/image.php 配置文件,执行下面命令
php artisan vendor:publish --provider="Intervention\Image\ImageServiceProviderLaravelRecent"
5、使用
<?php
use Intervention\Image\Facades\Image;
//后面为图片路径
$img = Image::make('./image/abc.jpg')->resize(300, 300);
// 将处理后的图片重新保存到其他路径
$img->save('./image/new_abc.jpg');
?>
完整方法:
<?php
/*
* 图片上传接口
* 文件名
* 文件夹名
* */
public static function uploadImg($file,$folder){
$tmp = $file;
$folder = $folder ? $folder : "";
$path = '/static/uploads'; //public下的Uploads
if ($tmp->isValid()) { //判断文件上传是否有效
$FileType = $tmp->getClientOriginalExtension(); //获取文件后缀
$FilePath = $tmp->getRealPath(); //获取文件临时存放位置
$FileName = $folder.'/'.date('Ymd').'/' . uniqid() . '.' . $FileType; //定义文件名
Storage::disk('Uploads')->put($FileName, file_get_contents($FilePath)); //存储文件
$img = Image::make(public_path().$path . '/' . $FileName)->resize(30, 30);
$img->save(public_path().$path . '/' . $FileName);
return $data = [
'code' => ApiErrDesc::UPLOAD_SUCCESS[0],
'msg' =>ApiErrDesc::UPLOAD_SUCCESS[1] ,
'data'=>['path' => $path . '/' . $FileName] //文件路径
];
}else{
return $data = [
'code' => ApiErrDesc::UPLOAD_ERROR[0],
'msg' =>ApiErrDesc::UPLOAD_ERROR[1] ,
'data'=>[]
];
}
}
?>
沃梦达教程
本文标题为:Laravel8实现上传图片裁剪功能方法
![](/xwassets/images/pre.png)
![](/xwassets/images/next.png)
猜你喜欢
- Laravel balde模板文件中判断数据为空方法 2023-08-30
- PHP简单实现二维数组的矩阵转置操作示例 2022-10-02
- PHP中PDO事务处理操作示例 2022-10-15
- laravel通用化的CURD的实现 2023-03-17
- PHP实现微信支付(jsapi支付)流程步骤详解 2022-10-09
- PHP仿tp实现mvc框架基本设计思路与实现方法分析 2022-10-18
- php微信公众号开发之秒杀 2022-11-23
- 用nohup命令实现PHP的多进程 2023-09-02
- windows下9款一键快速搭建PHP本地运行环境的好工具(含php7.0环境) 2023-09-02
- laravel实现按月或天或小时统计mysql数据的方法 2023-02-22