hi,
In php programming, what is the format of the code for creating images ? And how does the code work ?
If anyone has the knowledge regarding this, then please give the relevant explanation.....thanks
hi,
In php programming, what is the format of the code for creating images ? And how does the code work ?
If anyone has the knowledge regarding this, then please give the relevant explanation.....thanks
PHP makes it very easy to do many things needed on a website, among which is to create an image. The ability to generate an image in PHP can be useful if you want to do things like create CAPTCHA images, or even design a banner or logo on the fly the way some free blogging software do.
Creating Images in php requires
Basic PHP Knowledge,
Your PHP Must Have Been Compiled with the GD Library,
FreeType Must Be Compiled for TrueType Font Support.
The easiest way to understand how to create an image is by looking at a sample code.
The above code creates a 200x80 PNG image with a blue background and yellow text. It can be called from within your web page simply by referencing the php file.Code:<?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, "thesitewizard.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 ); ?>
For example, if the PHP file that contains the above code is called myimage.php, then the HTML code to invoke it can simply be:
<img src="myimpage.php" alt="Image created by a PHP script" width="200" height="80">
Some useful functions for creating images in php :
imagecreate() - This function helps you to set dimensions of the image, namely its width and height in that order.
imagecolorallocate() - This function helps you to collaborate colors. It will automatically fill the background of the image with the colour the first time you call it, as well as return an identifier for that particular colour. Subsequent calls to imagecolorallocate() will simply create a colour identifier for your colour, without affecting your image background.
imagestring() - This function helps you to use a set of built-in fonts to do the writing.
The second parameter specifies - the size of the font. The fonts have various sizes, ranging from 1 to 5, where 1 is the smallest font size and 5 the largest.
The third and fourth parameters specifies the x,y coordinate for the top left hand corner of the text.
The fifth parameter is for the text to print.
The final parameter the colour of the text. This is the same colour that was allocated earlier using imagecolorallocate().
Bookmarks