126 lines
3.7 KiB
PHP
126 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Libraries\MyUtil;
|
|
|
|
use App\Libraries\MyUtilLibrary;
|
|
use App\Traits\FileTrait;
|
|
|
|
class ImageLibrary extends MyUtilLibrary
|
|
{
|
|
use FileTrait;
|
|
private $_srcPath = "";
|
|
private $_dstPath = "";
|
|
private $_dstFile = "";
|
|
private $_image;
|
|
private $_imageType;
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
// 이미지의 현재 너비를 반환하는 메소드
|
|
final public function getSourcePath(): string
|
|
{
|
|
return $this->_srcPath;
|
|
}
|
|
final public function setSourcePath(string $srcPath): void
|
|
{
|
|
$this->_srcPath = $srcPath;
|
|
}
|
|
final public function getDestinationPath(): string
|
|
{
|
|
return $this->_dstPath;
|
|
}
|
|
final public function setDestinationPath(string $dstPath): void
|
|
{
|
|
$this->_dstPath = $dstPath;
|
|
}
|
|
final public function getDestinationFile(): string
|
|
{
|
|
return $this->_dstFile;
|
|
}
|
|
final public function setDestinationFile(string $dstFile): void
|
|
{
|
|
$this->_dstFile = $dstFile;
|
|
}
|
|
|
|
final public function getWidth()
|
|
{
|
|
return imagesx($this->_image);
|
|
}
|
|
// 이미지의 현재 높이를 반환하는 메소드
|
|
final public function getHeight()
|
|
{
|
|
return imagesy($this->_image);
|
|
}
|
|
// 이미지 파일을 로드하는 메소드
|
|
final protected function load($file)
|
|
{
|
|
$imageInfo = getimagesize($file);
|
|
$this->_imageType = $imageInfo[2];
|
|
switch ($this->_imageType) {
|
|
case IMAGETYPE_JPEG:
|
|
$this->_image = imagecreatefromjpeg($file);
|
|
break;
|
|
case IMAGETYPE_GIF:
|
|
$this->_image = imagecreatefromgif($file);
|
|
break;
|
|
case IMAGETYPE_PNG:
|
|
$this->_image = imagecreatefrompng($file);
|
|
break;
|
|
case IMAGETYPE_WEBP:
|
|
$this->_image = imagecreatefromwebp($file);
|
|
break;
|
|
}
|
|
}
|
|
// 이미지 크기를 지정된 너비, 높이로 변경하는 메소드
|
|
final protected function resize($width, $height)
|
|
{
|
|
$newImage = imagecreatetruecolor($width, $height);
|
|
imagecopyresampled($newImage, $this->_image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
|
|
$this->_image = $newImage;
|
|
}
|
|
// 이미지 비율을 유지하면서 크기를 조정하는 메소드
|
|
final protected function resizeToWidth($width)
|
|
{
|
|
$ratio = $width / $this->getWidth();
|
|
$height = $this->getHeight() * $ratio;
|
|
$this->resize($width, $height);
|
|
}
|
|
final protected function resizeToHeight($height)
|
|
{
|
|
$ratio = $height / $this->getHeight();
|
|
$width = $this->getWidth() * $ratio;
|
|
$this->resize($width, $height);
|
|
}
|
|
final protected function scale($scale)
|
|
{
|
|
$width = $this->getWidth() * ($scale / 100);
|
|
$height = $this->getHeight() * ($scale / 100);
|
|
$this->resize($width, $height);
|
|
}
|
|
// 이미지를 저장하는 메소드
|
|
final protected function save($file, $imageType = IMAGETYPE_WEBP, $compression = 75)
|
|
{
|
|
switch ($imageType) {
|
|
case IMAGETYPE_JPEG:
|
|
imagejpeg($this->_image, $file, $compression);
|
|
break;
|
|
case IMAGETYPE_GIF:
|
|
imagegif($this->_image, $file);
|
|
break;
|
|
case IMAGETYPE_PNG:
|
|
imagepng($this->_image, $file);
|
|
break;
|
|
case IMAGETYPE_WEBP:
|
|
default:
|
|
imagewebp($this->_image, $file, $compression);
|
|
break;
|
|
}
|
|
}
|
|
// 메모리 해제를 위한 메소드
|
|
final protected function destroy()
|
|
{
|
|
imagedestroy($this->_image);
|
|
}
|
|
}
|