作者 李宇航

合并分支 'lyh-server' 到 'master'

Lyh server



查看合并请求 !1941
... ... @@ -35,15 +35,10 @@ class CropImage extends Command
public function handle(){
echo '测试裁剪->cs-crop:'.PHP_EOL;
$images ='https://ecdn6.globalso.com/upload/p/1/image_other/2025-05/1746675561075.jpg';
$image = str_replace_url($images);
$cosService = new CosService();
$new_url = $cosService->cropAndUploadFromCos($image);
return $new_url;
// $project_id = $this->argument('project_id');
// ProjectServer::useProject($project_id);
// $this->_action();
// DB::disconnect('custom_mysql');
$project_id = $this->argument('project_id');
ProjectServer::useProject($project_id);
$this->_action($project_id);
DB::disconnect('custom_mysql');
}
/**
... ... @@ -60,8 +55,9 @@ class CropImage extends Command
foreach ($data as $val){
//处理图片为相对路径
$image = str_replace_url($val);
$new_url = $cosService->cropAndUploadFromCos($image);
echo '返回的图片路径:'.$new_url.PHP_EOL;
$height = $cosService->getImageHeight($image);
echo '返回的图片高度:'.$height.PHP_EOL;
$cosService->cropCosImage($image);
}
}
return true;
... ...
... ... @@ -315,28 +315,22 @@ class CosService
/**
* @remark :获取cos图片高度
* @name :cropAndUploadFromCos
* @name :getImageHeight
* @author :lyh
* @method :post
* @time :2025/5/8 10:58
* @param :pathUrl->存储桶路径
* @param :pathUrl->存储桶相对路径
*/
public function cropAndUploadFromCos($pathUrl,$maxHeight = 300){
public function getImageHeight($pathUrl){
$cos = config('filesystems.disks.cos');
$url = 'https://' . $cos['bucket'] . '.cos.' . $cos['region'] . '.myqcloud.com/' . ltrim($pathUrl, '/') . '?image/info';
try {
$imageInfo = @getimagesize($url);
if ($imageInfo) {
$width = $imageInfo[0];
$height = $imageInfo[1];
return "图片尺寸:宽度 {$width}px,高度 {$height}px";
} else {
return "错误:无法读取图片或链接无效";
}
} catch (\Exception $e) {
@file_put_contents(storage_path('logs/crop_image_error.log'), '获取图片高度失败: ' . $e->getMessage() . PHP_EOL, FILE_APPEND);
return '';
$imageInfo = @getimagesize($url);
if ($imageInfo) {
// $width = $imageInfo[0];
$height = $imageInfo[1];
return $height;
}
return '';
}
/**
... ... @@ -346,10 +340,9 @@ class CosService
* @method :post
* @time :2025/5/8 11:06
*/
public function cropCosImage($cosUrl,$height = 200)
public function cropCosImage($cosUrl,$height = 220)
{
$cos = config('filesystems.disks.cos');
// 初始化 COS 客户端
$cosClient = new Client([
'region' => $cos['region'],
'credentials' => [
... ... @@ -357,30 +350,24 @@ class CosService
'secretKey' => $cos['credentials']['secretKey'],
],
]);
// 定义 Pic-Operations JSON,用于裁剪并覆盖原图
$pathInfo = pathinfo($cosUrl);
$newKey = $pathInfo['dirname'] . '/' . $pathInfo['filename'] . '-'. time() . $pathInfo['extension'];
$newKey = $pathInfo['dirname'] . '/crop_' . $pathInfo['filename'] . $pathInfo['extension'];
$operations = [
'is_pic_info' => 0,
'rules' => [
[
// 注意 fileid 要 base64 编码,并与 Key 相同才能覆盖
'fileid' => base64_encode($newKey),
'fileid' => $newKey,
'rule' => 'imageMogr2/crop/x'.$height.'/gravity/center'
]
]
];
try {
// 执行裁剪并覆盖
$cosClient->ImageProcess([
'Bucket' => $cos['bucket'],
'Key' => $cosUrl, // 要处理的对象路径
'PicOperations' => json_encode($operations),
]);
return $newKey;
} catch (\Exception $e) {
@file_put_contents(storage_path('logs/crop_image_error.log'), var_export($e->getMessage(), true) . PHP_EOL, FILE_APPEND);
return '';
}
// 执行裁剪并覆盖
$cosClient->ImageProcess([
'Bucket' => $cos['bucket'],
'Key' => $cosUrl, // 要处理的对象路径
'PicOperations' => json_encode($operations),
]);
return $newKey;
}
}
... ...