Always use JPEG for Thumbnails

This commit is contained in:
seiichiro 2022-08-10 21:22:40 +02:00
parent d54027beeb
commit 54f9593e51

View file

@ -11,22 +11,15 @@ class thumb {
const IMAGE_HANDLERS = [ const IMAGE_HANDLERS = [
IMAGETYPE_JPEG => [ IMAGETYPE_JPEG => [
'load' => 'imagecreatefromjpeg', 'load' => 'imagecreatefromjpeg',
'save' => 'imagejpeg',
'quality' => 100
], ],
IMAGETYPE_PNG => [ IMAGETYPE_PNG => [
'load' => 'imagecreatefrompng', 'load' => 'imagecreatefrompng',
'save' => 'imagepng',
'quality' => 0
], ],
IMAGETYPE_GIF => [ IMAGETYPE_GIF => [
'load' => 'imagecreatefromgif', 'load' => 'imagecreatefromgif',
'save' => 'imagegif',
], ],
IMAGETYPE_WEBP => [ IMAGETYPE_WEBP => [
'load' => 'imagecreatefromwebp', 'load' => 'imagecreatefromwebp',
'save' => 'imagecreatewebp',
'quality' => 90
] ]
]; ];
@ -40,10 +33,8 @@ class thumb {
public function get_thumb($iname) { public function get_thumb($iname) {
$src = $this->imagedir.$iname; $src = $this->imagedir.$iname;
$ext = pathinfo($iname, PATHINFO_EXTENSION);
$dstname = hash('sha256', $src); $dstname = hash('sha256', $src);
$dstpath = $this->thumbdir.$dstname.'.'.$ext; $dstpath = $this->thumbdir.$dstname.'.jpg';
if (!file_exists($dstpath)) { if (!file_exists($dstpath)) {
$type = exif_imagetype($src); $type = exif_imagetype($src);
@ -78,15 +69,11 @@ class thumb {
} }
imagecopyresampled($thumbnail, $image, 0, 0, 0, 0, $width, $height, $srcwidth, $srcheight); imagecopyresampled($thumbnail, $image, 0, 0, 0, 0, $width, $height, $srcwidth, $srcheight);
if ($type != IMAGETYPE_GIF) { call_user_func('imagejpeg',$thumbnail,$dstpath,100);
call_user_func(self::IMAGE_HANDLERS[$type]['save'],$thumbnail,$dstpath,self::IMAGE_HANDLERS[$type]['quality']);
} else {
call_user_func(self::IMAGE_HANDLERS[$type]['save'],$thumbnail,$dstpath);
}
imagedestroy($image); imagedestroy($image);
imagedestroy($thumbnail); imagedestroy($thumbnail);
} }
return $this->thumburl.$dstname.'.'.$ext; return $this->thumburl.$dstname.'.jpg';
} }
} }