作者 刘锟

update

... ... @@ -1468,64 +1468,66 @@ function diffInHours($startTime, $endTime)
return round($hours, 1);
}
/**
* 通过图片地址压缩图片
* @param $url
* @param int $width
* @return string
* @author Akun
* @date 2025/09/01 15:18
*/
function thumbImageByUrl($url, $width = 360)
{
if (empty($url)) {
return $url;
}
if (!function_exists('thumbImageByUrl')) {
/**
* 通过图片地址压缩图片
* @param $url
* @param int $width
* @return string
* @author Akun
* @date 2025/09/01 15:18
*/
function thumbImageByUrl($url, $width = 360)
{
if (empty($url)) {
return $url;
}
if ($width == 0) {
return $url;
}
if ($width == 0) {
return $url;
}
//获取图片完整访问地址
$url_complete = getImageUrl($url, 0, 0, 0);//先用v6-file地址
if (strpos($url_complete, 'v6-file') !== false) {
$is_exists = checkRemoteFileExists($url_complete);
if (!$is_exists) {
//不存在,再用cdn地址
$url_complete = getImageUrl($url);
//获取图片完整访问地址
$url_complete = getImageUrl($url, 0, 0, 0);//先用v6-file地址
if (strpos($url_complete, 'v6-file') !== false) {
$is_exists = checkRemoteFileExists($url_complete);
if (!$is_exists) {
//不存在,再用cdn地址
$url_complete = getImageUrl($url);
}
}
}
//获取与原图存储路径相同的压缩路径
$path = parse_url($url_complete, PHP_URL_PATH);
$path_arr = explode('.', $path);
if (count($path_arr) != 2) {
return $url;
}
$path_arr[0] = $path_arr[0] . '_thumbW' . $width;
$key = implode('.', $path_arr);
//获取与原图存储路径相同的压缩路径
$path = parse_url($url_complete, PHP_URL_PATH);
$path_arr = explode('.', $path);
if (count($path_arr) != 2) {
return $url;
}
$path_arr[0] = $path_arr[0] . '_thumbW' . $width;
$key = implode('.', $path_arr);
try {
$img = \Intervention\Image\Facades\Image::make($url_complete);
try {
$img = \Intervention\Image\Facades\Image::make($url_complete);
//宽度按设定,高度自动调整
$img->resize($width, null, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
//宽度按设定,高度自动调整
$img->resize($width, null, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
//获取处理后的图片二进制资源
$resource = $img->stream()->__toString();
//获取处理后的图片二进制资源
$resource = $img->stream()->__toString();
//上传存储桶
$thumb_url = CosService::uploadRemote('', '', '', $key, $resource);
//上传存储桶
$thumb_url = CosService::uploadRemote('', '', '', $key, $resource);
$url = $thumb_url ? $thumb_url : $url;
} catch (\Exception $e) {
Log::channel('thumb_img')->error($e->getMessage(), [$url_complete, $width]);
}
$url = $thumb_url ? $thumb_url : $url;
} catch (\Exception $e) {
Log::channel('thumb_img')->error($e->getMessage(), [$url_complete, $width]);
}
return $url;
return $url;
}
}
if (!function_exists('checkRemoteFileExists')) {
... ...