mirror of
https://github.com/kuaifan/dootask.git
synced 2026-01-27 04:58:12 +00:00
perf: 优化缩略图
This commit is contained in:
parent
423aad4179
commit
a393dec0a0
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -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;
|
||||
|
||||
4
resources/assets/js/functions/web.js
vendored
4
resources/assets/js/functions/web.js
vendored
@ -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)$/, '')
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user