更改缩略图生成逻辑
This commit is contained in:
@@ -184,45 +184,4 @@ class Controller extends BaseController
|
||||
echo $contents;
|
||||
}, headers: ['Content-type' => $mimetype]);
|
||||
}
|
||||
|
||||
public function thumbnail(Request $request): StreamedResponse
|
||||
{
|
||||
/** @var Image $image */
|
||||
$image = Image::query()
|
||||
->where('key', $request->route('key'))
|
||||
->where('extension', strtolower($request->route('extension')))
|
||||
->firstOr(fn() => abort(404));
|
||||
|
||||
try {
|
||||
$cacheKey = "image_thumb_{$image->key}";
|
||||
|
||||
if (Cache::has($cacheKey)) {
|
||||
$contents = Cache::get($cacheKey);
|
||||
} else {
|
||||
$stream = $image->filesystem()->readStream($image->pathname);
|
||||
$img = InterventionImage::make($stream);
|
||||
|
||||
$width = $w = $image->width;
|
||||
$height = $h = $image->height;
|
||||
|
||||
$max = 400; // 最大宽高
|
||||
|
||||
if ($w > $max && $h > $max) {
|
||||
$scale = min($max / $w, $max / $h);
|
||||
$width = (int)($w * $scale);
|
||||
$height = (int)($h * $scale);
|
||||
}
|
||||
|
||||
$contents = $img->fit($width, $height, fn($constraint) => $constraint->upsize())->encode('png');
|
||||
Cache::rememberForever($cacheKey, fn () => (string)$contents);
|
||||
}
|
||||
} catch (FilesystemException $e) {
|
||||
Utils::e($e, '图片缩略图输出时出现异常');
|
||||
abort(404);
|
||||
}
|
||||
|
||||
return \response()->stream(function () use ($contents) {
|
||||
echo $contents;
|
||||
}, headers: ['Content-type' => 'image/png']);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -180,7 +180,15 @@ class Image extends Model
|
||||
public function thumbUrl(): Attribute
|
||||
{
|
||||
return new Attribute(function () {
|
||||
return asset("{$this->key}.{$this->extension}!thumbnail");
|
||||
|
||||
$path = trim(env('THUMBNAIL_PATH', 'thumbnails'), '/');
|
||||
|
||||
// 没有缩略图则返回原图
|
||||
if (! file_exists($path)) {
|
||||
return $this->url;
|
||||
}
|
||||
|
||||
return asset($path."/{$this->md5}.png");
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -225,6 +225,9 @@ class ImageService
|
||||
}
|
||||
}
|
||||
|
||||
// 生成缩略图
|
||||
$this->makeThumbnail($image, $img);
|
||||
|
||||
return $image;
|
||||
}
|
||||
|
||||
@@ -444,6 +447,47 @@ class ImageService
|
||||
return $image;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成缩略图,缩略图目录必须在 public 目录下,且该目录必须存在
|
||||
*
|
||||
* @param mixed $image 图片数据
|
||||
* @param mixed $data 物理图片数据
|
||||
* @param int $max 最大宽高
|
||||
* @param bool $force 是否强制覆盖
|
||||
* @return void
|
||||
*/
|
||||
public function makeThumbnail(Image $image, mixed $data, int $max = 400, bool $force = false): void
|
||||
{
|
||||
$pathname = public_path(env('THUMBNAIL_PATH', 'thumbnails').'/'.$image->md5.'.png');
|
||||
|
||||
if (! file_exists($pathname) || $force) {
|
||||
try {
|
||||
// 创建文件夹
|
||||
if (! is_dir(dirname($pathname))) {
|
||||
@mkdir(dirname($pathname));
|
||||
}
|
||||
|
||||
@ini_set('memory_limit', '512M');
|
||||
|
||||
$img = InterventionImage::make($data);
|
||||
|
||||
$width = $w = $image->width;
|
||||
$height = $h = $image->height;
|
||||
|
||||
if ($w > $max && $h > $max) {
|
||||
$scale = min($max / $w, $max / $h);
|
||||
$width = (int)($w * $scale);
|
||||
$height = (int)($h * $scale);
|
||||
}
|
||||
|
||||
$img->fit($width, $height, fn($constraint) => $constraint->upsize())->encode('png')->save($pathname);
|
||||
|
||||
} catch (\Throwable $e) {
|
||||
Utils::e($e, '生成缩略图时出现异常');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取水印画布
|
||||
*
|
||||
|
||||
2
public/thumbnails/.gitignore
vendored
Normal file
2
public/thumbnails/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
*
|
||||
!.gitignore
|
||||
Reference in New Issue
Block a user