PHP Image Drawing Tutorial: Canvas Creation, Shapes, Text, and Captcha Generation
This tutorial explains how to use PHP's GD library to create a canvas, draw basic shapes such as ellipses, lines, rectangles, arcs, and filled arcs, output images in common formats, add text with TrueType fonts, generate captchas, and properly release memory, providing complete example code.
PHP can generate images on the fly using the GD library, which is useful for reports, graphics, and captchas. The coordinate system starts at the top‑left corner with pixel units, where the x‑axis runs horizontally and the y‑axis runs vertically.
The basic workflow consists of four steps:
Create a canvas (image resource) with $im = imagecreatetruecolor(400, 300); and set a background color, e.g., white using
$white = imagecolorallocate($im, 255, 255, 255); imagefill($im, 0, 0, $white);Allocate colors, for example a red color with $red = imagecolorallocate($im, 255, 0, 0); Draw shapes:
Ellipse (or circle): imageellipse($im, 20, 20, 20, 20, $red); Line: imageline($im, 0, 0, 400, 300, $red); Rectangle: imagerectangle($im, 2, 2, 40, 50, $red); Filled rectangle: imagefilledrectangle($im, 2, 2, 40, 50, $red); Arc: imagearc($im, 100, 100, 50, 50, 0, 30, $red); Filled arc (pie slice):
imagefilledarc($im, 100, 100, 80, 50, 180, 270, $red, IMG_ARC_PIE);Output the image to the browser and free resources:
header("Content-Type: image/png"); imagepng($im); imagedestroy($im);Common web image formats include GIF (256‑color, supports animation), JPG/JPEG (lossy compression, widely used), and PNG (lossless, no animation). The tutorial also shows how to load an external image with $srcImage = imagecreatefrompng("arrow.jpg");, obtain its dimensions via $info = getimagesize("arrow.jpg");, and copy it onto the canvas using imagecopy($im, $srcImage, 0, 0, 100, 0, $info[0], $info[1]);.
For text, imagestring() can draw simple ASCII strings, but to render Chinese or TrueType fonts you should use imagettftext($im, 15, 10, 50, 50, $red, "SIMYOU.TTF", $str);, where the parameters specify font size, angle, position, color, font file, and the string.
A complete example script combines all the steps above, creating a canvas, drawing shapes, adding text, copying a source image, and finally sending the PNG to the client.
The article also provides a 3‑D pie‑chart example that draws multiple filled arcs with different colors and vertical offsets to simulate depth, followed by the same output and cleanup code.
Finally, a simple captcha generator (checkCode.php) is presented. It creates a random hexadecimal code, stores it in a session, draws the code on a black canvas with random interference lines, and outputs the image. The captcha can be embedded in a login form, and the image refreshes on click.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
