PHP GD, what’s that?
PHP can’t make only HTML output, but it can also make images. There are a lot of functions to make or edit images. Around 100 functions, all the functions you can see here: (Log in om link te zien!) . I will tell you something about PHP GD and some important functions.
The first step
The first thing to do is make sure that your host supports the GD extension. You can ask it the person who manages the server. Or you can make a php document with the following code:
<?php
var_dump(gd_info());
?>
If the page is empty or it gives an error. Your host doesn’t support GD. But if there is some info, your host supports it. The following information gives this function:
GD version
Freetype Support - true/false
Freetype Linkage
T1Lib Support - true/false
GIF Read Support - true/false
GIF Create Support - true/false
JPG Support - true/false
PNG Support - true/false
WBMP Support - true/false
XBM Support - true/false
If you don’t have the GD library, you can ask to the person who manages the server or you can install yourself. To install the library you need to do this: Download it from here: (Log in om link te zien!) . Search to php_gd2.dll and copy that file to the map where you have installed PHP. Edit your php.ini, you need to delete the ; before the GD extension. And the last thing to do is restart your server. Well done, now you can make/edit images!
Making your first empty image
Now we go make an empty image. You can make it with 2 different functions. Imagecreatetruecolor() and Imagecreate(). Imagecreatetruecolor is a better function, because it gives a better picture. The syntax is by both functions the same, namely:
resource imagecreate ( int $width , int $height )
Normally I first choose what kind of format I will have my image. For this example I will choose for *.gif. To see your picture in your browser, you need a header. And because I have chosen for *.gif. I need this header, if you have chosen for another one. Edit gif to your extension.
header('Content-type: image/gif');
If you want to know more about headers, you can read all about it here: (Log in om link te zien!) Now you want to display the image, you can do that with the function imagegif(). Or if you have another extension, edit gif again to it. And I will use imagedestroy() to clear the computer memory, because there was loaded the image. This is what I have, an empty image.
Colors, Lines and text
Now we can give a color to the image. With this function you can use all your colors. So you will need this also for the color of the line or the text. The syntax of the function imagecolorallocate is this:
bool imagecolorallocate ( resource $image , int $red , int $green , int $blue )
$red, $green and $blue are colors in RGB. So if you want the color to black. You need to set $red, $green and $blue to 0. And for white, you need to set it to 255. An important notice is that the first time that you call to imagecolorallocate(), that you will set the background color. You also can use hexadecimal instead of RGB. So this will be white: 0xFF, and this black: 0x00. If you want some lines in your image, you can use the function imageline(). The syntax is this:
bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
If your image is 100px x 100px and $x1 is 50. The line will start horizontally in the middle. If you set $y1 to 10. The line will start vertically on 10px from the top. It is comparable with a coordinate system. A little example: I have an image, 100px x 100px. And I will do this:
imageline ( $image, 0, 0 , 100, 100, $color );
So this line is diagonal, from the left top to the right bottom of the image. Now it’s time for some text in your image. You can do that with the function imagestring() or imagestringup(). This is the syntax of this functions are:
bool imagestring ( resource $image , int $font , int $x , int $y , string $string , int $color )
bool imagestringup ( resource $image , int $font , int $x , int $y , string $string , int $color )
Imagestring() is for horizontal text and imagestringup() is for vertical text.
$font = You can use 1, 2, 3, 4 or 5. These are built-in letter types. But if you want your own letter
type. You can use the function imagettftext() instead of imagestring().
$x = x-coordinate of the upper left corner
$y = y-coordinate of the upper left corner
$string = Your text.
The syntax of imagettftext is this:
array imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string
$font , string $text )
$size = The size of your text
$angle = The angle in degrees, 0 means left to right reading.
$font = Your *.ttf font file.
Objects
In this part of this article I will tell you only how you can make an object and the syntax. With PHP GD you also can make objects in your image. Like an ellipse, polygon or rectangle. And if you are a little bit good in it. You also can make 3d of these objects.
Ellipse, for an ellipse you need to use the function imagefilledellipse() or imageellipse(). With the first one you can make ellipse that is filled up with a color. And with the second one you can only make a border/line. The syntax is the same for both functions
bool imageellipse ( resource $image , int $cx , int $cy , int $width , int $height , int $color )
$cx = The x-coordinate of the center of the ellipse
$cy = The y-coordinate of the center of the ellipse
$width = The width of the ellipse
$height = The height of the ellipse
Example: (Log in om link te zien!)
Polygon, for this one you need the function imagefilledpolygon() or imagepolygon. Also for this one, the first one will fill the image and the second one will draw a border/line. The syntax is this:
bool imagepolygon ( resource $image , array $points , int $num_points , int $color )
$points = This is an array and this array will looks like this:
array (
x0, y0, // Begin
x1, y1, // First point
x2, y2 // Second point
);
$num_points = Total number of points
Example: (Log in om link te zien!)
Rectangle, you can make a rectangle with the function imagefilledrectangle() or imagerectangle(). And also for this one, it has the same things as ellipse and polygon. The syntax for imagerectangle:
bool imagerectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
$x1 = Upper left x coordinate
$y1 = Upper left y coordinate
$x2 = Bottom right x coordinate
$y2 = Bottom right y coordinate
Example: (Log in om link te zien!)
Other functions to know
Getimagesize() (Log in om link te zien!)
Imagecopy() (Log in om link te zien!)
Imagesx() (Log in om link te zien!)
Imagesy() (Log in om link te zien!)
Imagerotate() (Log in om link te zien!)
Imagefilltoborder() (Log in om link te zien!)
Nice, but what else can I do with PHP GD?
Well, you can make captcha’s with this and beautiful graphics and stuff like that. You can post your creation here in a comment.