> // $org_img オリジナル画像のリソースID // $org_ext オリジナル画像の拡張子 // $mode リサンプリング方法 // $size1 サイズ指定1 // $size2 サイズ指定2 // // <> // 画像リソースID // // 指定された画像リソースIDを元に $mode で指定した方法によってリサンプリング // を実行し、変換後のリソースIDを返します。 // 返されるリソースIDは、処理内で使い終わったらimagedestroyしてください。 // image系の関数が動く環境が必須です。 // $mode によるリサンプリングの方法は次の通りです。 // // 'fix' // $size1 = 画像の幅, $size2 = 画像の高さ // $size1, $size2で指定された大きさに強制的にリサンプリングを行います。 // // 'widthfix' // $size1 = 画像の幅 // $size1を横幅とし、元画像の縦横サイズ比率によって自動的に調整したサイズ // にリサンプリングします。 // // 'heightfix' // $size1 = 画像の高さ // $size1を高さとし、元画像の縦横サイズ比率によって自動的に調整したサイズ // にリサンプリングします。 // // 'square' // $size1 = 正方形のサイズ // $size1を一辺とした正方形の中に画像がフィットするようにリサンプリング // 行います。出力される画像は$size1を一辺とした正方形になります。 // 画像の縦横サイズ比率による正方形内の余白は白く塗りつぶされます。 // // 'squarefit' // $size1 = 正方形のサイズ // $size1を一辺とした正方形の中に画像がフィットするようにリサンプリング // 行います。出力される画像はのサイズは $size1を越えることなく、元画像 // の縦横サイズ比率によって自動的に調整されます。 // // 'squarecut' // $size1 = 正方形のサイズ // $size1を一辺とした正方形によって画像がちょうど切り取れるようにリサン // プリング行います。出力される画像は$size1を一辺とした正方形になります。 // 元画像の短辺を $size1とし、縦横サイズ比率によって自動的に調整された // 画像の中央部分を $size1の正方形で切り取ります。 // function generate_resampleimage($org_img, $org_ext, $mode, $size1, $size2=0) { // ベース画像の情報を取得 $org_width = imageSX($org_img); $org_height = imageSY($org_img); // サイズ計算 $arycalc = calc_imagesize($org_width, $org_height, $mode, $size1, $size2); $new_width = $arycalc['width']; $new_height = $arycalc['height']; $org_x = $arycalc['org_x']; $org_y = $arycalc['org_y']; $new_x = $arycalc['new_x']; $new_y = $arycalc['new_y']; // リサンプリング画像の生成 $new_img = NULL; if ($org_ext == 'gif') { $new_img = imagecreate($new_width, $new_height); } else { $new_img = imagecreatetruecolor($new_width, $new_height); } $backcolor = imagecolorallocate($new_img, 255, 255, 255); imagefill($new_img, 0, 0, $backcolor); $ret = imagecopyresampled ($new_img, $org_img, $new_x, $new_y, $org_x, $org_y, $new_width-$new_x*2, $new_height-$new_y*2, $org_width-$org_x*2, $org_height-$org_y*2); return $new_img; } //-------------------------------------------------------------------------- // calc_imagesize // リサンプリング方法に応じた変換後のサイズやリサンプリング位置を計算する。 // function calc_imagesize($width, $height, $mode, $size1, $size2=0) { $ary = array( 'width' => 0, 'height' => 0, 'org_x' => 0, 'org_y' => 0, 'new_x' => 0, 'new_y' => 0 ); // 固定サイズ if ($mode == 'fix') { $ary['width'] = $size1; $ary['height'] = $size2; } // 矩形 else if ($mode == 'square') { $ary['width'] = $size1; $ary['height'] = $size1; $xyrate = $width / $height; if ($xyrate >= 1) { $tmp_width = $size1; $tmp_height = round($size1 / $xyrate); } else { $tmp_width = round($size1 * $xyrate); $tmp_height = $size1; } // 位置 $ary['new_x'] = round(($ary['width'] - $tmp_width) / 2); $ary['new_y'] = round(($ary['height'] - $tmp_height) / 2); } // 矩形内フィット else if ($mode == 'squarefit') { $xyrate = $width / $height; if ($xyrate >= 1) { $ary['width'] = $size1; $ary['height'] = round($size1 / $xyrate); } else { $ary['width'] = round($size1 * $xyrate); $ary['height'] = $size1; } } // 矩形カット else if ($mode == 'squarecut') { $ary['width'] = $size1; $ary['height'] = $size1; $xyrate = $width / $height; if ($xyrate >= 1) { $tmp_width = $height; $tmp_height = $height; } else { $tmp_width = $width; $tmp_height = $width; } // 位置 $ary['org_x'] = round(($width - $tmp_width) / 2); $ary['org_y'] = round(($height - $tmp_height) / 2); } // 幅固定(高さ自動) else if ($mode == 'widthfix') { $xyrate = $width / $height; $ary['width'] = $size1; $ary['height'] = round($size1 / $xyrate); } // 高さ固定(幅自動) else if ($mode == 'heightfix') { $xyrate = $width / $height; $ary['width'] = round($size1 * $xyrate); $ary['height'] = $size1; } return $ary; } ?>