outputImage($_GET["path"], $w, $h); if($ret === false) { header("Content-Type: text/html; charset=utf-8"); echo $obj_thumb->getErrorResult(); } /** * サムネイル出力クラス * */ class thumb { var $error = ""; var $default = 175; /** * コンストラクタ(PHP5対応) * */ function __construct() { } /* * コンストラクタ */ function thumb() { } function outputImage($path, $maxwidth, $maxheight) { if(!isset($path)) { $this->error = "画像へのパスが設定されていません"; return false; } if(!file_exists($path)) { $this->error = "ファイルが見つかりません"; return false; } $size = getimagesize($path); if($size == false) { $this->error = "ファイルの形式が不正です"; return false; } if($maxwidth == 0 && $maxheight == 0) { $maxwidth = $this->default; $maxheight = $this->default; } $re_size = $this->_getThumbSize($path, $maxwidth, $maxheight); // リサイズの必要がない場合は,そのまま出力 if($re_size[2] == 0) { echo readfile($path); break; } $imagecreate = function_exists("imagecreatetruecolor") ? "imagecreatetruecolor" : "imagecreate"; $imageresize = function_exists("imagecopyresampled") ? "imagecopyresampled" : "imagecopyresized"; switch($size[2]): case "1": // gif形式 if(function_exists("imagecreatefromgif")) { $src_im = imagecreatefromgif($path); $dst_im = $imagecreate($re_size[0], $re_size[1]); $transparent = imagecolorat($src_im, $dummy_x, $dummy_y); $colorstotal = imagecolorstotal ($src_im); if(function_exists("imagecreatetruecolor")) { $dst_im = imagecreatetruecolor($re_size[0], $re_size[1]); $tc = imagecolorsforindex($src_im, $transparent); imagefill ($dst_im, 0, 0, imagecolorallocate ($dst_im, $tc["red"], $tc["green"], $tc["blue"])); imagetruecolortopalette ($dst_im, false, $colorstotal); imagecolortransparent ($dst_im, imagecolorclosest ($dst_im, $tc["red"], $tc["green"], $tc["blue"])); imagecopyresampled ($dst_im, $src_im, 0, 0, 0, 0, $re_size[0], $re_size[1], $size[0], $size[1]); } else { $dst_im = imagecreate($re_size[0], $re_size[1]); if (0 <= $transparent && $transparent < $colorstotal) { imagepalettecopy ($dst_im, $src_im); imagefill ($dst_im, 0, 0, $transparent); imagecolortransparent ($dst_im, $transparent); } imageresize($dst_im, $src_im, 0, 0, 0, 0, $re_size[0], $re_size[1], $size[0], $size[1]); } if(function_exists("imagegif")) { header("content-Type: image/gif"); imagegif($dst_im); imagedestroy($src_im); imagedestroy($dst_im); } else { header("content-Type: image/png"); imagepng($dst_im); imagedestroy($src_im); imagedestroy($dst_im); } } else { // サムネイル作成不可の場合(旧バージョン対策) $dst_im = imageCreate($re_size[0], $re_size[1]); imageColorAllocate($dst_im, 255, 255, 214); //背景色 // 枠線と文字色の設定 $black = imageColorAllocate($dst_im, 0, 0, 0); $red = imageColorAllocate($dst_im, 255, 0, 0); imagestring($dst_im, 5, 10, 10, "GIF $size[0]x$size[1]", $red); imageRectangle ($dst_im, 0, 0, ($re_size[0]-1), ($re_size[1]-1), $black); header("content-Type: image/png"); imagepng($dst_im); imagedestroy($src_im); imagedestroy($dst_im); } case "2": // jpg形式 $src_im = imageCreateFromJpeg($path); $dst_im = $imagecreate($re_size[0], $re_size[1]); $imageresize( $dst_im, $src_im, 0, 0, 0, 0, $re_size[0], $re_size[1], $size[0], $size[1]); header("content-Type: image/jpeg"); imageJpeg($dst_im); imagedestroy($src_im); imagedestroy($dst_im); break; // png形式 case "3": $src_im = imagecreatefrompng($path); $colortransparent = imagecolortransparent($src_im); if ($colortransparent > -1) { $dst_im = $imagecreate($re_size[0], $re_size[1]); imagepalettecopy($dst_im, $src_im); imagefill($dst_im, 0, 0, $colortransparent); imagecolortransparent($dst_im, $colortransparent); $imageresize($dst_im,$src_im, 0, 0, 0, 0, $re_size[0], $re_size[1], $size[0], $size[1]); } else { $dst_im = $imagecreate($re_size[0], $re_size[1]); $imageresize($dst_im,$src_im, 0, 0, 0, 0, $re_size[0], $re_size[1], $size[0], $size[1]); imagetruecolortopalette($dst_im, false, imagecolorstotal($src_im)); } header("content-Type: image/png"); imagepng($dst_im); imagedestroy($src_im); imagedestroy($dst_im); break; default: $this->error = "画像の形式が不正です"; return false; break; endswitch; } /** * $maxsize を長辺の長さとした場合のサイズを戻す * * @param string $path 画像のパス * @param int $maxwidth 横の長さ * @param int $maxheight 縦の長さ * @return mixed array(横幅, 縦幅, リサイズ要フラグ) */ function _getThumbSize($path, $maxwidth, $maxheight) { // $maxsizeの値チェック if($maxwidth == 0 && $maxheight == 0) { return false; } // 画像の存在チェック if(!file_exists($path)) { return false; } // 画像サイズ取得 $size = getimagesize($path); $wratio = 1; $hratio = 1; // 縮小比率計算 if($maxwidth > 0) { $wratio = $maxwidth / $size[0]; } if($maxheight > 0) { $hratio = $maxheight / $size[1]; } // 拡大になる場合は原寸 if($wratio > 1 && $hratio > 1) { return false; } // 縮小率が高い方に合わせる if($wratio > $hratio) { $height = $maxheight; $width = round($size[0] * $hratio, 0); } else { $width = $maxwidth; $height = round($size[1] * $wratio, 0); } return array($width, $height, 1); } /** * エラーメッセージを戻す * * @return string */ function getErrorResult() { return $this->error; } } ?>