SyncImage.php
4.8 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
<?php
/**
* @remark :
* @name :SyncImage.php
* @author :lyh
* @method :post
* @time :2024/9/11 10:39
*/
namespace App\Console\Commands\Sync;
use App\Models\File\Image;
use App\Models\File\ImageSetting;
use App\Services\UpyunService;
use Illuminate\Console\Command;
use Qcloud\Cos\Client;
class SyncImage extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'sync_shuiying_image';
/**
* The console command description.
*
* @var string
*/
protected $description = '同步图片与文件';
// public function handle(){
// $str = $this->getProjectConfig(501);
// $imageModel = new Image();
// $lists = $imageModel->list(['project_id'=>501]);
// $domain = 'http://globalso-v6-1309677403.cos.ap-hongkong.myqcloud.com';//cos域名
// foreach ($lists as $k => $v){
// if($v['path'] == '/upload/p/501/image_product/2024-09/6569ac3a212aa39368.png'){
// continue;
// }
// $url = $domain . $v['path'].'?'.$str;
// echo date('Y-m-d H:i:s') . '水印路径:'. $url .',主键id:'. $v['id'] . PHP_EOL;
// $this->coverOriginalImage($url,$v['path']);
// }
// return true;
// }
public function handle(){
$data = [];
$domain = 'https://ecdn6.globalso.com';
$imageModel = new Image();
$lists = $imageModel->list(['project_id'=>501]);
foreach ($lists as $k => $v){
if($v['path'] == '/upload/p/501/image_product/2024-09/6569ac3a212aa39368.png'){
continue;
}
$url = $domain . $v['path'];
echo date('Y-m-d H:i:s') . '刷新路径:'. $url .',主键id:'. $v['id'] . PHP_EOL;
$data[] = $url;
}
$yunService = new UpyunService();
$yunService->preheatPush($data);
}
/**
* @remark :添加水印后保存图片(覆盖/非覆盖的文件未存入数据库)
* @name :uploadImages
* @author :lyh
* @method :post
* @time :2024/8/19 17:06
*/
public function coverOriginalImage($url,$cdnUrl){
// 获取水印后的图片内容
$imageContent = file_get_contents($url);
// 使用 COS SDK 将图片重新上传并覆盖原图
$cos = config('filesystems.disks.cos');
$cosClient = new Client([
'region' => $cos['region'],
'credentials' => [
'secretId' => $cos['credentials']['secretId'],
'secretKey' => $cos['credentials']['secretKey'],
],
]);
// 上传并覆盖原图
$cosClient->putObject([
'Bucket' => $cos['bucket'],
'Key' => $cdnUrl, // 去掉域名部分,得到存储桶内的路径
'Body' => $imageContent,
]);
return $cos['cdn'].$cdnUrl;
}
/**
* @remark :获取图片配置
* @name :getProjectConfig
* @author :lyh
* @method :post
* @time :2024/8/24 11:03
*/
public function getProjectConfig($project_id = 0){
$str = '';
$imageSettingModel = new ImageSetting();
$settingInfo = $imageSettingModel->read(['project_id'=>$project_id]);
if($settingInfo !== false){
if($settingInfo['status'] == 1 && !empty($settingInfo['image_data'])){
$image_data = json_decode($settingInfo['image_data'],true);
foreach ($image_data as $k => $v){
if (str_starts_with($v, "image/")) {
$v = 'image/'.urlSafeBase64Encode(substr($v, strlen("image/")));
}
$image_data[$k] = $v;
}
$str = 'watermark/1/'.implode('/',$image_data);
return $str;
}
if($settingInfo['status'] == 2 && !empty($settingInfo['str_data'])){
$str_data = json_decode($settingInfo['str_data'],true);
foreach ($str_data as $k => $v){
$arr = explode('/',$v);
if ($arr[0] == 'text') {
$arr[1] = urlSafeBase64Encode($arr[1]);
$v = implode('/',$arr);
}
if ($arr[0] == 'font') {
$arr[1] = urlSafeBase64Encode($arr[1]);
$v = implode('/',$arr);
}
if ($arr[0] == 'fill') {
$arr[1] = urlSafeBase64Encode($arr[1]);
$v = implode('/',$arr);
}
$str_data[$k] = $v;
}
$str = 'watermark/2/'.implode('/',$str_data);
return $str;
}
}
return $str;
}
}