open($filepath); $this->set_bgcolor_transparent(); } public function __destruct() { $this->close(); } public function create( int $width, int $height ):self { $this->resource = imagecreatetruecolor($width,$height); return $this; } // 画像を開く public function open( string $filepath ):self { $this->close(); $this->resource = null; if (false !== getimagesize($filepath)) { exif_imagetype($filepath); $resource = match (exif_imagetype($filepath)) { 1 => imageCreateFromGif($filepath), 2 => imageCreateFromJpeg($filepath), 3 => imageCreateFromPng($filepath), 6 => imageCreateFromBmp($filepath), }; if (!is_null($resource)) { $this->resource = $resource; } } return $this; } public function close():self { if (!is_null($this->resource)) imagedestroy($this->resource); return $this; } public function getResource() { return $this->resource; } // 画像を保存 public function save( string $filepath, int $quality = 100 ):self { $png_quality = intval(round(($quality - 100) / 11.111111)); match (pathinfo($filepath,PATHINFO_EXTENSION)) { 'jpeg','jpg' => imagejpeg($this->resource,$filepath,$quality), 'png' => imagepng($this->resource,$filepath,$png_quality), 'gif' => imagegif($this->resource,$filepath), 'bmp' => imagebmp($this->resource,$filepath), }; return $this; } // 画像を出力 public function __toString():string { ob_start(); imagejpeg($this->resource,null,100); $data = ob_get_contents(); ob_end_clean(); return ('' == $data) ? '' : 'data:image/jpeg;base64,'.base64_encode($data); } // 画像をリサイズ(正方形) public function resize_square( $length ):self { $left = $top = 0; if ($this->get_width() > $this->get_height()) { $this->resize($length,0); $top = intval(($length - $this->get_height()) / 2); } else { $this->resize(0,$length); $left = intval(($length - $this->get_width()) / 2); } $width = $this->get_width(); $height = $this->get_height(); $old_resource = $this->resource; $this->create($length,$length)->fill($this->bgcolor); imagecopy($this->resource,$old_resource,$left,$top,0,0,$width,$height); imagedestroy($old_resource); return $this; } // 画像をリサイズ public function resize( int $width = 0, int $height = 0 ):self { $old_width = $this->get_width(); $old_heigth = $this->get_height(); if (0 == $width && 0 != $height) { $width = intval(round(($height / $old_heigth) * $old_width)); } else if (0 == $height && 0 != $width) { $height = intval(round(($width / $old_width) * $old_heigth)); } $old_resource = $this->resource; $this->create($width,$height); imagecopyresampled($this->resource,$old_resource,0,0,0,0,$width,$height,$old_width,$old_heigth); imagedestroy($old_resource); return $this; } // 画像を切り抜く public function trim( int $top = 0, int $right = 0, int $bottom = 0, int $left = 0 ):self { $width = $this->get_width() - $left - $right; $height = $this->get_height() - $top - $bottom; $old_resource = $this->resource; $this->create($width,$height); imagecopy($this->resource,$old_resource,0,0,$left,$top,$width,$height); imagedestroy($old_resource); return $this; } // 画像をコピー public function copy( Image $img, int $dx = 0 , int $dy =0 ):self { imagecopy($this->resource,$img->get_resource(),$dx,$dy,0,0,$img->get_width(),$img->get_height()); return $this; } // 画像を回転 public function rotate( float $angle ):self { $old_resource = $this->resource; $this->resource = imagerotate($this->resource,$angle,$this->bgcolor,0); imagedestroy($old_resource); return $this; } // 画像を反転 public function flip( int $mode ):self { imageflip($this->resource,$mode); return $this; } // 画像を塗りつぶす public function fill( int $color ):self { imagefill($this->resource,0,0,$color); return $this; } public function get_resource() { return $this->resource; } public function get_width():int { return imagesx($this->resource); } public function get_height():int { return imagesy($this->resource); } public function set_bgcolor( int $bgcolor ):self { $this->bgcolor = $bgcolor; return $this; } // 背景を透明にする public function set_bgcolor_transparent():self { $this->bgcolor = imagecolorallocatealpha($this->resource,0,0,0,127); return $this; } // 背景を白にする public function set_bgcolor_white():self { $this->bgcolor = imagecolorallocatealpha($this->resource,255,255,255,0); return $this; } // 背景を黒にする public function set_bgcolor_black():self { $this->bgcolor = imagecolorallocatealpha($this->resource,0,0,0,0); return $this; } public function get_bgcolor():int { return $this->bgcolor; } }