diff --git a/app/Module/Base.php b/app/Module/Base.php index de25685be..850fb5151 100755 --- a/app/Module/Base.php +++ b/app/Module/Base.php @@ -2444,6 +2444,12 @@ class Base } } } + // 压缩图片 + $quality = intval($param['quality']); + if ($quality > 0) { + Image::compressImage($array['file'], null, $quality); + $array['size'] = Base::twoFloat(filesize($array['file']) / 1024, true); + } // 生成缩略图 $array['thumb'] = $array['path']; if ($array['ext'] === 'gif' && !isset($param['autoThumb'])) { @@ -2456,12 +2462,6 @@ class Base } $array['thumb'] = Base::fillUrl($array['thumb']); } - // 压缩图片 - $quality = intval($param['quality']); - if ($quality > 0) { - Image::compressImage($array['file'], null, $quality); - $array['size'] = Base::twoFloat(filesize($array['file']) / 1024, true); - } // return Base::retSuccess('success', $array); } else { @@ -2507,9 +2507,7 @@ class Base */ public static function isThumb($file): bool { - return str_ends_with($file, '_thumb.jpeg') - || str_ends_with($file, '_thumb.jpg') - || str_ends_with($file, '_thumb.png'); + return preg_match('/_thumb\.(jpg|jpeg|png)$/', $file); } /** @@ -2533,19 +2531,11 @@ class Base /** * 缩略图还原 * @param $file - * @return mixed|string + * @return string */ - public static function thumbRestore($file): mixed + public static function thumbRestore($file): string { - if (str_ends_with($file, '_thumb.jpeg')) { - return Base::rightDelete($file, '_thumb.jpeg'); - } elseif (str_ends_with($file, '_thumb.jpg')) { - return Base::rightDelete($file, '_thumb.jpg'); - } elseif (str_ends_with($file, '_thumb.png')) { - return Base::rightDelete($file, '_thumb.png'); - } else { - return $file; - } + return preg_replace('/_thumb\.(jpg|jpeg|png)$/', '', $file); } /** diff --git a/app/Module/Image.php b/app/Module/Image.php index a16af0a4c..97f895f73 100644 --- a/app/Module/Image.php +++ b/app/Module/Image.php @@ -24,6 +24,15 @@ class Image public function __construct($imagePath) { $this->path = $imagePath; $this->image = new Imagick($this->path); + $this->updateSize(); + } + + /** + * 更新图片尺寸 + * @return void + * @throws \ImagickException + */ + private function updateSize() { $geo = $this->image->getImageGeometry(); $this->height = $geo['height']; $this->width = $geo['width']; @@ -47,6 +56,36 @@ class Image return $this->height; } + /** + * 按比例裁剪 + * @param int $ratio + * @return $this + * @throws \ImagickException + */ + public function ratioCrop(int $ratio = 0): static + { + if ($ratio === 0) { + return $this; + } + if ($ratio < 1) { + $ratio = 1 / $ratio; + } + $width = $this->width; + $height = $this->height; + if ($width > $height * $ratio) { + $newWidth = $height * $ratio; + $newHeight = $height; + } elseif ($height > $width * $ratio) { + $newWidth = $width; + $newHeight = $width * 3; + } else { + return $this; + } + $this->image->cropImage($newWidth, $newHeight, ($width - $newWidth) / 2, ($height - $newHeight) / 2); + $this->updateSize(); + return $this; + } + /** * 创建缩略图 * @param int $width @@ -97,6 +136,7 @@ class Image } else { $this->image->thumbnailImage($width, $height); } + $this->updateSize(); return $this; } @@ -191,6 +231,10 @@ class Image if ($quality > 0) { Image::compressImage($savePath, null, $quality); } + if (filesize($savePath) >= filesize($imagePath)) { + unlink($savePath); + symlink(basename($imagePath), $savePath); + } return $extension; } catch (\ImagickException) { return null; diff --git a/resources/assets/js/functions/web.js b/resources/assets/js/functions/web.js index c76692b52..c380a3dc5 100755 --- a/resources/assets/js/functions/web.js +++ b/resources/assets/js/functions/web.js @@ -500,9 +500,7 @@ import {MarkdownPreview} from "../store/markdown"; * @returns {*|string} */ thumbRestore(url) { - url = $A.rightDelete(url, '_thumb.jpg') - url = $A.rightDelete(url, '_thumb.png') - return url + return `${url}`.replace(/_thumb\.(jpg|jpeg|png)$/, '') }, /**