Skip to content

Getting started with GD Library

Often we need to create a chart or add watermark to image, we could do it manually and create those images by ourselves using Photoshop for example, but it would be time consuming and would cost a lot of money (to buy graphics software mainly). Fortunately PHP has got nice library for creating and editing images on the fly, it is called GD library.

This library is not installed by default with PHP, but almost always you can find it in PHP package, so if you have PHP installed then probably everything you need to install GD library is already on your computer, however you may still need to configure GD.

The best way to find out if your GD library is ready to use is to create simple script like:

If you will receive Fatal Error while running this script it means your GD library is not active and you need to install it or configure server first, it is very simple, and installation process is well covered in PHP manual so I won’t go into details here.

I assume you have your GD library up and running so we can write first script.

We will start with small image containing text only, this will be simple script, but it should give you an idea how to handle GD library.

 

We start with setting content type to image/png, we have to do it because using GD library we cannot throw our image into html code. If you have script ready then save it as img.php and open in browser, you should see black image with white string on it, but try to view source code, there is no html, right? This is because our img.php file is no longer text file for our server it is PNG image.

Let’s play with it further, after “?>” add to img.php “I like HTML”, save file and reload it in browser. What do you see now? A lot of signs and letters which do not make any sense? If yes then you did this exercise correctly. By adding text we corrupted our file structure, the same effect can be achieved if you open with Notepad any image on your computer add some text to it, save and close image. If you will try to open this file with some image viewer you will get error saying that there is something wrong with your file. Of course you know that, because you just edited this file by yourself.

However, what should concern you now is a fact that img.php is just image file and we can’t really do anything with it, besides displaying our image, therefore if you wish to display our image on your website with html code around it then you need to create new .html (or .php) file and add there this similar line, if your new file is in the same directory as img.php.

<img src="img.php" mce_src="img.php">

Now let me explain our code briefly, imagecreate(…) is a function which creates new blank image, two parameters given are image width and height of course. In the next line you can find imagecolorallocate(…) function which assigns to variable color in RGB format, BUT always first use of this function after imagecreate(…) fills background with selected color. So after first two lines we have 200 px width and 50px height black image.

In the next line we assign value which represents white color to variable $white, then by using imagestring() function we add text to our image. First parameter is a reference to image which will contain this string, second parameter is a size of build-in font (from 1 to 5), third and fourth parameters are x and y coordinates, we need to know where our string starts and this numbers define it. Fifth parameter is string which we want to put in the image and finally last parameter is color of our string, defined earlier with imagecolorallocate(…).

Imagepng(…) sends created image to output as a PNG image, analogically if we want to send our file as GIF then we must use imagegif(…), if we need JPEG then we must use imagejpeg(…). Of course you need to remember to change header if you change file format.

If we would like to save our image to file as PNG image then in our example we should redirect output stream to file, in other words instead of imagepng($img); we should write imagepng($img, “our_first.png”);.

That’s it for now. If you are new to GD library then I am confident this article will get you started and you will soon want to learn how to draw objects with GD, so for more functions visit www.php.net and find “Image Functions” category.

Published inGeneral