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

GD グラフィック ライブラリ

画像の生成

//ヘッダーでimg画像であることを宣言
header("Content-Type: image/png");
//新規イメージオブジェクトの生成
$img = imagecreate(200, 200);
//カラー情報を付与(対象、R、G、B)
imagecolorallocate($img, 255, 0, 0);
//書き出し
imagepng($img);

※imagecreateで扱えるのは256色まで、TrueColorイメージを扱いたい場合はimagecreatetruecolorを使う。

外部から画像の読み込み

//ヘッダーでimg画像であることを宣言
header("Content-Type: image/png");
//外部から画像の読み込み
$img = imagecreatefrompng('sample02.png');
//書き出し
imagepng($img);
参考にしたサイト
http://www.geekpage.jp/web/php-gd/imagefilledrectangle.php
http://phpspot.net/php/man/php/function.imagecolorallocate.html
http://ponk.jp/php/gd/kihon
http://www43.atpages.jp/opicon69/libgd/