vsgallery/lib/thumbs.class.php

89 lines
2.4 KiB
PHP
Raw Normal View History

2022-01-08 16:22:16 +00:00
<?php
class thumb {
protected string $imagedir;
protected string $thumbdir;
2022-02-11 16:19:03 +00:00
protected string $thumburl;
2022-01-08 16:22:16 +00:00
protected int $w = 200;
protected int $h = 200;
const IMAGE_HANDLERS = [
IMAGETYPE_JPEG => [
'load' => 'imagecreatefromjpeg',
'save' => 'imagejpeg',
'quality' => 100
],
IMAGETYPE_PNG => [
'load' => 'imagecreatefrompng',
'save' => 'imagepng',
'quality' => 0
],
IMAGETYPE_GIF => [
'load' => 'imagecreatefromgif',
'save' => 'imagegif',
],
IMAGETYPE_WEBP => [
'load' => 'imagecreatefromwebp',
'save' => 'imagecreatewebp',
'quality' => 90
]
];
2022-02-11 16:19:03 +00:00
public function __construct($idir, $tdir, $turl, $width, $heigth) {
2022-01-08 16:22:16 +00:00
$this->imagedir = $idir;
$this->thumbdir = $tdir;
2022-02-11 16:19:03 +00:00
$this->thumburl = $turl;
2022-01-08 16:22:16 +00:00
$this->w = $width;
$this->h = $heigth;
}
public function get_thumb($iname) {
$src = $this->imagedir.$iname;
$ext = pathinfo($iname, PATHINFO_EXTENSION);
2022-02-11 12:20:52 +00:00
$dstname = hash('sha256', $src);
2022-01-08 16:22:16 +00:00
$dstpath = $this->thumbdir.$dstname.'.'.$ext;
if (!file_exists($dstpath)) {
$type = exif_imagetype($src);
2022-02-11 16:13:20 +00:00
if ($type == 0) {
error_log("$src: Unknown Image Type");
return "img/error.png";
}
2022-01-08 16:22:16 +00:00
$image = call_user_func(self::IMAGE_HANDLERS[$type]['load'], $src);
$srcwidth = imagesx($image);
$srcheight = imagesy($image);
$ratio = min($this->w / $srcwidth, $this->h / $srcheight);
$width = round($srcwidth*$ratio);
$height = round($srcheight*$ratio);
$thumbnail = imagecreatetruecolor($width, $height);
if ($type == IMAGETYPE_GIF || $type == IMAGETYPE_PNG) {
imagecolortransparent($thumbnail, imagecolorallocate($thumbnail, 0, 0, 0));
if ($type == IMAGETYPE_PNG) {
imagealphablending($thumbnail, false);
imagesavealpha($thumbnail, true);
}
}
imagecopyresampled($thumbnail, $image, 0, 0, 0, 0, $width, $height, $srcwidth, $srcheight);
2022-02-03 14:37:54 +00:00
if ($type != IMAGETYPE_GIF) {
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);
}
2022-01-08 16:22:16 +00:00
imagedestroy($image);
imagedestroy($thumbnail);
}
2022-02-11 16:19:03 +00:00
return $this->thumburl.$dstname.'.'.$ext;
2022-01-08 16:22:16 +00:00
}
}
?>