PHPマニュアル

gd_info()

array(14) { ["GD Version"]=> string(26) "bundled (2.1.0 compatible)" ["FreeType Support"]=> bool(true) ["FreeType Linkage"]=> string(13) "with freetype" ["GIF Read Support"]=> bool(true) ["GIF Create Support"]=> bool(true) ["JPEG Support"]=> bool(true) ["PNG Support"]=> bool(true) ["WBMP Support"]=> bool(true) ["XPM Support"]=> bool(false) ["XBM Support"]=> bool(true) ["WebP Support"]=> bool(true) ["BMP Support"]=> bool(true) ["TGA Read Support"]=> bool(true) ["JIS-mapped Japanese Font Support"]=> bool(false) }

サンプル


imori.png

基本

画像ファイルを取り込む

	$img = imagecreatefrompng("imori.png");
	var_dump($img);
	imagedestroy($img); // 破棄
resource(2) of type (gd)

取り込んだ画像からサイズを取得する

	$img = imagecreatefrompng("imori.png");
	$sx = imagesx($img);
	$sy = imagesy($img);
	imagedestroy($img);
	
int(320) int(180)

別名で保存する

imagepngでimageオブジェクトを画像ファイルとして保存する。
	$img = imagecreatefrompng("imori.png");
	
	// 楕円を描画
	$col_ellipse = imagecolorallocate($img, 255, 255, 255);
	imageellipse($img, 200, 150, 300, 200, $col_ellipse);
	
	// 別名で保存
	imagepng( $img, "imori2.png");
	imagedestroy($img);


空画像を作成してファイル保存する

	// 空の画像を作成する
	$img = imagecreatetruecolor(200, 160);
	
	// 楕円の色を選択しますRGB
	$col = imagecolorallocate($img, 44,168,108);
	imageellipse($img, 50, 50, 60, 60, $col);
	
	// 画像を出力します
	imagepng( $img, "test1.png");
	
	imagedestroy($img);



画像の大きさを取得する

	$ret = getimagesize('imori.png');
	var_dump($ret);
array(6) { [0]=> int(320) [1]=> int(180) [2]=> int(3) [3]=> string(24) "width="320" height="180"" ["bits"]=> int(8) ["mime"]=> string(9) "image/png" }

画像の文字列データから画像の大きさを取得する

	$data = file_get_contents('imori.png');// 文字列データとして取得
	$ret = getimagesizefromstring($data); // 文字列データから情報を取得する
	var_dump($ret);
array(6) { [0]=> int(320) [1]=> int(180) [2]=> int(3) [3]=> string(24) "width="320" height="180"" ["bits"]=> int(8) ["mime"]=> string(9) "image/png" }