本文绝对的保姆级教程,因为网络上现有的教程和博文一般时间可能都很久远。有些文件路径或者引入路径不正确导致出现一些不必要的报错。现对处理方式进行整理
一、安装intervention/image
composer require intervention/image
二、引入依赖
在
app/config/app.php
中将安装好的依赖引入到项目中
'providers' => [
// 在数组末尾添加
Intervention\Image\ImageServiceProvider::class,
],
'aliases' => [
// 在数组末尾添加
'Image' => Intervention\Image\Image::class,
],
三、控制器中使用
注意:use的Image一定是
Intervention\Image\ImageManagerStatic
<?php
namespace App\Services;
use Intervention\Image\ImageManagerStatic as Image;
class ImageService {
/**
* 图片等比缩放
*
* @param [type] $image
* @param [type] $filepath
* @param integer $width
* @param integer $max_height
* @return string
*/
public static function imageResize($image, $filepath)
{
$img = Image::make($image);
$imgWidth = $img->width();
$imgHeight = $img->height();
// 你的处理逻辑
// 图片缩放 $width 缩放后的宽; $height 缩放后的高
$img->resize($width, $height);
// 图片保存
$img->save($filepath);
}
}
版权声明:本文为g960214原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。