PHPは、注)GDライブラリを通して以下の画像操作を実行することができます。
CGIや各種プログラムから画像の作成・編集を行うためのライブラリです。PHP4.3.0からは標準で実装されています。
PHPでは、簡単な図形から複雑な画像までテキストベースで描画する機能が備わっています。外部の画像ファイルを直接読み込んで、Webブラウザに出力することも可能です。また、様々なテキストを読み込んで、画像に埋め込むこともできます。
インターネットで画像を扱う際に使われる画像形式には、GIF、JPEG、PNGなど様々あります。もともと、PHPの画像操作で使うことができる形式は GIFのみでしたが、現在ではGIFに関する機能が削除され、PNGとJPEGフォーマットをサポートするように変更されています。
imageline()関数を使用して、指定された2点を結ぶ直線を引くことができます。
<?php
header( "Content-Type: image/png" );
$image = imagecreate( 120, 120 );
$green = imagecolorallocate( $image, 131, 195, 28 );
$white = imagecolorallocate( $image, 255, 255, 255 );
imageline( $image, 10, 60, 110, 60, $white );
imagepng( $image );
imagedestroy( $image );
?>

指定した大きさの空の画像を表す画像IDを返すimagecreate()関数を使用して、背景となる正方形の画像を生成します。
imagecreate( int width, int height );
imagecolorallocate()関数を使用して、利用する色を指定します。
imagecolorallocate( resource image, red, green, blue );
imageline()関数を使用して、描画した画像の上に直線を書きます。
imageline( image, 始点x座標, 始点y座標, 終点x座標, 終点y座標, color );
imagepng()関数を使用して、PNGイメージをブラウザまたはファイルに出力します。
imagepng( resource image [, string filename ] );
imagedestroy()関数を使用して、PHPで表現された画像をメモリ上から解放します。
imagedestroy( resource image [, string filename ] );
図形を描くための様々な関数を組み合わせて、基本的な図形から複雑なものまで描くことができます。
下記のスクリプトとも、imagecreate()関数を使用して、背景となる正方形の画像を生成し、その上にimagearc()関数やimagerectangle()関数を使って、楕円形と長方形を描画しています。
<?php
header( "Content-Type: image/png" );
$image = imagecreate( 120, 120 );
$green = imagecolorallocate( $image, 131, 195, 28 );
$white = imagecolorallocate( $image, 255, 255, 255 );
imagearc( $image, 60, 60, 100, 60, 0, 360, $white );
imagepng( $image );
imagedestroy( $image );
?>
imagearc( image, 中心x座標, 中心y座標, int width,
int height, 開始角度, 終了角度, color );

<?php
header( "Content-Type: image/png" );
$image = imagecreate( 120, 120 );
$green = imagecolorallocate( $image, 131, 195, 28 );
$white = imagecolorallocate( $image, 255, 255, 255 );
imagerectangle( $image, 20, 40, 100, 80, $white );
imagepng( $image );
imagedestroy( $image );
?>
imagerectangle( image, 左上x座標, 左上y座標, 右下x座標, 右下y座標, color );
