作者 lyh

gx

@@ -321,23 +321,17 @@ class CosService @@ -321,23 +321,17 @@ class CosService
321 * @time :2025/5/8 10:58 321 * @time :2025/5/8 10:58
322 * @param :pathUrl->存储桶路径 322 * @param :pathUrl->存储桶路径
323 */ 323 */
324 - public function cropAndUploadFromCos($pathUrl,$maxHeight = 300){ 324 + public function getImageHeight($pathUrl,$maxHeight = 300){
325 $cos = config('filesystems.disks.cos'); 325 $cos = config('filesystems.disks.cos');
326 $url = 'https://' . $cos['bucket'] . '.cos.' . $cos['region'] . '.myqcloud.com/' . ltrim($pathUrl, '/') . '?image/info'; 326 $url = 'https://' . $cos['bucket'] . '.cos.' . $cos['region'] . '.myqcloud.com/' . ltrim($pathUrl, '/') . '?image/info';
327 - try {  
328 $imageInfo = @getimagesize($url); 327 $imageInfo = @getimagesize($url);
329 if ($imageInfo) { 328 if ($imageInfo) {
330 - $width = $imageInfo[0]; 329 +// $width = $imageInfo[0];
331 $height = $imageInfo[1]; 330 $height = $imageInfo[1];
332 - return "图片尺寸:宽度 {$width}px,高度 {$height}px";  
333 - } else {  
334 - return "错误:无法读取图片或链接无效"; 331 + return $height;
335 } 332 }
336 - } catch (\Exception $e) {  
337 - @file_put_contents(storage_path('logs/crop_image_error.log'), '获取图片高度失败: ' . $e->getMessage() . PHP_EOL, FILE_APPEND);  
338 return ''; 333 return '';
339 } 334 }
340 - }  
341 335
342 /** 336 /**
343 * @remark :裁剪图片 337 * @remark :裁剪图片
@@ -357,20 +351,18 @@ class CosService @@ -357,20 +351,18 @@ class CosService
357 'secretKey' => $cos['credentials']['secretKey'], 351 'secretKey' => $cos['credentials']['secretKey'],
358 ], 352 ],
359 ]); 353 ]);
360 - // 定义 Pic-Operations JSON,用于裁剪并覆盖原图  
361 $pathInfo = pathinfo($cosUrl); 354 $pathInfo = pathinfo($cosUrl);
362 - $newKey = $pathInfo['dirname'] . '/' . $pathInfo['filename'] . '-'. time() . $pathInfo['extension']; 355 + $newKey = $pathInfo['dirname'] . '/crop_' . $pathInfo['filename'] . $pathInfo['extension'];
363 $operations = [ 356 $operations = [
364 'is_pic_info' => 0, 357 'is_pic_info' => 0,
365 'rules' => [ 358 'rules' => [
366 [ 359 [
367 // 注意 fileid 要 base64 编码,并与 Key 相同才能覆盖 360 // 注意 fileid 要 base64 编码,并与 Key 相同才能覆盖
368 - 'fileid' => base64_encode($newKey), 361 + 'fileid' => $newKey,
369 'rule' => 'imageMogr2/crop/x'.$height.'/gravity/center' 362 'rule' => 'imageMogr2/crop/x'.$height.'/gravity/center'
370 ] 363 ]
371 ] 364 ]
372 ]; 365 ];
373 - try {  
374 // 执行裁剪并覆盖 366 // 执行裁剪并覆盖
375 $cosClient->ImageProcess([ 367 $cosClient->ImageProcess([
376 'Bucket' => $cos['bucket'], 368 'Bucket' => $cos['bucket'],
@@ -378,9 +370,5 @@ class CosService @@ -378,9 +370,5 @@ class CosService
378 'PicOperations' => json_encode($operations), 370 'PicOperations' => json_encode($operations),
379 ]); 371 ]);
380 return $newKey; 372 return $newKey;
381 - } catch (\Exception $e) {  
382 - @file_put_contents(storage_path('logs/crop_image_error.log'), var_export($e->getMessage(), true) . PHP_EOL, FILE_APPEND);  
383 - return '';  
384 - }  
385 } 373 }
386 } 374 }