|
|
|
@ -6,8 +6,7 @@ |
|
|
|
namespace app\common\controller; |
|
|
|
|
|
|
|
use app\BaseController; |
|
|
|
use app\enterprise\model\{File as FileModel,Message,User,Emoji}; |
|
|
|
use app\manage\model\{Config}; |
|
|
|
use app\enterprise\model\{File as FileModel,Message,User,Emoji}; |
|
|
|
use think\facade\Filesystem; |
|
|
|
use think\facade\Request; |
|
|
|
use think\File; |
|
|
|
@ -98,6 +97,11 @@ class Upload extends BaseController |
|
|
|
} |
|
|
|
// 把左边的/去掉再加上,避免有些有/有些没有 |
|
|
|
$object='/'.ltrim($object,'/'); |
|
|
|
// 压缩图片 |
|
|
|
$compress_img = $file['compress_img'] ?? ''; |
|
|
|
if ($filecate == 'image' && empty($compress_img)) { |
|
|
|
$compress_img = $this->getCompressImg($object, $info['ext'], $prefix); |
|
|
|
} |
|
|
|
$ret = [ |
|
|
|
"src" => $object, |
|
|
|
"name" => $name, |
|
|
|
@ -108,6 +112,7 @@ class Upload extends BaseController |
|
|
|
"ext" => $info['ext'], |
|
|
|
"type" =>2, |
|
|
|
'user_id'=>$uid, |
|
|
|
'compress_img'=>$compress_img, |
|
|
|
'videoInfo'=>$imageInfo |
|
|
|
]; |
|
|
|
|
|
|
|
@ -140,7 +145,7 @@ class Upload extends BaseController |
|
|
|
if($message['type']!='voice'){ |
|
|
|
$newFile->save($ret); |
|
|
|
} |
|
|
|
$message['content']=$ret['src']; |
|
|
|
$message['content']=$ret['compress_img'] ?: $ret['src']; |
|
|
|
$message['file_id']=$newFile->file_id ?? 0; |
|
|
|
$message['file_cate']=$fileType; |
|
|
|
$message['file_size']=$info['size']; |
|
|
|
@ -403,4 +408,82 @@ class Upload extends BaseController |
|
|
|
return $info; |
|
|
|
} |
|
|
|
|
|
|
|
// 压缩图片地址 |
|
|
|
protected function getCompressImg($src, $ext, $prefix) |
|
|
|
{ |
|
|
|
$newName2 = uniqid() . '.' . $ext; |
|
|
|
$compress_path = $prefix . $newName2; |
|
|
|
if($this->disk=='local'){ |
|
|
|
$compress_path='/storage/'.$compress_path; |
|
|
|
} |
|
|
|
// 原图地址 |
|
|
|
$filePath = root_path().'public' . $src; |
|
|
|
// 压缩图地址 |
|
|
|
$target = root_path().'public/' .ltrim($compress_path,'/'); |
|
|
|
// 压缩并处理图片大小 |
|
|
|
$compress_res = $this->compressTo5x5($filePath, $target); |
|
|
|
if (!$compress_res) { |
|
|
|
return ''; |
|
|
|
} |
|
|
|
return $compress_path; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 使用Imagick压缩到5x5像素 |
|
|
|
* @param $source |
|
|
|
* @param $target |
|
|
|
* @return bool |
|
|
|
*/ |
|
|
|
protected function compressTo5x5($source, $target) |
|
|
|
{ |
|
|
|
// 获取图片信息 |
|
|
|
list($width, $height, $type) = getimagesize($source); |
|
|
|
|
|
|
|
// 根据类型创建图像资源 |
|
|
|
switch ($type) { |
|
|
|
case IMAGETYPE_JPEG: |
|
|
|
$image = imagecreatefromjpeg($source); |
|
|
|
break; |
|
|
|
case IMAGETYPE_PNG: |
|
|
|
$image = imagecreatefrompng($source); |
|
|
|
break; |
|
|
|
case IMAGETYPE_GIF: |
|
|
|
$image = imagecreatefromgif($source); |
|
|
|
break; |
|
|
|
default: |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
// 创建5x5目标图像 |
|
|
|
$thumb = imagecreatetruecolor($width, $height); |
|
|
|
|
|
|
|
// 处理透明背景 |
|
|
|
if ($type == IMAGETYPE_PNG || $type == IMAGETYPE_GIF) { |
|
|
|
imagecolortransparent($thumb, imagecolorallocatealpha($thumb, 0, 0, 0, 127)); |
|
|
|
imagealphablending($thumb, false); |
|
|
|
imagesavealpha($thumb, true); |
|
|
|
} |
|
|
|
|
|
|
|
// 缩放并复制图像 |
|
|
|
imagecopyresampled($thumb, $image, 0, 0, 0, 0, $width, $height, $width, $height); |
|
|
|
|
|
|
|
// 保存压缩后的图片 |
|
|
|
switch ($type) { |
|
|
|
case IMAGETYPE_JPEG: |
|
|
|
imagejpeg($thumb, $target, 40); |
|
|
|
break; |
|
|
|
case IMAGETYPE_PNG: |
|
|
|
imagepng($thumb, $target, 40); |
|
|
|
break; |
|
|
|
case IMAGETYPE_GIF: |
|
|
|
imagegif($thumb, $target); |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
// 释放内存 |
|
|
|
imagedestroy($image); |
|
|
|
imagedestroy($thumb); |
|
|
|
|
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|