作者 lyh

gx

... ... @@ -321,22 +321,16 @@ class CosService
* @time :2025/5/8 10:58
* @param :pathUrl->存储桶路径
*/
public function cropAndUploadFromCos($pathUrl,$maxHeight = 300){
public function getImageHeight($pathUrl,$maxHeight = 300){
$cos = config('filesystems.disks.cos');
$url = 'https://' . $cos['bucket'] . '.cos.' . $cos['region'] . '.myqcloud.com/' . ltrim($pathUrl, '/') . '?image/info';
try {
$imageInfo = @getimagesize($url);
if ($imageInfo) {
$width = $imageInfo[0];
$height = $imageInfo[1];
return "图片尺寸:宽度 {$width}px,高度 {$height}px";
} else {
return "错误:无法读取图片或链接无效";
}
} catch (\Exception $e) {
@file_put_contents(storage_path('logs/crop_image_error.log'), '获取图片高度失败: ' . $e->getMessage() . PHP_EOL, FILE_APPEND);
return '';
$imageInfo = @getimagesize($url);
if ($imageInfo) {
// $width = $imageInfo[0];
$height = $imageInfo[1];
return $height;
}
return '';
}
/**
... ... @@ -357,30 +351,24 @@ class CosService
'secretKey' => $cos['credentials']['secretKey'],
],
]);
// 定义 Pic-Operations JSON,用于裁剪并覆盖原图
$pathInfo = pathinfo($cosUrl);
$newKey = $pathInfo['dirname'] . '/' . $pathInfo['filename'] . '-'. time() . $pathInfo['extension'];
$newKey = $pathInfo['dirname'] . '/crop_' . $pathInfo['filename'] . $pathInfo['extension'];
$operations = [
'is_pic_info' => 0,
'rules' => [
[
// 注意 fileid 要 base64 编码,并与 Key 相同才能覆盖
'fileid' => base64_encode($newKey),
'fileid' => $newKey,
'rule' => 'imageMogr2/crop/x'.$height.'/gravity/center'
]
]
];
try {
// 执行裁剪并覆盖
$cosClient->ImageProcess([
'Bucket' => $cos['bucket'],
'Key' => $cosUrl, // 要处理的对象路径
'PicOperations' => json_encode($operations),
]);
return $newKey;
} catch (\Exception $e) {
@file_put_contents(storage_path('logs/crop_image_error.log'), var_export($e->getMessage(), true) . PHP_EOL, FILE_APPEND);
return '';
}
// 执行裁剪并覆盖
$cosClient->ImageProcess([
'Bucket' => $cos['bucket'],
'Key' => $cosUrl, // 要处理的对象路径
'PicOperations' => json_encode($operations),
]);
return $newKey;
}
}
... ...