画像の拡大縮小+縁取り


サムネイル画像の作成とその画像の縁取りをしてみました。

<?php //サムネイル画像作成
//画像URL
$url = "http://hogehoge/hoge.jpg";
//ファイル名を決める
$outfile = "./image/" md5( $url ) . ".jpg";
//バナーサムネイルの幅
$w_new = 127;
$h_new = 60;
//画像のサイズや情報を取得
$size = getimagesize( $url );
$w_src = $size[0];
$h_src = $size[1];
//画像タイプによって読み込み形式を変える
if( $size['mime'] == "image/gif" ){
    $im_src     = imagecreatefromgif( $url );
}elseif( $size['mime'] == "image/jpeg" ){
    $im_src     = imagecreatefromjpeg( $url );
}elseif( $size['mime'] == "image/png" ){
    $im_src     = imagecreatefrompng( $url );
}else{
    exit;
}
//縦横比の計算
$hiritu = $h_src * ( $w_new / $w_src );
//画像を作成する。
$im_new = @imagecreatetruecolor ( $w_new, $h_new ) or die ("Cannot Initialize new GD image stream");
imagecolorallocate($im_new, 255,255,255);
//画像の縮小
imagecopyresampled ( $im_new , $im_src , 0 , 0 , 0 , 0 + intval( $h_new / 1.8 ), $w_new , $hiritu , $w_src , $h_src );
//額縁を作成
imagesetthickness($im_new , 10);
$col_poly = imagecolorallocate($im_new, 255, 255, 255);
imagepolygon($im_new,
     array (
    0,      0,
    0,      $h_new,
    $w_new, $h_new,
    $w_new, 0
    ),
     4,
     $col_poly);
//額縁を作成2
imagesetthickness($im_new , 2);
$col_poly = imagecolorallocate($im_new, 255, 35, 195);
imagepolygon($im_new,
     array (
    0,      0,
    0,      $h_new,
    $w_new, $h_new,
    $w_new, 0
    ),
     4,
     $col_poly);
//保存
imagejpeg( $im_new , $outfile );
//出力
header("Content-type: image/jpeg");
imagejpeg( $im_new );
exit;
?>