<?php
declare(strict_types=1);
namespace classes;
use GdImage;
class ImgConverter
{
private array $info;
private ?GdImage $img;
public function setImg(string $inputPath): ImgConverter
{
$this->info = pathinfo($inputPath);
switch ($this->info['extension']) {
case 'jpg':
$this->img = imageCreateFromJpeg($inputPath);
break;
case 'png':
$this->img = imageCreateFromPng($inputPath);
break;
case 'gif':
$this->img = imageCreateFromGif($inputPath);
break;
case 'webp':
$this->img = imagecreatefromwebp($inputPath);
break;
}
return $this;
}
public function out(string $outPath, string $extension): bool
{
$outPath = $outPath . '.' . $extension;
if ($this->info['extension'] === $extension) {
return false;
}
switch ($extension) {
case 'jpg':
imagejpeg($this->img, $outPath, 100);
break;
case 'webp':
imageWebp($this->img, $outPath, 100);
break;
}
imagedestroy($this->img);
return true;
}
public function outWebp(string $outPath)
{
imageWebp($this->img, $outPath, 100);
imagedestroy($this->img);
}
public function outJpg(string $outPath)
{
imagejpeg($this->img, $outPath, 100);
imagedestroy($this->img);
}
}