记录:fastadmin 压缩图片
1,安装图片处理拓展:composer require topthink/think-image
2,找到公共上传类,application/common/library/Upload.php,在最下面添加一个方法,这里呢我是直接处理上传完成的图片,将其直接压缩覆盖原图,如果期望单独生成一张缩略图的话也可以在这个基础上做修改
/**
* 等比压缩上传的图片,覆盖原图
* @Author: ELK
* @Date: 2022-1-10 16:10:28
* @param array $params 上传图片信息
* @param int $ratio 压缩比例
*/
public function thumb_image(&$params,$ratio = 10)
{
if($params['imagewidth'] > $ratio){
$width = $params['imagewidth'] / $ratio;
$height = $params['imageheight'] / $ratio;
$pathname = $params['url'];
$file = $this->file;
$image = \think\Image::open($file);
$image->thumb($width, $height)->save('.' . $pathname);
$image_root_path = ROOT_PATH . '/public' . $pathname;
$params['imagewidth'] = $width;
$params['imageheight'] = $height;
$params['filesize'] = filesize($image_root_path);
}
}
/**
* 等比压缩上传的图片,单独创建
* @Author: ELK
* @Date: 2022-1-10 16:10:28
* @param array $params 上传图片信息
* @param int $ratio 期望压缩比例
*/
public function add_thumb_image(&$params,$ratio = 10)
{
if($params['imagewidth'] > $ratio){
$width = $params['imagewidth'] / $ratio;
$height = $params['imageheight'] / $ratio;
// 修改存储图片路径
$params['url'] = $pathname = str_replace('.','_thumb.',$params['url']);
$params['filename'] = str_replace('.','_thumb.',$params['filename']);
$file = $this->file;
$image = \think\Image::open($file);
$image->thumb($width, $height)->save('.' . $pathname);
$image_root_path = ROOT_PATH . '/public' . $pathname;
$params['imagewidth'] = $width;
$params['imageheight'] = $height;
$params['filesize'] = filesize($image_root_path);
}
}
3,找到里面的 upload() 方法,在文件上传完成,将要添加到attachment表的图片数据时进行等比压缩处理,插入我们添加的处理方法
if (in_array($this->fileInfo['type'], ['image/gif', 'image/jpg', 'image/jpeg', 'image/bmp', 'image/png', 'image/webp']) || in_array($this->fileInfo['suffix'], ['gif', 'jpg', 'jpeg', 'bmp', 'png', 'webp'])) {
if($this->fileInfo['imagewidth'] > 3000){
$this->thumb_image($params,10);
}else{
if($this->fileInfo['imagewidth'] > 1600){
$this->thumb_image($params,4);
}else{
if($this->fileInfo['imagewidth'] > 600){
$this->thumb_image($params,2);
}
}
}
}
取商品图的第一张压缩处理为封面图:
添加方法:
// 生成商品封面图
$image_path = $params['images'] ? explode(",",$params['images'])[0] : '';
$imageurl = $image_path;
if($image_path){
list($width, $height, $type, $attr) = getimagesize(cdnurl($image_path, true));
if($width > 320){
// 新图片宽高,等比例缩小
$new_width = 320;
$ratio = $width / $new_width;
$new_height = $height / $ratio;
// 新图片保存路径
$filename = basename($image_path);
$filepath = str_replace($filename, "", $image_path);
$suffix = substr($filename,strpos($filename,'.',1));
$savename = md5(microtime(true));
// 保存新图片
$imageurl = $filepath . $savename . $suffix;
$image = \think\Image::open(ROOT_PATH . 'public' . $image_path);
$image->thumb($new_width, $new_height)->save('.' . $imageurl);
}
}
$params['image'] = $imageurl;
修改方法:
// 生成商品封面图
$image_path = $params['images'] ? explode(",",$params['images'])[0] : '';
$imageurl = $image_path;
if($image_path){
$old_image_path = $row['images'] ? explode(",", $row['images'])[0] : '';
if($old_image_path != $image_path || !$row['image']){
list($width, $height, $type, $attr) = getimagesize(cdnurl($image_path, true));
if($width > 320){
// 新图片宽高,等比例缩小
$new_width = 320;
$ratio = $width / $new_width;
$new_height = $height / $ratio;
// 新图片保存路径
$filename = basename($image_path);
$filepath = str_replace($filename, "", $image_path);
$suffix = substr($filename,strpos($filename,'.',1));
$savename = md5(microtime(true));
// 保存新图片
$imageurl = $filepath . $savename . $suffix;
$image = \think\Image::open(ROOT_PATH . 'public' . $image_path);
$image->thumb($new_width, $new_height)->save('.' . $imageurl);
}
}else{
$imageurl = $row['image'];
}
}
$params['image'] = $imageurl;