作者 李宇航

合并分支 'lyh-server' 到 'master'

Lyh server



查看合并请求 !1941
@@ -35,15 +35,10 @@ class CropImage extends Command @@ -35,15 +35,10 @@ class CropImage extends Command
35 35
36 public function handle(){ 36 public function handle(){
37 echo '测试裁剪->cs-crop:'.PHP_EOL; 37 echo '测试裁剪->cs-crop:'.PHP_EOL;
38 - $images ='https://ecdn6.globalso.com/upload/p/1/image_other/2025-05/1746675561075.jpg';  
39 - $image = str_replace_url($images);  
40 - $cosService = new CosService();  
41 - $new_url = $cosService->cropAndUploadFromCos($image);  
42 - return $new_url;  
43 -// $project_id = $this->argument('project_id');  
44 -// ProjectServer::useProject($project_id);  
45 -// $this->_action();  
46 -// DB::disconnect('custom_mysql'); 38 + $project_id = $this->argument('project_id');
  39 + ProjectServer::useProject($project_id);
  40 + $this->_action($project_id);
  41 + DB::disconnect('custom_mysql');
47 } 42 }
48 43
49 /** 44 /**
@@ -60,8 +55,9 @@ class CropImage extends Command @@ -60,8 +55,9 @@ class CropImage extends Command
60 foreach ($data as $val){ 55 foreach ($data as $val){
61 //处理图片为相对路径 56 //处理图片为相对路径
62 $image = str_replace_url($val); 57 $image = str_replace_url($val);
63 - $new_url = $cosService->cropAndUploadFromCos($image);  
64 - echo '返回的图片路径:'.$new_url.PHP_EOL; 58 + $height = $cosService->getImageHeight($image);
  59 + echo '返回的图片高度:'.$height.PHP_EOL;
  60 + $cosService->cropCosImage($image);
65 } 61 }
66 } 62 }
67 return true; 63 return true;
@@ -315,28 +315,22 @@ class CosService @@ -315,28 +315,22 @@ class CosService
315 315
316 /** 316 /**
317 * @remark :获取cos图片高度 317 * @remark :获取cos图片高度
318 - * @name :cropAndUploadFromCos 318 + * @name :getImageHeight
319 * @author :lyh 319 * @author :lyh
320 * @method :post 320 * @method :post
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){
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 /**
@@ -346,10 +340,9 @@ class CosService @@ -346,10 +340,9 @@ class CosService
346 * @method :post 340 * @method :post
347 * @time :2025/5/8 11:06 341 * @time :2025/5/8 11:06
348 */ 342 */
349 - public function cropCosImage($cosUrl,$height = 200) 343 + public function cropCosImage($cosUrl,$height = 220)
350 { 344 {
351 $cos = config('filesystems.disks.cos'); 345 $cos = config('filesystems.disks.cos');
352 - // 初始化 COS 客户端  
353 $cosClient = new Client([ 346 $cosClient = new Client([
354 'region' => $cos['region'], 347 'region' => $cos['region'],
355 'credentials' => [ 348 'credentials' => [
@@ -357,30 +350,24 @@ class CosService @@ -357,30 +350,24 @@ class CosService
357 'secretKey' => $cos['credentials']['secretKey'], 350 'secretKey' => $cos['credentials']['secretKey'],
358 ], 351 ],
359 ]); 352 ]);
360 - // 定义 Pic-Operations JSON,用于裁剪并覆盖原图  
361 $pathInfo = pathinfo($cosUrl); 353 $pathInfo = pathinfo($cosUrl);
362 - $newKey = $pathInfo['dirname'] . '/' . $pathInfo['filename'] . '-'. time() . $pathInfo['extension']; 354 + $newKey = $pathInfo['dirname'] . '/crop_' . $pathInfo['filename'] . $pathInfo['extension'];
363 $operations = [ 355 $operations = [
364 'is_pic_info' => 0, 356 'is_pic_info' => 0,
365 'rules' => [ 357 'rules' => [
366 [ 358 [
367 // 注意 fileid 要 base64 编码,并与 Key 相同才能覆盖 359 // 注意 fileid 要 base64 编码,并与 Key 相同才能覆盖
368 - 'fileid' => base64_encode($newKey), 360 + 'fileid' => $newKey,
369 'rule' => 'imageMogr2/crop/x'.$height.'/gravity/center' 361 'rule' => 'imageMogr2/crop/x'.$height.'/gravity/center'
370 ] 362 ]
371 ] 363 ]
372 ]; 364 ];
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 - } 365 + // 执行裁剪并覆盖
  366 + $cosClient->ImageProcess([
  367 + 'Bucket' => $cos['bucket'],
  368 + 'Key' => $cosUrl, // 要处理的对象路径
  369 + 'PicOperations' => json_encode($operations),
  370 + ]);
  371 + return $newKey;
385 } 372 }
386 } 373 }