4.1. Configuring the GD Extension

Incorporating images into Web pages that you create dynamically with PHP is pretty straightforward — simply send the following line of HTML to the browser:

<img src="my_image.gif">

But what if you need to incorporate images that change based on the content of the page? You could create dozens of separate image files and then include them based on the page content. But that solution relies on you being able to anticipate every image you'll need. It's also a good way to fill up your file system with virtually identical files.

Luckily, there's a better way. The GD extension allows you to create and modify images on the fly from within PHP. What's so great about creating images on the fly? Imagine that you needed to display a pie chart graphic that showed users the exact percentages of fiction and non-fiction books in your library database. Without GD, you would have to manually create a different pie chart image for every possibility — it would take you all day to create those images, chew up a lot of storage space, and probably give you a headache too. Or you could send your percentages (along with some other information) to GD and let it create pie charts on the fly.

Image manipulation is very resource intensive, which could slow your application down to a crawl or even crash the entire Web server. Be sure you set memory_limit sufficiently high ...

Get PHP & MySQL® Web Development All-in-One Desk Reference for Dummies® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.