1、首先确保php安装了ImageMagick,可通过phpinfo()进行查看
PHP >=5.4
Fileinfo Extension
GD Library (>=2.0)
Imagick PHP extension (>=6.5.7)
2、 安装及配置
composer require intervention/image
在 app/config/app.php 添加 providers 数组中添加如下代码:
/*
* Package Service Providers...
*/
Intervention\Image\ImageServiceProvider::class,
在 app/config/app.php 添加 aliases 数组中添加如下代码:
'Image' => Intervention\Image\Facades\Image::class,
发布扩展生成 config/image.php 配置文件,执行下面命令
php artisan vendor:publish --provider="Intervention\Image\ImageServiceProviderLaravelRecent"
3.使用方法
use Intervention\Image\Facades\Image;
//后面为图片路径
$path = base_path().'/public'.$filePath;
$img = Image::make($path);
//按照 5:3 的比例裁剪,然后缩放大小为 250 * 150,并防止图片放大
$b= $img->fit(250, 150, function ($constraint) {
$constraint->upsize();
})->save(base_path().'/public/'.$destinationPath.time().'.'.$extension);
//save参数是生成的新地址文件