mirror of
https://github.com/kuaifan/dootask.git
synced 2026-01-30 17:20:26 +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'];
|
$array['thumb'] = $array['path'];
|
||||||
if ($array['ext'] === 'gif' && !isset($param['autoThumb'])) {
|
if ($array['ext'] === 'gif' && !isset($param['autoThumb'])) {
|
||||||
@ -2456,12 +2462,6 @@ class Base
|
|||||||
}
|
}
|
||||||
$array['thumb'] = Base::fillUrl($array['thumb']);
|
$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);
|
return Base::retSuccess('success', $array);
|
||||||
} else {
|
} else {
|
||||||
@ -2507,9 +2507,7 @@ class Base
|
|||||||
*/
|
*/
|
||||||
public static function isThumb($file): bool
|
public static function isThumb($file): bool
|
||||||
{
|
{
|
||||||
return str_ends_with($file, '_thumb.jpeg')
|
return preg_match('/_thumb\.(jpg|jpeg|png)$/', $file);
|
||||||
|| str_ends_with($file, '_thumb.jpg')
|
|
||||||
|| str_ends_with($file, '_thumb.png');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -2533,19 +2531,11 @@ class Base
|
|||||||
/**
|
/**
|
||||||
* 缩略图还原
|
* 缩略图还原
|
||||||
* @param $file
|
* @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 preg_replace('/_thumb\.(jpg|jpeg|png)$/', '', $file);
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -24,6 +24,15 @@ class Image
|
|||||||
public function __construct($imagePath) {
|
public function __construct($imagePath) {
|
||||||
$this->path = $imagePath;
|
$this->path = $imagePath;
|
||||||
$this->image = new Imagick($this->path);
|
$this->image = new Imagick($this->path);
|
||||||
|
$this->updateSize();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新图片尺寸
|
||||||
|
* @return void
|
||||||
|
* @throws \ImagickException
|
||||||
|
*/
|
||||||
|
private function updateSize() {
|
||||||
$geo = $this->image->getImageGeometry();
|
$geo = $this->image->getImageGeometry();
|
||||||
$this->height = $geo['height'];
|
$this->height = $geo['height'];
|
||||||
$this->width = $geo['width'];
|
$this->width = $geo['width'];
|
||||||
@ -47,6 +56,36 @@ class Image
|
|||||||
return $this->height;
|
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
|
* @param int $width
|
||||||
@ -97,6 +136,7 @@ class Image
|
|||||||
} else {
|
} else {
|
||||||
$this->image->thumbnailImage($width, $height);
|
$this->image->thumbnailImage($width, $height);
|
||||||
}
|
}
|
||||||
|
$this->updateSize();
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -191,6 +231,10 @@ class Image
|
|||||||
if ($quality > 0) {
|
if ($quality > 0) {
|
||||||
Image::compressImage($savePath, null, $quality);
|
Image::compressImage($savePath, null, $quality);
|
||||||
}
|
}
|
||||||
|
if (filesize($savePath) >= filesize($imagePath)) {
|
||||||
|
unlink($savePath);
|
||||||
|
symlink(basename($imagePath), $savePath);
|
||||||
|
}
|
||||||
return $extension;
|
return $extension;
|
||||||
} catch (\ImagickException) {
|
} catch (\ImagickException) {
|
||||||
return null;
|
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}
|
* @returns {*|string}
|
||||||
*/
|
*/
|
||||||
thumbRestore(url) {
|
thumbRestore(url) {
|
||||||
url = $A.rightDelete(url, '_thumb.jpg')
|
return `${url}`.replace(/_thumb\.(jpg|jpeg|png)$/, '')
|
||||||
url = $A.rightDelete(url, '_thumb.png')
|
|
||||||
return url
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user