Last active
August 29, 2015 14:25
-
-
Save Aquei/51eecc2e381688a9ce04 to your computer and use it in GitHub Desktop.
phpのgdでサムネイル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* サムネイル画像を作るクラス | |
* | |
* PHP 5 >= 5.5.0 | |
* | |
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0 | |
*/ | |
class Thumbnailer | |
{ | |
//イメージリソース | |
protected $image; | |
//このイメージリソースのwidth | |
protected $imageWidth; | |
//このイメージリソースのheight | |
protected $imageHeight; | |
//デフォルトのサムネイルwidth | |
protected $thumbnailWidth = 300; | |
/** | |
* コンストラクタ | |
* | |
* @param string $img 画像を文字列で読み込んだもの | |
*/ | |
function __construct($img){ | |
if(!extension_loaded('gd') || !function_exists('gd_info')){ | |
throw new runtimeexception("GDがロードされていません"); | |
} | |
$resource = imagecreatefromstring($img); | |
if(!$resource){ | |
throw new runtimeexception("イメージの種類がサポートされていない、データのフォーマットが識別できない、 イメージが壊れておりリロードできない"); | |
} | |
$this->image = $resource; | |
$this->imageWidth = imagesx($this->image); | |
$this->imageHeight = imagesy($this->image); | |
} | |
function __destruct(){ | |
imagedestroy($this->image); | |
} | |
/** | |
* サムネイル画像のwidthを指定する | |
* | |
* @param int $width サムネイルのwidth | |
* @return true | |
*/ | |
function setThumbnailWidth($width){ | |
if(is_int($width)){ | |
$this->thumbnailWidth = $width; | |
} | |
return true; | |
} | |
/** | |
* サムネイルを返す | |
* | |
* @param int $mode スケーリングアルゴリズム | |
* @param int $thumbnail_width optional サムネイルのwidth | |
* @return png string | |
*/ | |
function thumbnailPng($mode = IMG_BILINEAR_FIXED, $thumbnail_width = null){ | |
if($thumbnail_width === null){ | |
$thumbnail_width = $this->thumbnailWidth; | |
} | |
//オリジナルより大きなサイズがサムネイルサイズに指定されていたら、 | |
//サムネイルサイズはオリジナルサイズに | |
if($this->imageWidth < $thumbnail_width){ | |
$thumbnail_width = $this->imageWidth; | |
} | |
$resized_resource = imagescale($this->image, $thumbnail_width, -1, $mode); | |
return $this->getPng($resized_resource); | |
} | |
/** | |
* resampledされたサムネイルを返す | |
* | |
* @param bool $isResampled optional trulyならリサンプリングする | |
* @param int $thumbnail_width optional サムネイルのwidth | |
* @return png string | |
*/ | |
function imagecopyThumbnailPng($isResampled = false, $thumbnail_width = null){ | |
if($thumbnail_width === null){ | |
$thumbnail_width = $this->thumbnailWidth; | |
} | |
$ratio = $this->imageHeight / $this->imageWidth; | |
$thumbnail_height = $thumbnail_width * $ratio; | |
$image_p = imagecreatetruecolor($thumbnail_width, $thumbnail_height); | |
if($isResampled){ | |
imagecopyresampled($image_p, $this->image, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $this->imageWidth, $this->imageHeight); | |
}else{ | |
imagecopyresized($image_p, $this->image, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $this->imageWidth, $this->imageHeight); | |
} | |
return $this->getPng($image_p); | |
} | |
/** | |
* イメージリソースからpngを返す | |
* | |
* @param resource $img optional イメージリソース | |
* @param int $level optional png圧縮のレベル 低0 - 9高 | |
* @return png string; | |
*/ | |
protected function getPng($img = null, $level = 9){ | |
if($img === null){ | |
$img = $this->image; | |
} | |
ob_start(); | |
imagepng($img, null, $level); | |
$res = ob_get_contents(); | |
ob_end_clean(); | |
return $res; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment