ThumbProjectImage.php
4.2 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
<?php
namespace App\Console\Commands\Project;
use App\Helper\Arr;
use App\Models\Product\Product;
use App\Models\Project\Project;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class ThumbProjectImage extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'thumb_project_image {project_id}';
/**
* The console command description.
*
* @var string
*/
protected $description = '处理项目产品缩略图';
public function handle()
{
$project_id = $this->argument('project_id');
if ($project_id > 0) {
//指定项目
$this->output('project_id:' . $project_id . ' | start');
$project_info = ProjectServer::useProject($project_id);
if ($project_info) {
$thumb_w = $project_info->deploy_build->thumb_w ?? 0;
Product::select(['id', 'project_id', 'thumb'])->chunk(100, function ($products) use ($thumb_w) {
foreach ($products as $product) {
$thumb = $product->thumb;
if (isset($thumb['url']) && $thumb['url']) {
$new_thumb = thumbImageByUrl($thumb['url'], $thumb_w);
if ($new_thumb != $thumb['url']) {
$thumb['url'] = $new_thumb;
$json_thumb = Arr::a2s($thumb);
if (strlen($json_thumb) <= 500) {
$product->timestamps = false;
$product->thumb = $json_thumb;
$product->save();
$this->output('project_id:' . $product->project_id . ',product_id:' . $product->id . ' | success');
}
}
}
}
});
DB::disconnect('custom_mysql');
}
$this->output('project_id:' . $project_id . ' | end');
} else {
//所有项目
$projectModel = new Project();
$list = $projectModel->list(['delete_status' => 0, 'is_upgrade' => 0, 'type' => ['in', [1, 2, 3, 4, 6]]], 'id', ['id'], 'asc');
foreach ($list as $k => $v) {
$project_id = $v['id'];
$this->output('project_id:' . $project_id . ' | start');
$project_info = ProjectServer::useProject($project_id);
if ($project_info) {
$thumb_w = $project_info->deploy_build->thumb_w ?? 0;
Product::select(['id', 'project_id', 'thumb'])->chunk(100, function ($products) use ($thumb_w) {
foreach ($products as $product) {
$thumb = $product->thumb;
if (isset($thumb['url']) && $thumb['url']) {
$new_thumb = thumbImageByUrl($thumb['url'], $thumb_w);
if ($new_thumb != $thumb['url']) {
$thumb['url'] = $new_thumb;
$json_thumb = Arr::a2s($thumb);
if (strlen($json_thumb) <= 500) {
$product->timestamps = false;
$product->thumb = $json_thumb;
$product->save();
$this->output('project_id:' . $product->project_id . ',product_id:' . $product->id . ' | success');
}
}
}
}
});
DB::disconnect('custom_mysql');
}
$this->output('project_id:' . $project_id . ' | end');
}
}
}
/**
* 输出处理日志
* @param $message
* @return bool
*/
public function output($message)
{
echo date('Y-m-d H:i:s') . ' | ' . $message . PHP_EOL;
return true;
}
}