|
@@ -315,28 +315,22 @@ class CosService |
|
@@ -315,28 +315,22 @@ class CosService |
|
315
|
|
315
|
|
|
316
|
/**
|
316
|
/**
|
|
317
|
* @remark :获取cos图片高度
|
317
|
* @remark :获取cos图片高度
|
|
318
|
- * @name :cropAndUploadFromCos
|
318
|
+ * @name :getImageHeight
|
|
319
|
* @author :lyh
|
319
|
* @author :lyh
|
|
320
|
* @method :post
|
320
|
* @method :post
|
|
321
|
* @time :2025/5/8 10:58
|
321
|
* @time :2025/5/8 10:58
|
|
322
|
- * @param :pathUrl->存储桶路径
|
322
|
+ * @param :pathUrl->存储桶相对路径
|
|
323
|
*/
|
323
|
*/
|
|
324
|
- public function cropAndUploadFromCos($pathUrl,$maxHeight = 300){
|
324
|
+ public function getImageHeight($pathUrl){
|
|
325
|
$cos = config('filesystems.disks.cos');
|
325
|
$cos = config('filesystems.disks.cos');
|
|
326
|
$url = 'https://' . $cos['bucket'] . '.cos.' . $cos['region'] . '.myqcloud.com/' . ltrim($pathUrl, '/') . '?image/info';
|
326
|
$url = 'https://' . $cos['bucket'] . '.cos.' . $cos['region'] . '.myqcloud.com/' . ltrim($pathUrl, '/') . '?image/info';
|
|
327
|
- try {
|
|
|
|
328
|
- $imageInfo = @getimagesize($url);
|
|
|
|
329
|
- if ($imageInfo) {
|
|
|
|
330
|
- $width = $imageInfo[0];
|
|
|
|
331
|
- $height = $imageInfo[1];
|
|
|
|
332
|
- return "图片尺寸:宽度 {$width}px,高度 {$height}px";
|
|
|
|
333
|
- } else {
|
|
|
|
334
|
- return "错误:无法读取图片或链接无效";
|
|
|
|
335
|
- }
|
|
|
|
336
|
- } catch (\Exception $e) {
|
|
|
|
337
|
- @file_put_contents(storage_path('logs/crop_image_error.log'), '获取图片高度失败: ' . $e->getMessage() . PHP_EOL, FILE_APPEND);
|
|
|
|
338
|
- return '';
|
327
|
+ $imageInfo = @getimagesize($url);
|
|
|
|
328
|
+ if ($imageInfo) {
|
|
|
|
329
|
+// $width = $imageInfo[0];
|
|
|
|
330
|
+ $height = $imageInfo[1];
|
|
|
|
331
|
+ return $height;
|
|
339
|
}
|
332
|
}
|
|
|
|
333
|
+ return '';
|
|
340
|
}
|
334
|
}
|
|
341
|
|
335
|
|
|
342
|
/**
|
336
|
/**
|
|
@@ -346,10 +340,9 @@ class CosService |
|
@@ -346,10 +340,9 @@ class CosService |
|
346
|
* @method :post
|
340
|
* @method :post
|
|
347
|
* @time :2025/5/8 11:06
|
341
|
* @time :2025/5/8 11:06
|
|
348
|
*/
|
342
|
*/
|
|
349
|
- public function cropCosImage($cosUrl,$height = 200)
|
343
|
+ public function cropCosImage($cosUrl,$height = 220)
|
|
350
|
{
|
344
|
{
|
|
351
|
$cos = config('filesystems.disks.cos');
|
345
|
$cos = config('filesystems.disks.cos');
|
|
352
|
- // 初始化 COS 客户端
|
|
|
|
353
|
$cosClient = new Client([
|
346
|
$cosClient = new Client([
|
|
354
|
'region' => $cos['region'],
|
347
|
'region' => $cos['region'],
|
|
355
|
'credentials' => [
|
348
|
'credentials' => [
|
|
@@ -357,30 +350,24 @@ class CosService |
|
@@ -357,30 +350,24 @@ class CosService |
|
357
|
'secretKey' => $cos['credentials']['secretKey'],
|
350
|
'secretKey' => $cos['credentials']['secretKey'],
|
|
358
|
],
|
351
|
],
|
|
359
|
]);
|
352
|
]);
|
|
360
|
- // 定义 Pic-Operations JSON,用于裁剪并覆盖原图
|
|
|
|
361
|
$pathInfo = pathinfo($cosUrl);
|
353
|
$pathInfo = pathinfo($cosUrl);
|
|
362
|
- $newKey = $pathInfo['dirname'] . '/' . $pathInfo['filename'] . '-'. time() . $pathInfo['extension'];
|
354
|
+ $newKey = $pathInfo['dirname'] . '/crop_' . $pathInfo['filename'] . $pathInfo['extension'];
|
|
363
|
$operations = [
|
355
|
$operations = [
|
|
364
|
'is_pic_info' => 0,
|
356
|
'is_pic_info' => 0,
|
|
365
|
'rules' => [
|
357
|
'rules' => [
|
|
366
|
[
|
358
|
[
|
|
367
|
// 注意 fileid 要 base64 编码,并与 Key 相同才能覆盖
|
359
|
// 注意 fileid 要 base64 编码,并与 Key 相同才能覆盖
|
|
368
|
- 'fileid' => base64_encode($newKey),
|
360
|
+ 'fileid' => $newKey,
|
|
369
|
'rule' => 'imageMogr2/crop/x'.$height.'/gravity/center'
|
361
|
'rule' => 'imageMogr2/crop/x'.$height.'/gravity/center'
|
|
370
|
]
|
362
|
]
|
|
371
|
]
|
363
|
]
|
|
372
|
];
|
364
|
];
|
|
373
|
- try {
|
|
|
|
374
|
- // 执行裁剪并覆盖
|
|
|
|
375
|
- $cosClient->ImageProcess([
|
|
|
|
376
|
- 'Bucket' => $cos['bucket'],
|
|
|
|
377
|
- 'Key' => $cosUrl, // 要处理的对象路径
|
|
|
|
378
|
- 'PicOperations' => json_encode($operations),
|
|
|
|
379
|
- ]);
|
|
|
|
380
|
- return $newKey;
|
|
|
|
381
|
- } catch (\Exception $e) {
|
|
|
|
382
|
- @file_put_contents(storage_path('logs/crop_image_error.log'), var_export($e->getMessage(), true) . PHP_EOL, FILE_APPEND);
|
|
|
|
383
|
- return '';
|
|
|
|
384
|
- }
|
365
|
+ // 执行裁剪并覆盖
|
|
|
|
366
|
+ $cosClient->ImageProcess([
|
|
|
|
367
|
+ 'Bucket' => $cos['bucket'],
|
|
|
|
368
|
+ 'Key' => $cosUrl, // 要处理的对象路径
|
|
|
|
369
|
+ 'PicOperations' => json_encode($operations),
|
|
|
|
370
|
+ ]);
|
|
|
|
371
|
+ return $newKey;
|
|
385
|
}
|
372
|
}
|
|
386
|
} |
373
|
} |