|
...
|
...
|
@@ -376,4 +376,75 @@ class CosService |
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @remark :ai_video裁剪图片为4张
|
|
|
|
* @name :cropAndUploadToCOS
|
|
|
|
* @author :lyh
|
|
|
|
* @method :post
|
|
|
|
* @time :2025/8/2 16:52
|
|
|
|
*/
|
|
|
|
public function cropAndUploadToCOS($imageUrl)
|
|
|
|
{
|
|
|
|
// 1. 下载远程图片内容
|
|
|
|
$imageData = file_get_contents($imageUrl);
|
|
|
|
if (!$imageData) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// 2. 保存原图到临时文件
|
|
|
|
$tempOriginal = tempnam(sys_get_temp_dir(), 'original_') . '.png';
|
|
|
|
file_put_contents($tempOriginal, $imageData);
|
|
|
|
// 3. 使用 GD 加载图像
|
|
|
|
$src = imagecreatefrompng($tempOriginal);
|
|
|
|
if (!$src) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$width = imagesx($src);
|
|
|
|
$height = imagesy($src);
|
|
|
|
$halfWidth = intval($width / 2);
|
|
|
|
$halfHeight = intval($height / 2);
|
|
|
|
// 4. 从原图 URL 提取路径信息
|
|
|
|
$parsed = parse_url($imageUrl);
|
|
|
|
$pathInfo = pathinfo($parsed['path']); // upload/p/1/png/2025-08/688dcebc26a7a59911.png
|
|
|
|
$cosPath = ltrim($pathInfo['dirname'], '/'); // 相对路径:upload/p/1/png/2025-08
|
|
|
|
$baseName = $pathInfo['filename']; // 文件名:688dcebc26a7a59911
|
|
|
|
$ext = $pathInfo['extension'] ?? 'png'; // 扩展名
|
|
|
|
// 5. 初始化 COS 客户端
|
|
|
|
$cos = config('filesystems.disks.cos');
|
|
|
|
$cosClient = new Client([
|
|
|
|
'region' => $cos['region'],
|
|
|
|
'credentials' => [
|
|
|
|
'secretId' => $cos['credentials']['secretId'],
|
|
|
|
'secretKey' => $cos['credentials']['secretKey'],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
// 6. 循环裁剪并上传
|
|
|
|
$resultPaths = [];
|
|
|
|
$index = 0;
|
|
|
|
for ($y = 0; $y < 2; $y++) {
|
|
|
|
for ($x = 0; $x < 2; $x++) {
|
|
|
|
$crop = imagecreatetruecolor($halfWidth, $halfHeight);
|
|
|
|
imagecopy($crop, $src, 0, 0, $x * $halfWidth, $y * $halfHeight, $halfWidth, $halfHeight);
|
|
|
|
$tempCropped = tempnam(sys_get_temp_dir(), 'crop_') . '.png';
|
|
|
|
imagepng($crop, $tempCropped);
|
|
|
|
imagedestroy($crop);
|
|
|
|
// 新文件名,保持路径不变
|
|
|
|
$filename = $baseName . '_part' . $index++ . '.' . $ext;
|
|
|
|
$objectKey = $cosPath . '/' . $filename;
|
|
|
|
// 上传到 COS
|
|
|
|
$cosClient->putObject([
|
|
|
|
'Bucket' => $cos['bucket'],
|
|
|
|
'Key' => $objectKey,
|
|
|
|
'Body' => fopen($tempCropped, 'rb'),
|
|
|
|
]);
|
|
|
|
// 返回相对路径
|
|
|
|
$resultPaths[] = $objectKey;
|
|
|
|
unlink($tempCropped);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 清理资源
|
|
|
|
imagedestroy($src);
|
|
|
|
unlink($tempOriginal);
|
|
|
|
return $resultPaths; // 相对路径数组
|
|
|
|
}
|
|
|
|
} |
...
|
...
|
|