作者 lyh

gx

@@ -39,7 +39,8 @@ class CropImage extends Command @@ -39,7 +39,8 @@ class CropImage extends Command
39 echo '测试裁剪->cs-crop:'.PHP_EOL; 39 echo '测试裁剪->cs-crop:'.PHP_EOL;
40 $project_id = $this->argument('project_id'); 40 $project_id = $this->argument('project_id');
41 ProjectServer::useProject($project_id); 41 ProjectServer::useProject($project_id);
42 - $this->_action($project_id); 42 + $data = $this->_keywordAction($project_id);
  43 + $this->_aiAction($project_id,$data[0] ?? []);
43 DB::disconnect('custom_mysql'); 44 DB::disconnect('custom_mysql');
44 } 45 }
45 46
@@ -50,22 +51,63 @@ class CropImage extends Command @@ -50,22 +51,63 @@ class CropImage extends Command
50 * @method :post 51 * @method :post
51 * @time :2025/5/8 9:21 52 * @time :2025/5/8 9:21
52 */ 53 */
53 - public function _action($project_id){ 54 + public function _keywordAction($project_id){
  55 + //聚合页裁剪
54 $data = $this->getKeywordImage($project_id); 56 $data = $this->getKeywordImage($project_id);
55 - if(!empty($data)){  
56 $cosService = new CosService(); 57 $cosService = new CosService();
  58 + if(!empty($data)){
57 foreach ($data as $val){ 59 foreach ($data as $val){
58 //处理图片为相对路径 60 //处理图片为相对路径
59 $image = str_replace_url($val); 61 $image = str_replace_url($val);
60 $height = $cosService->getImageHeight($image); 62 $height = $cosService->getImageHeight($image);
  63 + if(empty($height)){
  64 + echo '未获取到图片高度。'.PHP_EOL;
  65 + continue;
  66 + }
61 echo '返回的图片高度:'.$height.PHP_EOL; 67 echo '返回的图片高度:'.$height.PHP_EOL;
62 - $cosService->cropCosImage($image); 68 + if($height > 220){
  69 + $result = $cosService->cropCosImage($image);
  70 + if(empty($result)){
  71 + continue;
63 } 72 }
  73 + $this->saveMysql($project_id,$result['size'],$result['type'],$result['path'],$result['mime']);
64 } 74 }
65 - return true; 75 + }
  76 + }
  77 + return $data;
66 } 78 }
67 79
68 /** 80 /**
  81 + * @remark :ai执行方法
  82 + * @name :_aiAction
  83 + * @author :lyh
  84 + * @method :post
  85 + * @time :2025/5/8 16:12
  86 + */
  87 + public function _aiAction($project_id,$keywordImage){
  88 + $cosService = new CosService();
  89 + //ai_blog裁剪
  90 + $ai_image = $this->getAiBlogImage($project_id,$keywordImage ?? '');
  91 + if(empty($ai_image)){
  92 + echo '当前图片不需要裁剪。'.PHP_EOL;
  93 + return true;
  94 + }
  95 + $height = $cosService->getImageHeight($ai_image);
  96 + if(empty($height)){
  97 + echo '未获取到AI_BLOG图片高度。'.PHP_EOL;
  98 + return true;
  99 + }
  100 + echo '返回的图片高度:'.$height.PHP_EOL;
  101 + if($height > 220){
  102 + $result = $cosService->cropCosImage($ai_image);
  103 + if(empty($result)){
  104 + return true;
  105 + }
  106 + $this->saveMysql($project_id,$result['size'],$result['type'],$result['path'],$result['mime']);
  107 + }
  108 + return true;
  109 + }
  110 + /**
69 * @remark :获取aiBlog图片 111 * @remark :获取aiBlog图片
70 * @name :getAiBlogImage 112 * @name :getAiBlogImage
71 * @author :lyh 113 * @author :lyh
@@ -73,17 +115,19 @@ class CropImage extends Command @@ -73,17 +115,19 @@ class CropImage extends Command
73 * @time :2025/5/8 10:42 115 * @time :2025/5/8 10:42
74 */ 116 */
75 public function getAiBlogImage($project_id,$keywordImage){ 117 public function getAiBlogImage($project_id,$keywordImage){
76 - $image = '';  
77 // AI博客banner type:1:产品,2:博客,3:新闻,4:AIBlog 118 // AI博客banner type:1:产品,2:博客,3:新闻,4:AIBlog
78 $webSettingImageModel = new WebSettingImage(); 119 $webSettingImageModel = new WebSettingImage();
79 $aiBlogInfo = $webSettingImageModel->read(['project_id' => $project_id, 'type' => 4],['image']); 120 $aiBlogInfo = $webSettingImageModel->read(['project_id' => $project_id, 'type' => 4],['image']);
80 - if($aiBlogInfo !== false && !empty($aiBlogInfo['image'])){  
81 - $image = getImageUrl($aiBlogInfo['image']); 121 + if($aiBlogInfo === false && !empty($keywordImage)){
  122 + $webSettingImageModel->addReturnId(['project_id'=>$project_id,'image'=>$keywordImage,'type'=>4]);
  123 + return '';
82 } 124 }
83 - if(empty($image) && !empty($keywordImage)){  
84 - $image = $keywordImage; 125 + if(empty($aiBlogInfo['image']) && !empty($keywordImage)){
  126 + $webSettingImageModel->edit(['image'=>$keywordImage],['id'=>$aiBlogInfo['id']]);
  127 + return '';
85 } 128 }
86 - return $image; 129 + $ai_image = str_replace_url($aiBlogInfo['image']);
  130 + return $ai_image;
87 } 131 }
88 132
89 /** 133 /**
@@ -147,25 +191,20 @@ class CropImage extends Command @@ -147,25 +191,20 @@ class CropImage extends Command
147 * @method :post 191 * @method :post
148 * @time :2025/5/8 14:59 192 * @time :2025/5/8 14:59
149 */ 193 */
150 - public function saveMysql($size,$image_type,$fileName,$hash,$is_cos = 0,$mime = '', $name=''){ 194 + public function saveMysql($project_id,$size,$image_type,$path,$mime = ''){
151 $imageModel = new Image(); 195 $imageModel = new Image();
152 $data = [ 196 $data = [
153 - 'path' => $this->path.'/'.$fileName,  
154 - 'created_at' => date('Y-m-d H:i:s',time()), 197 + 'path' => $path,
155 'size' => $size, 198 'size' => $size,
156 - 'hash' => $hash,  
157 - 'type'=>$image_type,  
158 - 'refer'=>$this->param['refer'] ?? 0,  
159 - 'is_cos'=>($is_cos == 0) ? 1 : 0,  
160 - 'mime'=>$mime,  
161 - 'project_id'=>$this->cache['project_id'] ?? 0,  
162 - 'name'=>$name,  
163 - 'en_name'=>$fileName 199 + 'hash' => hash_file('sha256', $path),
  200 + 'type' => $image_type,
  201 + 'refer'=> 0,
  202 + 'mime' => $mime,
  203 + 'project_id'=>$project_id,
  204 + 'name'=>basename($path),
  205 + 'en_name'=>basename($path)
164 ]; 206 ];
165 - $rs = $imageModel->add($data);  
166 - if ($rs === false) {  
167 - return $this->response('添加失败', Code::USER_ERROR);  
168 - } 207 + $imageModel->addReturnId($data);
169 return true; 208 return true;
170 } 209 }
171 210
@@ -363,11 +363,19 @@ class CosService @@ -363,11 +363,19 @@ class CosService
363 ] 363 ]
364 ]; 364 ];
365 // 执行裁剪并覆盖 365 // 执行裁剪并覆盖
366 - $cosClient->ImageProcess([ 366 + $res = $cosClient->ImageProcess([
367 'Bucket' => $cos['bucket'], 367 'Bucket' => $cos['bucket'],
368 'Key' => $cosUrl, // 要处理的对象路径 368 'Key' => $cosUrl, // 要处理的对象路径
369 'PicOperations' => json_encode($operations), 369 'PicOperations' => json_encode($operations),
370 ]); 370 ]);
371 - return $newKey; 371 + if($res){
  372 + return [
  373 + 'path' => $res['Key'] ?? '',
  374 + 'size' => (int)$res['ProcessResults']['Object'][0]['Size'] ?? 0,
  375 + 'mime' => 'image/'.($res['ProcessResults']['Object'][0]['Format'] ?? 'jpg'),
  376 + 'type' => $res['ProcessResults']['Object'][0]['Format'] ?? 'jpg',
  377 + ];
  378 + }
  379 + return [];
372 } 380 }
373 } 381 }