$dist_width) { $dist_left = ($min_w - $dist_width) / 2; $dist_width = $min_w; $resize_flag = true; } if (!is_null($min_h) && $min_h > $dist_height) { $dist_top = ($min_h - $dist_height) / 2; $dist_height = $min_h; $resize_flag = true; } $resize_dist = array($dist_left, $dist_top, $dist_width, $dist_height); return $resize_flag; } function parse_filename($name) { $p = strrpos($name, '.'); if ($p === false) { return null; } return array(substr($name, 0, $p), substr($name, $p + 1)); } function imagexxx($file_ext, $img_bin, $file_path = null) { switch (strtolower($file_ext)) { case 'gif': if (!is_null($file_path)) { imagegif($img_bin, $file_path); } else { imagegif($img_bin); } break; case 'jpg': case 'jpeg': if (!is_null($file_path)) { imagejpeg($img_bin, $file_path, 100); } else { imagejpeg($img_bin, null, 100); } break; case 'png': if (!is_null($file_path)) { imagepng($img_bin, $file_path, 0); } else { imagepng($img_bin, null, 0); } break; } } if (!extension_loaded('gd')) { header("Content-Type: text/html;charset=utf-8;"); die("GDがインストールされていません"); } $filename = isset($_GET['file']) ? $_GET['file'] : null; $max_w = isset($_GET['maxwidth']) ? $_GET['maxwidth'] : null; $max_h = isset($_GET['maxheight']) ? $_GET['maxheight'] : null; $caching = isset($_GET['cache']) ? ($_GET['cache'] == '1') : false; if ($filename === null || empty($filename)) { header("Content-Type: text/html;charset=utf-8;"); die("パラメータ file が指定されていません"); } if (preg_match('/_\d*x\d*\.\w+$/', $filename)) { header("Content-Type: text/html;charset=utf-8;"); die("パラメータ file が無効です"); } list($base_name, $file_ext) = parse_filename($filename); if (!in_array(strtolower($file_ext), array('gif', 'jpeg', 'jpg', 'png'))) { header("Content-Type: text/html;charset=utf-8;"); die("gif jpg png のみ対応しています"); } // パス乗り越え対策 if (strpos($filename, "..") !== false || strpos($filename, "/") !== false || strpos($filename, "\\") !== false) { header("Content-Type: text/html;charset=utf-8;"); die("ファイルが存在しません"); } if (!is_numeric($max_w)) { $max_w = null; } if (!is_numeric($max_h)) { $max_h = null; } // オリジナルのファイル名 $src_path = realpath($IAMGE_DIR) .'/'. $filename; // キャッシュを検索する if ($caching) { if (!is_dir($CACHE_DIR)) { if (!@mkdir($CACHE_DIR)) { header("Content-Type: text/html;charset=utf-8;"); die("キャッシュディレクトリを作成できません"); } } $cache_path = sprintf("%s/%s_%sx%s.%s", realpath($CACHE_DIR), $base_name, $max_w, $max_h, $file_ext); if (file_exists($cache_path)) { header_wrap($file_ext); readfile($cache_path); exit; } } // 元画像の保存先を取得する if (!file_exists($src_path)) { header("Content-Type: text/html;charset=utf-8;"); die("元画像が見つかりません : ".$filename); } // 画像のサイズを取得する // 1 = GIF、2 = JPG、3 = PNG、4 = SWF list ($src_width, $src_height, $src_type, $org_attr) = getimagesize($src_path); // リサイズの計算 $resize_flag = resize_to($src_width, $src_height, $max_w, $max_h, null, null, $resize_dist); if ($resize_flag) { $imagecreatefromxxx = array( null, 'imagecreatefromgif', 'imagecreatefromjpeg', 'imagecreatefrompng', null ); // Read image. $src_img = $imagecreatefromxxx[$src_type]($src_path); list ($dist_left, $dist_top, $dist_width, $dist_height) = $resize_dist; $dst_img = imagecreatetruecolor($dist_width, $dist_height);//先画像 $color_while = imagecolorallocate($dst_img, 255, 255, 255); imagefill($dst_img, 0, 0, $color_while); imagecopyresampled($dst_img, $src_img, $dist_left, $dist_top, 0, 0, ($dist_width-($dist_left*2)), ($dist_height-($dist_top*2)), $src_width, $src_height); imagedestroy($src_img); if ($caching) { imagexxx($file_ext, $dst_img, $cache_path); } header_wrap($file_ext); imagexxx($file_ext, $dst_img); imagedestroy($dst_img); } else { if ($caching) { copy($src_path, $cache_path); header_wrap($file_ext); readfile($cache_path); } else { header_wrap($file_ext); readfile($src_path); } } ?>