This is the first of a scheduled three part tutorial on the GD Library and the functions contained within.
The GD library is a monster by itself. It's used in such things as
PokePlushies (which I know several people on this forum use). Now, the methods Used from the GD library are different then the ones used by PokePlushies, but the result is the same, a dynamic image that can change on the fly.
now, php.net has it's own section of the
GD Library with all of the functions available. I've never used all of them, but in part 1 of this tutorial, I'll go over some of the more common functions and what they do.
Image Creation
There are a few functions in the GD Library that allow you to create an image. A few of them do the same thing, just with different names.
imagecreate: imagecreate makes a blank image of a given size (measured in pixels). This image is usually turned into a variable, and is called several times throughout the process of making the image.
php
$img = imagecreate(250,500);
//this will create a rectangle that is 250 pixels wide, and 500 pixels tall
imagecreatetruecolor: imagecreatetruecolor is similar to imagecreate, however the image is black instead of blank.
php
$img = imagecreatetruecolor(250,500);
//this will create a rectangle that is 250 pixels wide, and 500 pixels
imagecreatefrom: imagecreatefrom allows you to make an image from a pre-existing image. It is called by adding the file type to the end of it (IE: imagecreatefromjpeg). Valid File Types supported are:
- gd2
- gd2part
- gd
- gif
- jpeg (also jpg)
- string
- wbmp
- xbm
- xpm
php
$img = imagecreatefromjpeg(images/horse.jpg);
//this will create an image from the horse.jpg image
Colors
Spicing up your image
The GD library is very dynamic when it comes to colors. All colors in RGB and Hex format are allowed, though vary depending on the functions used.
imagecolorallocate: imagecolorallocate binds a color to the specific image, which can then be used for things such as string colors, Etc. Colors provided by imagecolorallocate are always in RGB format.
php
$white = imagecolorallocate($img, 255, 255, 255);
//this will set the variable $white to the color white
imagecolorallocatealpha: imagecolorallocatealpha behaves exactly the same as imagecolorallocate, with the addition of one feature. the fifth element of the function is Alpha, which allows you to add opacity to the color. 0 is completely opaque, and 127 is completely clear.
php
$black_trans = imagecolorallocatealpha($img, 0, 0, 0, 63);
//this will set the variable $black to a 50% opacity black
Strings
Writing text to an image
Strings can easily be added to images through one of several functions. Some use a preset text, while others allow you to choose which type of text you want to use. Because of the way text can be done, and the amount, I will only provide a few of the more common ones.
imagefttext: This function creates text on an image using FreeType 2 fonts. Text can be angled up to 359 degrees, starting at 0 (which is left to right).
php
imagefttext($img, 12, 0, 25, 25, $white, "fonts/anything.ftt", "Hello World!");
//this will add the font contained in "fonts/anything.ftt" to the image at 25
//pixels to the right, 25 pixels from the top, color white, size 12,
//and it will say "Hello world!" and be angled at 0 degrees
imagettftext: Creates text on the image using TrueType fonts. Text can be angled up to 359 degrees, starting at 0 (which is left to right).
php
imagettftext($img, 12, 0, 25, 25, $white, "fonts/anything.ttf", "Hello World!");
//this will add the font contained in "fonts/anything.ttf" to the image at 25
//pixels to the right, 25 pixels from the top, color white, size 12,
//and it will say "Hello world!" and be angled at 0 degrees
imagestring: This function creates text on the image, with no need to store fonts on your server. While it is easier to use, it does not allow you to angle your text at all, whereas the previous 2 do.
php
imagestring($img, 1, 25, 25, "Hello world!", $white);
//this will produce text on the image at 25 pixels from the right,
//25 pixels from the top, font 1 (there are up to 5 different fonts
//available), and it will be white
imagestringup: This function creates text on an image that is displayed vertically. It also does not need you to store fonts on the server, and you can not angle your text.
php
imagestringup($img, 1, 25, 25, "Hello world!", $white);
//this will produce text on the image at 25 pixels from the right,
//25 pixels from the top, The text will be vertical,
//font 1 (there are up to 5 different fonts
//available), and it will be white
This brings us to the end of Part 1 of this tutorial. Part 2 will focus on creating shapes.