作者 lyh

gx

@@ -321,22 +321,16 @@ class CosService @@ -321,22 +321,16 @@ 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);  
329 - if ($imageInfo) {  
330 - $width = $imageInfo[0];  
331 - $height = $imageInfo[1];  
332 - return "图片尺寸:宽度 {$width}px,高度 {$height}px";  
333 - } else {  
334 - return "错误:无法读取图片或链接无效";  
335 - }  
336 - } catch (\Exception $e) {  
337 - @file_put_contents(storage_path('logs/crop_image_error.log'), '获取图片高度失败: ' . $e->getMessage() . PHP_EOL, FILE_APPEND);  
338 - return ''; 327 + $imageInfo = @getimagesize($url);
  328 + if ($imageInfo) {
  329 +// $width = $imageInfo[0];
  330 + $height = $imageInfo[1];
  331 + return $height;
339 } 332 }
  333 + return '';
340 } 334 }
341 335
342 /** 336 /**
@@ -357,30 +351,24 @@ class CosService @@ -357,30 +351,24 @@ 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 - // 执行裁剪并覆盖  
375 - $cosClient->ImageProcess([  
376 - 'Bucket' => $cos['bucket'],  
377 - 'Key' => $cosUrl, // 要处理的对象路径  
378 - 'PicOperations' => json_encode($operations),  
379 - ]);  
380 - 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 - } 366 + // 执行裁剪并覆盖
  367 + $cosClient->ImageProcess([
  368 + 'Bucket' => $cos['bucket'],
  369 + 'Key' => $cosUrl, // 要处理的对象路径
  370 + 'PicOperations' => json_encode($operations),
  371 + ]);
  372 + return $newKey;
385 } 373 }
386 } 374 }