正在显示
1 个修改的文件
包含
122 行增加
和
0 行删除
app/Console/Commands/Sync/SyncImage.php
0 → 100644
| 1 | +<?php | ||
| 2 | +/** | ||
| 3 | + * @remark : | ||
| 4 | + * @name :SyncImage.php | ||
| 5 | + * @author :lyh | ||
| 6 | + * @method :post | ||
| 7 | + * @time :2024/9/11 10:39 | ||
| 8 | + */ | ||
| 9 | + | ||
| 10 | +namespace App\Console\Commands\Sync; | ||
| 11 | + | ||
| 12 | +use App\Models\File\Image; | ||
| 13 | +use App\Models\File\ImageSetting; | ||
| 14 | +use Illuminate\Console\Command; | ||
| 15 | +use Qcloud\Cos\Client; | ||
| 16 | + | ||
| 17 | +class SyncImage extends Command | ||
| 18 | +{ | ||
| 19 | + /** | ||
| 20 | + * The name and signature of the console command. | ||
| 21 | + * | ||
| 22 | + * @var string | ||
| 23 | + */ | ||
| 24 | + protected $signature = 'sync_file'; | ||
| 25 | + | ||
| 26 | + /** | ||
| 27 | + * The console command description. | ||
| 28 | + * | ||
| 29 | + * @var string | ||
| 30 | + */ | ||
| 31 | + protected $description = '同步图片与文件'; | ||
| 32 | + | ||
| 33 | + | ||
| 34 | + public function handle(){ | ||
| 35 | + $str = $this->getProjectConfig(501); | ||
| 36 | + $imageModel = new Image(); | ||
| 37 | + $lists = $imageModel->list(['project_id'=>501]); | ||
| 38 | + $domain = 'http://globalso-v6-1309677403.cos.ap-hongkong.myqcloud.com';//cos域名 | ||
| 39 | + foreach ($lists as $k => $v){ | ||
| 40 | + $url = $domain . $v['path'].'/'.$str; | ||
| 41 | + echo date('Y-m-d H:i:s') . '水印路径:'. $url .',主键id:'. $v['id'] . PHP_EOL; | ||
| 42 | +// $this->coverOriginalImage($url,$v['path']); | ||
| 43 | + } | ||
| 44 | + return true; | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + /** | ||
| 48 | + * @remark :添加水印后保存图片(覆盖/非覆盖的文件未存入数据库) | ||
| 49 | + * @name :uploadImages | ||
| 50 | + * @author :lyh | ||
| 51 | + * @method :post | ||
| 52 | + * @time :2024/8/19 17:06 | ||
| 53 | + */ | ||
| 54 | + public function coverOriginalImage($url,$cdnUrl){ | ||
| 55 | + // 获取水印后的图片内容 | ||
| 56 | + $imageContent = file_get_contents($url); | ||
| 57 | + // 使用 COS SDK 将图片重新上传并覆盖原图 | ||
| 58 | + $cos = config('filesystems.disks.cos'); | ||
| 59 | + $cosClient = new Client([ | ||
| 60 | + 'region' => $cos['region'], | ||
| 61 | + 'credentials' => [ | ||
| 62 | + 'secretId' => $cos['credentials']['secretId'], | ||
| 63 | + 'secretKey' => $cos['credentials']['secretKey'], | ||
| 64 | + ], | ||
| 65 | + ]); | ||
| 66 | + // 上传并覆盖原图 | ||
| 67 | + $cosClient->putObject([ | ||
| 68 | + 'Bucket' => $cos['bucket'], | ||
| 69 | + 'Key' => $cdnUrl, // 去掉域名部分,得到存储桶内的路径 | ||
| 70 | + 'Body' => $imageContent, | ||
| 71 | + ]); | ||
| 72 | + return $cos['cdn'].$cdnUrl; | ||
| 73 | + } | ||
| 74 | + | ||
| 75 | + /** | ||
| 76 | + * @remark :获取图片配置 | ||
| 77 | + * @name :getProjectConfig | ||
| 78 | + * @author :lyh | ||
| 79 | + * @method :post | ||
| 80 | + * @time :2024/8/24 11:03 | ||
| 81 | + */ | ||
| 82 | + public function getProjectConfig($project_id = 0){ | ||
| 83 | + $str = ''; | ||
| 84 | + $imageSettingModel = new ImageSetting(); | ||
| 85 | + $settingInfo = $imageSettingModel->read(['project_id'=>$project_id]); | ||
| 86 | + if($settingInfo !== false){ | ||
| 87 | + if($settingInfo['status'] == 1 && !empty($settingInfo['image_data'])){ | ||
| 88 | + $image_data = json_decode($settingInfo['image_data'],true); | ||
| 89 | + foreach ($image_data as $k => $v){ | ||
| 90 | + if (str_starts_with($v, "image/")) { | ||
| 91 | + $v = 'image/'.urlSafeBase64Encode(substr($v, strlen("image/"))); | ||
| 92 | + } | ||
| 93 | + $image_data[$k] = $v; | ||
| 94 | + } | ||
| 95 | + $str = 'watermark/1/'.implode('/',$image_data); | ||
| 96 | + return $str; | ||
| 97 | + } | ||
| 98 | + if($settingInfo['status'] == 2 && !empty($settingInfo['str_data'])){ | ||
| 99 | + $str_data = json_decode($settingInfo['str_data'],true); | ||
| 100 | + foreach ($str_data as $k => $v){ | ||
| 101 | + $arr = explode('/',$v); | ||
| 102 | + if ($arr[0] == 'text') { | ||
| 103 | + $arr[1] = urlSafeBase64Encode($arr[1]); | ||
| 104 | + $v = implode('/',$arr); | ||
| 105 | + } | ||
| 106 | + if ($arr[0] == 'font') { | ||
| 107 | + $arr[1] = urlSafeBase64Encode($arr[1]); | ||
| 108 | + $v = implode('/',$arr); | ||
| 109 | + } | ||
| 110 | + if ($arr[0] == 'fill') { | ||
| 111 | + $arr[1] = urlSafeBase64Encode($arr[1]); | ||
| 112 | + $v = implode('/',$arr); | ||
| 113 | + } | ||
| 114 | + $str_data[$k] = $v; | ||
| 115 | + } | ||
| 116 | + $str = 'watermark/2/'.implode('/',$str_data); | ||
| 117 | + return $str; | ||
| 118 | + } | ||
| 119 | + } | ||
| 120 | + return $str; | ||
| 121 | + } | ||
| 122 | +} |
-
请 注册 或 登录 后发表评论