CropImage.php
7.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
/**
* @remark :
* @name :CropImage.php
* @author :lyh
* @method :post
* @time :2025/5/8 9:19
*/
namespace App\Console\Commands\CropImage;
use App\Enums\Common\Code;
use App\Models\Domain\DomainInfo;
use App\Models\File\Image;
use App\Models\WebSetting\AggregationSetting;
use App\Models\WebSetting\WebSettingImage;
use App\Services\CosService;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class CropImage extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'crop_image {project_id}';
/**
* The console command description.
*
* @var string
*/
protected $description = '裁剪图片';
public function handle(){
echo '测试裁剪->cs-crop:'.PHP_EOL;
$project_id = $this->argument('project_id');
ProjectServer::useProject($project_id);
$data = $this->_keywordAction($project_id);
$this->_aiAction($project_id,$data[0] ?? []);
DB::disconnect('custom_mysql');
}
/**
* @remark :执行的方法
* @name :_action
* @author :lyh
* @method :post
* @time :2025/5/8 9:21
*/
public function _keywordAction($project_id){
//聚合页裁剪
$data = $this->getKeywordImage($project_id);
$cosService = new CosService();
if(!empty($data)){
foreach ($data as $val){
//处理图片为相对路径
$image = str_replace_url($val);
$height = $cosService->getImageHeight($image);
if(empty($height)){
echo '未获取到图片高度。'.PHP_EOL;
continue;
}
echo '返回的图片高度:'.$height.PHP_EOL;
if($height > 220){
$result = $cosService->cropCosImage($image);
if(empty($result)){
continue;
}
$this->saveMysql($project_id,$result['size'],$result['type'],$result['path'],$result['mime']);
}
}
}
return $data;
}
/**
* @remark :ai执行方法
* @name :_aiAction
* @author :lyh
* @method :post
* @time :2025/5/8 16:12
*/
public function _aiAction($project_id,$keywordImage){
$cosService = new CosService();
//ai_blog裁剪
$ai_image = $this->getAiBlogImage($project_id,$keywordImage ?? '');
if(empty($ai_image)){
echo '当前图片不需要裁剪。'.PHP_EOL;
return true;
}
$height = $cosService->getImageHeight($ai_image);
if(empty($height)){
echo '未获取到AI_BLOG图片高度。'.PHP_EOL;
return true;
}
echo '返回的图片高度:'.$height.PHP_EOL;
if($height > 220){
$result = $cosService->cropCosImage($ai_image);
if(empty($result)){
return true;
}
$this->saveMysql($project_id,$result['size'],$result['type'],$result['path'],$result['mime']);
}
return true;
}
/**
* @remark :获取aiBlog图片
* @name :getAiBlogImage
* @author :lyh
* @method :post
* @time :2025/5/8 10:42
*/
public function getAiBlogImage($project_id,$keywordImage){
// AI博客banner type:1:产品,2:博客,3:新闻,4:AIBlog
$webSettingImageModel = new WebSettingImage();
$aiBlogInfo = $webSettingImageModel->read(['project_id' => $project_id, 'type' => 4],['image']);
if($aiBlogInfo === false && !empty($keywordImage)){
$webSettingImageModel->addReturnId(['project_id'=>$project_id,'image'=>$keywordImage,'type'=>4]);
return '';
}
if(empty($aiBlogInfo['image']) && !empty($keywordImage)){
$webSettingImageModel->edit(['image'=>$keywordImage],['id'=>$aiBlogInfo['id']]);
return '';
}
$ai_image = str_replace_url($aiBlogInfo['image']);
return $ai_image;
}
/**
* @remark :获取聚合页图片
* @name :getImage
* @author :lyh
* @method :post
* @time :2025/5/8 9:21
*/
public function getKeywordImage($project_id){
$data = [];
// 聚合页banner
$aggregationSettingModel = new AggregationSetting();
$aggregationSettingInfo = $aggregationSettingModel->read(['project_id' => $project_id],['id','top_banner']);
if($aggregationSettingInfo !== false && !empty($aggregationSettingInfo['top_banner'])){
foreach ($aggregationSettingInfo['top_banner'] as $val){
if($val != 'jpg' && $val != 'png' && $val != 'webp'){
$data[] = $val;
}
}
}
if(empty($data)){
//重页面上获取首页banner
$data = $this->getDomImage($project_id);
}
return $data;
}
/**
* @remark :页面上获取图片
* @name :getDomImage
* @author :lyh
* @method :post
* @time :2025/5/8 10:32
*/
public function getDomImage($project_id){
$data = [];
echo '获取首页banner:' . $project_id . PHP_EOL;
$domainModel = new DomainInfo();
$domainInfo = $domainModel->read(['project_id' => $project_id, 'status' => 1]);
if ($domainInfo !== false) {
$dom = @file_get_html('https://' . $domainInfo['domain'] . '/');
if (empty($dom)) {
$this->output('获取HTML失败: ' . $project_id);
}else{
$banner_dom = $dom->find('main .section-banner-wrap-block img', 0);
$data[] = $banner_dom ? $banner_dom->src : '';
$dom->clear();
unset($dom);
}
}else{
$this->output('域名不存在: ' . $project_id);
}
return $data;
}
/**
* @remark :写入数据库
* @name :saveMysql
* @author :lyh
* @method :post
* @time :2025/5/8 14:59
*/
public function saveMysql($project_id,$size,$image_type,$path,$mime = ''){
$imageModel = new Image();
$data = [
'path' => $path,
'size' => $size,
'hash' => hash_file('sha256', $path),
'type' => $image_type,
'refer'=> 0,
'mime' => $mime,
'project_id'=>$project_id,
'name'=>basename($path),
'en_name'=>basename($path)
];
$imageModel->addReturnId($data);
return true;
}
/**
* @remark :记录日志
* @name :output
* @author :lyh
* @method :post
* @time :2025/5/8 9:57
*/
public function output($message, $log_file = 'logs/crop_image.log')
{
$message = date('Y-m-d H:i:s') . ' ' . $message . PHP_EOL;
echo $message;
file_put_contents(storage_path($log_file), $message, FILE_APPEND);
return true;
}
}