Laravel8实现上传图片裁剪功能方法1、 安装扩展composer;require;intervention/image2、 在 app/config/app.php 添加 providers 数组中添加如下代码:Intervention\Image\ImageServiceProvider::class,3、 在 app/co
Laravel8实现上传图片裁剪功能方法
1、 安装扩展
composer require intervention/image2、 在 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实现上传图片裁剪功能方法
基础教程推荐
猜你喜欢
- PHP手机短信验证码实现流程详解 2022-10-18
- php实现构建排除当前元素的乘积数组方法 2022-11-23
- PHP实现文件下载【实例分享】 2024-04-27
- PHP实现抽奖系统的示例代码 2023-06-26
- PHP+MySQL+sphinx+scws实现全文检索功能详解 2023-01-31
- 设定php简写功能的方法 2023-03-17
- Yii框架连表查询操作示例 2023-02-13
- php数组函数序列之array_sum() – 计算数组元素值之和 2024-01-15
- PHP判断一个字符串是否是回文字符串的方法 2024-01-31
- php实现数组筛选奇数和偶数示例 2024-02-05
