Web Info and IT Lessons Blog...

Friday 17 April 2015

How to create an image in PHP?

The previous lesson of PHP Lessons Series was about making a virtual host and in this lesson we will learn to create an image in php and use several other php image functions.

Create Image in PHP

Well, creating an image in php is not a problem at all. All you need to do to create an image in php is use this function given below:


imagecreate(imageWidth,imageHeight);

The above imagecreate() function will create an image with the width and height passed in the function as parameters but it won't display the image. To display the image we have the following functions in php:

1. imagejpeg - used to display a jpeg image.
2. imagepng - used to display a png image.
3. imagegif - used to display a gif image.

Let us write a php code to create and display a blank image in php.


<?php
header('Content-type: image/jpeg');
$imageWidth=100;
$imageHeight=60;
$image=imagecreate($imageWidth,$imageHeight);
imagejpeg($image);
?>

In the above php code first of all we have set the content type to image/jpeg. This means that this php page will output a jpeg image when executed. In the next step we have set the image width and image height and passed the values in the imagecreate() function. Finally imagejpeg is used to display the newly created jpeg image.

Similarly we can create png and gif images by changing the Content-type header and imagejpeg() function to imagepng and imagegif functions respectively.

How to allocate a colour to an image in PHP?


PHP imagecolorallocate() function is used to allocate a colour to an image. The basic syntax of imagecolorallocate() function is given below:


imagecolorallocate(image,rgb(red),rgb(green),rgb(blue));

The syntax of imagecolorallocate() is pretty much self explanatory. We have to pass the image we want to assign colour to, and the rgb values of the colour we want to assign separated by commas. Let us allocate some colour to the previously created blank image.


<?php>
header('Content-type: image/jpeg');
$image_width=100;
$image_height=60;
$image=imagecreate($image_width,$image_height);
imagecolorallocate($image,0,255,255);
imagejpeg($image);
?>

The above php code will fill the black image with the specified colour.

How to deallocate the colour assigned to an image in PHP?


imagecolordeallocate() is used to deallocate the colour assigned by imagecolorallocate() function. Check the php code given below to first allocate a light blue colour to an image and then deallocate the assigned colour to allocate a new red colour to it.


<?php
header('Content-type: image/jpeg');
$image_width=100;
$image_height=60;
$image=imagecreate($image_width,$image_height);
$lightBlueColour = imagecolorallocate($image,0,255,255);
imagecolordeallocate($image, $lightBlueColour);
$redColour = imagecolorallocate($image,255,0,0);
imagejpeg($image);
?>


For more lessons about PHP subscribe on InformationBitz.com

Related Posts
Drawing image patterns using imagesetpixel() in PHP
Convert a string to an image in PHP
Convert an image to a string in PHP
Write text to image in php using imagettftext()

No comments:

Post a Comment