fastadmin 使用 topthink/img 压缩、裁剪、加水印、处理图片
- 使用
Composer安装ThinkPHP5的图像处理类库:
composer require topthink/think-image
- 在fastadmin中上传文件的公用方法中使用:
fastadmin中文件路径:application/admin/controller/Ajax.php
上传完后对图片再次进行处理:
try { $upload = new Upload($file); $attachment = $upload->upload(); //使用open方法打开已上传成功的图像文件: $image = \think\Image::open('.' . $attachment->url); if (in_array($image->mime(), ['image/gif', 'image/jpg', 'image/jpeg', 'image/bmp', 'image/png', 'image/webp'])) {
//加水印步骤(图片加水印) $image->water('./333.png', \think\Image::WATER_NORTHWEST)->save('.' . $attachment->url); } } catch (UploadException $e) { $this->error($e->getMessage()); } - 更多处理方式:
裁剪:
生成缩略图:$image = \think\Image::open('./image.png'); //将图片裁剪为300x300并保存为crop.png $image->crop(300, 300,100,30)->save('./crop.png');
图像翻转:$image = \think\Image::open('./image.png'); // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.png $image->thumb(150, 150)->save('./thumb.png');
旋转:$image = \think\Image::open('./image.png'); // 对图像进行以x轴进行翻转操作 $image->flip()->save('./filp_image.png');
等更详细操作移步tp文档,传送门:https://www.kancloud.cn/manual/thinkphp5/177530$image = \think\Image::open('./image.png'); // 对图像使用默认的顺时针旋转90度操作 $image->rotate()->save('./rotate_image.png');

浙公网安备 33010602011771号