首先把下列繪圖程式寫到 gdraw.php 檔中, 再使用 gdraw.html檔來引用圖片 (<img src="gdraw.php" style="border:1px #000 solid;" >)

<?php

$img = imagecreatetruecolor(300, 300);		// create a 300*300 image

$white = imagecolorallocate($img, 255, 255, 255);	// allocate color
$black = imagecolorallocate($img, 0, 0, 0);
$red   = imagecolorallocate($img, 255, 0, 0);	 $green = imagecolorallocate($img, 0, 255, 0);
$blue  = imagecolorallocate($img, 0, 0, 255);	 $yellow= imagecolorallocate($img, 255, 255, 0);
$GnBl  = imagecolorallocate($img, 0, 255, 255); $pink  = imagecolorallocate($img, 255, 0, 255);

imagefill($img, 0, 0, $white);			// image background

// 畫直線(底圖, X1座標, Y1座標, X2座標, Y2座標, 顏色);  //座標 採 正整數第4象限
imageline($img, 0, 0, 290, 290, $black);

// 畫矩形(底圖, X1座標, Y1座標, X2座標, Y2座標, 顏色);
imagerectangle($img, 10, 10, 50, 70, $yellow);		// 畫空心矩形
imagefilledrectangle($img, 110, 10, 150, 70, $GnBl);	// 畫實心矩形

$points1 = array(10,200, 40,190, 50,240, 35,290, 5,230);	// 點座標矩陣
$points2 = array(210,200, 240,190, 250,240, 235,290, 205,230);
// 畫多邊形(底圖, 點座標矩陣, 點數, 顏色);
imagepolygon($img, $points1, 5, $green);		// 畫空心多邊形
imagefilledpolygon($img, $points2, 5, $pink);		// 畫實心多邊形

// 畫圓(底圖, X座標, Y座標, width(寬), height(高),  起始度數, 結束度數, 空心圓顏色);
imagearc($img, 200, 200, 20, 30,  0, 360, $green);  		    // 畫空心圓
imagefilledarc($img, 100, 100, 10, 10,  0, 360, $red, IMG_ARC_PIE); // 畫實心圓

// 畫文字(底圖, 大小(1~5), X座標, Y座標, 文字, 顏色); 注意:不支援中文字型
$st1="Hello!";
imagestring(  $img, 5, 200,  50, $st1, $red);	// 畫橫向文字
imagestringup($img, 3, 200, 250, $st1, $blue);	// 畫直向文字

// 畫True Type文字(底圖, 大小, 角度, X座標, Y座標, 顏色, 字型, 文字); 注意:支援中文字型
// This function requires both the GD library and the FreeType library.
// $st2="你好嗎?"; $font="simhei.ttf";
// imagettftext($img, 16, 0, 70, 150, $black, $font, $st2);

// output image in the browser
header("Content-type: image/png");
imagepng($img);

// free memory
imagedestroy($img);
?>