perf: 优化缩略图

This commit is contained in:
kuaifan
2024-10-29 19:01:42 +08:00
parent 423aad4179
commit a393dec0a0
3 changed files with 55 additions and 23 deletions

View File

@@ -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;