Laravel
中使用 Intervention
库缩放图片的时候,没必要用专供的接口,用通用不需要任何配置,还更简单,直接调用就可以。
step 1、composer 安装 Intervention
包
composer require intervention/image
step 2、调用 Intervention API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| namespace App\Foundation;
use Illuminate\Support\Facades\Storage; use Intervention\Image\ImageManagerStatic;
class ImageHelper {
public static function thumb(string $orgPath, string $targetPath, int|null $width, int|null $height): bool { $img = ImageManagerStatic::make($orgPath);
$width || $width = null; $height || $height = null;
if ($width && $height) { $img->fit($width, $height, function ($constraint) { $constraint->upsize(); }); } else { $img->resize($width, $height, function ($constraint) { $constraint->aspectRatio(); $constraint->upsize(); }); }
return Storage::put( $targetPath, $img->encode('jpg')->getEncoded() ); } }
|