GDでサムネイルを作る(画像の拡大と縮小)

画像の拡大と縮小


//ヘッダーでimg画像であることを宣言
header("Content-Type: image/png");
//外部から画像の読み込み
$img = imagecreatefrompng('sample03.png');
//読み込んだ画像の幅と高さを取得する
$width = imagesx($img);
$height = imagesy($img);
//新規イメージオブジェクトの作成
$out = imagecreatetruecolor($width/2, $height/2);
//リサンプリング処理。コピー元、コピー先、座標、コピー先の幅、コピー先の高さ、コピー元の幅、コピー元の高さ
imagecopyresampled($out, $img,0,0,0,0, $width/2, $height/2, $width, $height);
//書き出し
imagepng($out);

※imagecreatetruecolorはimagecreate の強化版

参考にしたサイト
http://www.geekpage.jp/web/php-gd/resize1.php
http://phpspot.net/php/man/php/function.imagecopyresampled.html