It is very easy to do multiple things required for a website with the help of PHP; one of the best things is to create an image. The capacity to create an image in PHP can be beneficial in case we want to accomplish the things such as create CAPTCHA images. This will even help in designing a banner or logo on the fly much in the similar way that any free blogging software does.
The tutorial will assist you to create an image making use of PHP, other would be setting the colours of its background. Also we would be able to set the text and the lines that are drawn, write text to that image, draw a line and set its thickness. One would also be able to send that image to a web browser as well as save it as a file.
Prerequisites before you begin:
- Basic PHP Knowledge
- Your PHP Must Have Been Compiled with the GD Library
- Free Type Must Be Compiled for TrueType Font Support
It is a must to have some basic knowledge of how to write PHP programs. If not, please go through some basic PHP tutorial and learn how to Program in PHP.
The PHP interpreter must have been complied with the GD library so that any of the functions listed are available.
The PHP must have Free Type support compiled if you want TrueType font support.
An Introduction to Creating an Image utilizing PHP
The sample code listed below displays one of the easiest way to understand how to create an image.
<?php $my_img = imagecreate( 200, 80 ); $background = imagecolorallocate( $my_img, 0, 0, 255 ); $text_colour = imagecolorallocate( $my_img, 255, 255, 0 ); $line_colour = imagecolorallocate( $my_img, 128, 255, 0 ); imagestring( $my_img, 4, 30, 25, "mrbool.com", $text_colour ); imagesetthickness ( $my_img, 5 ); imageline( $my_img, 30, 45, 165, 45, $line_colour ); header( "Content-type: image/png" ); imagepng( $my_img ); imagecolordeallocate( $line_color ); imagecolordeallocate( $text_color ); imagecolordeallocate( $background ); imagedestroy( $my_img ); ?>
An image of 200x80 PNG image will be created with the help of above code with a blue background and yellow text. It can be called from within your web page simply by pointing the php file. Let us understand this by means of an example. Assuming the PHP file comprising the above code is called myimage.php, in that case, the HTML code to invoke it can simply be:
<img class='imagem_artigo' src="myimpage.php" alt="Image created by a PHP script" width="200" height="80">
Description of the Code
Creating the Image
The very first thing done by the code is to call the imagecreate() function with the dimensions of the image which is its width and height in that order. Resource identifier is returned for the image by this function that we save in $my_img. This identifier is required for all our operations on the image.
Assuming the function fails for any reason, the FALSE value will be returned. This should be tested in case one wants the code to be robust.
Using Colours in PHP
Colours are allocated with the help of imagecolorallocate() function that will automatically fill the background of the image with the colour the first time you call it. Also it will return an identifier for that particular colour. Subsequent calls to imagecolorallocate() will create a colour identifier for your colour, with no damage to the image background.
Seeing the above code, it can be noted that the script allocates a blue identifier for the image. Doing this causes imagecolorallocate() to set the background to blue automatically.
Writing Text to the Image
Imagestring() function is used to write text to your image. This function makes use of set of built-in fonts to do the writing. The fonts have several sizes that range from 1 to 5, where 1 is considered as the smallest font size and 5 as the largest. The font size is specified in the second parameter to the function and is used as 4 in the above code.
The third and fourth parameters to imagestring() specify the x,y coordinate for the top left hand corner of the text. In the case of the example above, my text will begin 25 pixels from the top edge of the image, and 30 pixels from the left.
The fifth parameter is for the text to print, and the final parameter the colour of the text. This is the same colour that was allocated earlier usingimagecolorallocate().
Drawing a Line and Setting the Thickness of the Brush
The imageline() function can be made use of to draw a line to the image. In order to set the thickness of the brush that is utilized to draw the line, one may require to call theimagesetthickness() function. The numeric parameter to imagesetthickness() is nothing but the the thickness of the brush. This is in pixels.
The imageline() function is called with the start and end coordinates of the line, in x,y format. In the above code, the line starts from 30,45 and ends on 165,45. This means that it will be a horizontal line 45 pixels from the top, starting 30 pixels from the left edge and ending 165 pixels from that same edge. For the reason that $line_colour was set to a shade of green earlier, the colour of the line will be green.
How to Output the Image
Because the output of my instance script is the image only, an “image/png” content type header is sent to the browser informing it that what follow is the bytes of a PNG image.
The next step is to call the function imagepng() so as to generate the required image from my $my_img image identifer. For the reason that we called imagepng() without a second parameter, output is send automatically by the function to the browser. Don't call the header() function to output the header in case you want to save the image you are creating. In fact you should call imagepng() with the filename of the image for its second parameter, like the one shown below:
imagepng( $my_img, "my_new_image.png" );
It is not required that the image should be a PNG image. One can also make use of imagegif() or imagejpeg() in order to come up with GIF and JPG images. Obviously, it is required to send the correct content type header for the type of image one is trying to create. For illustration, a jpeg image should have a content type of "image/jpeg" while a gif image "image/gif".
Once this is completed, the program releases the resources that are linked with the image by calling imagecolordeallocate() and imagedestroy().
The tutorial explained very briefly on the approach to be followed to create a new image by making use of PHP language. Hope you liked the article!!!