作者 lyh

Merge branch 'master' of http://47.244.231.31:8099/zhl/globalso-v6 into lyh-server

... ... @@ -4,10 +4,10 @@ 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;
use Illuminate\Support\Facades\Redis;
class ThumbProjectImage extends Command
{
... ... @@ -16,7 +16,7 @@ class ThumbProjectImage extends Command
*
* @var string
*/
protected $signature = 'thumb_project_image {project_id}';
protected $signature = 'thumb_project_image';
/**
* The console command description.
... ... @@ -27,79 +27,49 @@ class ThumbProjectImage extends Command
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');
}
while (true) {
$this->start_thumb();
}
}
$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'];
public function start_thumb()
{
$key = 'thumb_project_image';
$project_id = Redis::rpop($key);
if (empty($project_id)) {
sleep(60);
return true;
}
$this->output('project_id:' . $project_id . ' | start');
$this->output('project_id:' . $project_id . ' | start');
$project_info = ProjectServer::useProject($project_id);
$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();
if ($project_info) {
$thumb_w = $project_info->deploy_build->thumb_w ?? 0;
Product::select(['id', 'project_id', 'gallery'])->chunk(100, function ($products) use ($thumb_w) {
foreach ($products as $product) {
$thumb = $product['gallery'][0] ?? [];
if (isset($thumb['url']) && $thumb['url']) {
$thumb['url'] = thumbImageByUrl($thumb['url'], $thumb_w);
$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');
}
}
}
$this->output('project_id:' . $product->project_id . ',product_id:' . $product->id . ',thumb:' . $thumb_w . ' | success');
}
});
DB::disconnect('custom_mysql');
}
}
});
$this->output('project_id:' . $project_id . ' | end');
}
DB::disconnect('custom_mysql');
}
$this->output('project_id:' . $project_id . ' | end');
return true;
}
... ...
... ... @@ -1468,68 +1468,66 @@ function diffInHours($startTime, $endTime)
return round($hours, 1);
}
/**
* 通过图片地址压缩图片
* @param $url
* @param int $width
* @return string
* @author Akun
* @date 2025/09/01 15:18
*/
function thumbImageByUrl($url, $width = 360)
{
if (empty($url)) {
return $url;
}
if (!function_exists('thumbImageByUrl')) {
/**
* 通过图片地址压缩图片
* @param $url
* @param int $width
* @return string
* @author Akun
* @date 2025/09/01 15:18
*/
function thumbImageByUrl($url, $width = 360)
{
if (empty($url)) {
return $url;
}
if ($width == 0) {
return $url;
}
if ($width == 0) {
return $url;
}
if (strpos($url, '_thumb') !== false) {
return $url;
}
//获取图片完整访问地址
$url_complete = getImageUrl($url, 0, 0, 0);//先用v6-file地址
if (strpos($url_complete, 'v6-file') !== false) {
$is_exists = checkRemoteFileExists($url_complete);
if (!$is_exists) {
//不存在,再用cdn地址
$url_complete = getImageUrl($url);
}
}
//获取图片完整访问地址
$url_complete = getImageUrl($url, 0, 0, 0);//先用v6-file地址
if (strpos($url_complete, 'v6-file') !== false) {
$is_exists = checkRemoteFileExists($url_complete);
if (!$is_exists) {
//不存在,再用cdn地址
$url_complete = getImageUrl($url);
//获取与原图存储路径相同的压缩路径
$path = parse_url($url_complete, PHP_URL_PATH);
$path_arr = explode('.', $path);
if (count($path_arr) != 2) {
return $url;
}
}
$path_arr[0] = $path_arr[0] . '_thumbW' . $width;
$key = implode('.', $path_arr);
//获取与原图存储路径相同的压缩路径
$path = parse_url($url_complete, PHP_URL_PATH);
$path_arr = explode('.', $path);
if (count($path_arr) != 2) {
return $url;
}
$path_arr[0] = $path_arr[0] . '_thumbW' . $width;
$key = implode('.', $path_arr);
try {
$img = \Intervention\Image\Facades\Image::make($url_complete);
try {
$img = \Intervention\Image\Facades\Image::make($url_complete);
//宽度按设定,高度自动调整
$img->resize($width, null, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
//宽度按设定,高度自动调整
$img->resize($width, null, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
//获取处理后的图片二进制资源
$resource = $img->stream()->__toString();
//获取处理后的图片二进制资源
$resource = $img->stream()->__toString();
//上传存储桶
$thumb_url = CosService::uploadRemote('', '', '', $key, $resource);
//上传存储桶
$thumb_url = CosService::uploadRemote('', '', '', $key, $resource);
$url = $thumb_url ? $thumb_url : $url;
} catch (\Exception $e) {
Log::channel('thumb_img')->error($e->getMessage(), [$url_complete, $width]);
}
$url = $thumb_url ? $thumb_url : $url;
} catch (\Exception $e) {
Log::channel('thumb_img')->error($e->getMessage(), [$url, $width]);
return $url;
}
return $url;
}
if (!function_exists('checkRemoteFileExists')) {
... ...
... ... @@ -59,6 +59,7 @@ use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log as LogInfo;
use Illuminate\Support\Facades\Redis;
/**
* Class ProjectLogic
... ... @@ -528,6 +529,13 @@ class ProjectLogic extends BaseLogic
protected function saveProjectDeployBuild($deploy_build){
$deployBuildModel = new DeployBuild();
$deploy_build['configuration'] = Arr::a2s(!empty($deploy_build['configuration']) ? $deploy_build['configuration'] : []);
//如果更改了缩略图压缩大小,同步写入处理缩略图任务队列
$old_thumb_info = $deployBuildModel->read(['id'=>$deploy_build['id']],['thumb_w']);
if(isset($old_thumb_info['thumb_w']) && $old_thumb_info['thumb_w'] != $deploy_build['thumb_w']){
Redis::lpush('thumb_project_image',$deploy_build['project_id']);
}
$deployBuildModel->edit($deploy_build,['id'=>$deploy_build['id']]);
return $this->success();
}
... ...