[*] refactoring thumbs class

This commit is contained in:
rsgrinko committed 2023-10-19 00:39:46 +03:00
1 parent 890d9b8e0d
commit 12ee13c3d1
1 file changed
+59 -42
+59 -42
View File
@@ -25,41 +25,58 @@
class Thumbs class Thumbs
{ {
/** @var string $filename */
private string $filename;
/** @var int $width */
private $width;
/** @var int $height */
private $height;
/** @var int $type */
private $type;
/**
* @var false|\GdImage|resource
*/
private $img;
public function __construct(string $filename) public function __construct(string $filename)
{ {
$this->filename = $filename; $this->filename = $filename;
if (empty($this->filename)) { if (empty($this->filename)) {
throw new CoreException('Файл не найден'); throw new CoreException('Файл не найден');
} else { }
$info = getimagesize($this->filename); $info = getimagesize($this->filename);
if (empty($info)) { if (empty($info)) {
throw new CoreException('Файл не найден'); throw new CoreException('Файл не найден');
} else { }
$this->width = $info[0]; $this->width = $info[0];
$this->height = $info[1]; $this->height = $info[1];
$this->type = $info[2]; $this->type = $info[2];
switch ($this->type) { switch ($this->type) {
case 1: case IMAGETYPE_GIF:
$this->img = imageCreateFromGif($this->filename); $this->img = imagecreatefromgif($this->filename);
break; break;
case 2: case IMAGETYPE_JPEG:
$this->img = imageCreateFromJpeg($this->filename); $this->img = imagecreatefromjpeg($this->filename);
break; break;
case 3: case IMAGETYPE_PNG:
$this->img = imageCreateFromPng($this->filename); $this->img = imagecreatefrompng($this->filename);
imageAlphaBlending($this->img, true); imagealphablending($this->img, true);
imageSaveAlpha($this->img, true); imagesavealpha($this->img, true);
break; break;
case 18: case IMAGETYPE_WEBP:
$this->img = imageCreatefromWebp($this->filename); $this->img = imagecreatefromwebp($this->filename);
break; break;
default: default:
throw new CoreException('Формат файла не подерживается'); throw new CoreException('Формат файла не поддерживается');
break; break;
} }
} }
}
}
public function fixWebpBlue() public function fixWebpBlue()
{ {
@@ -93,7 +110,7 @@
} }
$tmp = imageCreateTrueColor($width, $height); $tmp = imageCreateTrueColor($width, $height);
if ($this->type == 1 || $this->type == 3) { if ($this->type === IMAGETYPE_GIF || $this->type === IMAGETYPE_PNG) {
imagealphablending($tmp, true); imagealphablending($tmp, true);
imageSaveAlpha($tmp, true); imageSaveAlpha($tmp, true);
$transparent = imagecolorallocatealpha($tmp, 0, 0, 0, 127); $transparent = imagecolorallocatealpha($tmp, 0, 0, 0, 127);
@@ -230,7 +247,7 @@
} }
$tmp = imageCreateTrueColor($width, $height); $tmp = imageCreateTrueColor($width, $height);
if ($this->type == 1 || $this->type == 3) { if ($this->type === IMAGETYPE_GIF || $this->type === IMAGETYPE_PNG) {
imagealphablending($tmp, true); imagealphablending($tmp, true);
imageSaveAlpha($tmp, true); imageSaveAlpha($tmp, true);
$transparent = imagecolorallocatealpha($tmp, 0, 0, 0, 127); $transparent = imagecolorallocatealpha($tmp, 0, 0, 0, 127);
@@ -263,9 +280,9 @@
$tw = ceil($height / ($this->height / $this->width)); $tw = ceil($height / ($this->height / $this->width));
$th = ceil($width / ($this->width / $this->height)); $th = ceil($width / ($this->width / $this->height));
if ($this->width == $this->height) { if ($this->width === $this->height) {
// Источник - квадратная фотка // Источник - квадратная фотка
if ($width == $height) { if ($width === $height) {
// Превью - квадратная. // Превью - квадратная.
$this->resize($width, $height); $this->resize($width, $height);
} elseif ($width > $height) { } elseif ($width > $height) {
@@ -341,9 +358,9 @@
$tw = ceil($height / ($this->height / $this->width)); $tw = ceil($height / ($this->height / $this->width));
$th = ceil($width / ($this->width / $this->height)); $th = ceil($width / ($this->width / $this->height));
if ($this->width != $width && $this->height != $height) { if ($this->width !== $width && $this->height !== $height) {
$tmp = imageCreateTrueColor($width, $height); $tmp = imageCreateTrueColor($width, $height);
if ($this->type == 1 || $this->type == 3) { if ($this->type === IMAGETYPE_GIF || $this->type === IMAGETYPE_PNG) {
imagealphablending($tmp, true); imagealphablending($tmp, true);
imageSaveAlpha($tmp, true); imageSaveAlpha($tmp, true);
$transparent = imagecolorallocatealpha($tmp, 0, 0, 0, 127); $transparent = imagecolorallocatealpha($tmp, 0, 0, 0, 127);
@@ -563,9 +580,9 @@
unset($tmp); unset($tmp);
return true; return true;
} else {
return $this->cut($width, $height);
} }
return $this->cut($width, $height);
} }
} }
@@ -626,19 +643,21 @@
{ {
if (empty($file)) { if (empty($file)) {
throw new CoreException('Файл маски не найден'); throw new CoreException('Файл маски не найден');
} else { }
$info = getimagesize($file); $info = getimagesize($file);
if (empty($info)) { if (empty($info)) {
throw new CoreException('Файл маски не найден'); throw new CoreException('Файл маски не найден');
} else { }
switch ($info[2]) { switch ($info[2]) {
case 1: case IMAGETYPE_GIF:
$dest = imageCreateFromGif($file); $dest = imageCreateFromGif($file);
break; break;
case 2: case IMAGETYPE_JPEG:
$dest = imageCreateFromJpeg($file); $dest = imageCreateFromJpeg($file);
break; break;
case 3: case IMAGETYPE_PNG:
$dest = imageCreateFromPng($file); $dest = imageCreateFromPng($file);
$transparent = imagecolorallocatealpha($dest, 0, 0, 0, 127); $transparent = imagecolorallocatealpha($dest, 0, 0, 0, 127);
imagefill($dest, 0, 0, $transparent); imagefill($dest, 0, 0, $transparent);
@@ -649,7 +668,7 @@
break; break;
case 18: case IMAGETYPE_WEBP:
$dest = imageCreatefromWebp($file); $dest = imageCreatefromWebp($file);
break; break;
default: default:
@@ -699,8 +718,6 @@
$dest = $this->opacity($dest, $transparency); $dest = $this->opacity($dest, $transparency);
imagecopy($this->img, $dest, $x, $y, 0, 0, $info[0], $info[1]); imagecopy($this->img, $dest, $x, $y, 0, 0, $info[0], $info[1]);
} }
}
}
/** /**
* Сохраняет изображение в файл. * Сохраняет изображение в файл.
@@ -736,13 +753,13 @@
} }
switch ($this->type) { switch ($this->type) {
case 1: case IMAGETYPE_GIF:
return $this->saveGif($filename); return $this->saveGif($filename);
case 2: case IMAGETYPE_JPEG:
return $this->saveJpg($filename, $quality); return $this->saveJpg($filename, $quality);
case 3: case IMAGETYPE_PNG:
return $this->savePng($filename); return $this->savePng($filename);
case 18: case IMAGETYPE_WEBP:
return $this->saveWebp($filename); return $this->saveWebp($filename);
} }
} }
@@ -753,19 +770,19 @@
public function output($quality = 100): void public function output($quality = 100): void
{ {
switch ($this->type) { switch ($this->type) {
case 1: case IMAGETYPE_GIF:
header('Content-Type: image/gif'); header('Content-Type: image/gif');
imageGif($this->img); imageGif($this->img);
break; break;
case 2: case IMAGETYPE_JPEG:
header('Content-Type: image/jpeg'); header('Content-Type: image/jpeg');
imageJpeg($this->img, null, $quality); imageJpeg($this->img, null, $quality);
break; break;
case 3: case IMAGETYPE_PNG:
header('Content-Type: image/x-png'); header('Content-Type: image/x-png');
imagePng($this->img); imagePng($this->img);
break; break;
case 18: case IMAGETYPE_WEBP:
header('Content-Type: image/webp'); header('Content-Type: image/webp');
imageWebp($this->img, $quality); imageWebp($this->img, $quality);
break; break;
@@ -782,15 +799,15 @@
} }
switch ($this->type) { switch ($this->type) {
case 1: case IMAGETYPE_GIF:
header('Content-Type: image/gif'); header('Content-Type: image/gif');
imageGif($this->img, $filename); imageGif($this->img, $filename);
break; break;
case 2: case IMAGETYPE_JPEG:
header('Content-Type: image/jpeg'); header('Content-Type: image/jpeg');
imageJpeg($this->img, $filename, $quality); imageJpeg($this->img, $filename, $quality);
break; break;
case 3: case IMAGETYPE_PNG:
header('Content-Type: image/x-png'); header('Content-Type: image/x-png');
imagePng($this->img, $filename); imagePng($this->img, $filename);
break; break;