The image function that PHP uses are based on the GD image library that is developed by Boutell.com. In previous versions of PHP the GD library was used as-is within PHP, but since PHP4.3 the code of GD library has been bundled with the PHP installation and includes some enhancement to the original code such as alpha blending.
If you see any room for improvement, or if you can add something than go ahead and comment and I will definitely give it a look for possible inclusion.
First you need to create a blank image canvas for PHP to work on it. You can use imagecreate() or imagecreatetruecolor() functions, both of the functions take two parameters—the width and height of the blank image that you want to create.
resource imagecreate ( int $width , int $height )
$myImage = imagecreate(400, 400);
The new blank image that this line creates is 400 pixels wide and 400 pixels high. This function also returns a value—after the function has been called, $myImage contains an image identifier that refer to the new blank image in memory. You’ll use this image identifier with other image functions so that they know which image in memory you’re working with.
Before you can start drawing on your blank image, you need to decide what colors you want and then use the the imagecolorallocate() functions which takes four parameters:
int imagecolorallocate(resource image, int red, int green, int blue)
You would do this in PHP using the following code:
$greyColor = imagecolorallocate($myImage, 204, 204, 204) //Grey color
To color a single pixel on your canvas, use the imagesetpixel() function:
int imagesetpixel( resource image, int x, int y, int color)
$blackColor = imagecolorallocate($myImage, 0, 0, 0) ; imagesetpixel($myImage, 200, 200, $blackColor);
To draw a line between the two given points, use the imageline() function.
bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
imageline($myImage, 0, 0, 400, 400, $blackColor);
Example code:
Open your text editor and add the following code:
<?php
$myImage = imagecreate(400, 400);
$greyColor = imagecolorallocate($myImage, 204, 204, 204) ;
$blackColor = imagecolorallocate($myImage, 0, 0, 0) ;
imageline($myImage, 0, 0, 400, 400, $blackColor);
header("Content-type:image/png");
imagepng($myImage);
imagedestroy($myImage);
?>
Save the file as line.php and open it in a web browser.

To be able to draw a rectangle you really only need two positions on the stage—the two opposite corners of your rectangle. Because of this, the syntax for the imagerectangle() function is exactly the same as the imageline() function.
bool imagerectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
Open the line.php file that you just created and save it as rectangle.php. Replace the line
imageline($myImage, 0, 0, 400, 400, $blackColor);
with the line
imagerectangle($myImage, 20, 20, 380, 380, $blackColor);

To draw circles and ellipses in PHP, use the imageellipse() function. It works differently from the imagerectangle() and imageline() functions, in that you do not provide the outer limits of the shape. Rather, the ellipse is described by providing its center point, and then telling the imageellipse() function how high and how wide the ellipse has to be. Here’s what the function syntax looks like:
bool imageellipse ( resource $image , int $cx , int $cy , int $width , int $height , int $color )
Here’s an example:
imageellipse($myImage, 200, 200, 380, 380, $blackColor);

The example of circle shown in above picture has its center on the pixel where x = 200 and y = 200. The width of the ellipse is 380 pixels and height is 380 pixels.
Example code:
<?php
$myImage = imagecreate(400, 400);
$greyColor = imagecolorallocate($myImage, 204, 204, 204) ;
$blackColor = imagecolorallocate($myImage, 0, 0, 0) ;
imageellipse($myImage, 200, 200, 380, 380, $blackColor);
header("Content-type:image/png");
imagepng($myImage);
imagedestroy($myImage);
?>
An arc is a partial ellipse—one that doesn’t join up. You describe an arc in the same way as an ellipse, except that you need to add additional parameters to describe where the arcs starts and ends. The start and end points are provided in degrees, there being 360 degree in a complete ellipse
Syntax for an arc:
bool imagearc ( resource $image , int $cx , int $cy , int $width , int $height , int $start , int $end , int $color )
Here’s an example of using the imagearc() function to draw a partial ellipse:
<?php
$myImage = imagecreate(400, 400);
$greyColor = imagecolorallocate($myImage, 204, 204, 204) ;
$blackColor = imagecolorallocate($myImage, 0, 0, 0) ;
imagearc($myImage, 200, 200, 380, 380, 0, 270, $blackColor);
header("Content-type:image/png");
imagepng($myImage);
imagedestroy($myImage);
?>

Type the following code in your text editor.
<?php
$myImage = imagecreatefromjpeg("fubbu.jpg");
header("Content-type:image/png");
imagepng($myImage);
imagedestroy($myImage);
?>
Save it as showjpeg.php, make sure that the filename that you provides as the parameter to the imagecreatefromjpeg() function is a JPEG file and exists in the same directory from which the showjpeg.php script is running.
Browse to showjpeg.php in your web browser.

If you are working on a Web site that displays original art or photographs, you may want to protect your or your client’s intellectual property from being stolen. A common way of doing this is to apply a watermark to the image in such a way that it is still recognizable, but stops someone else from using it as his own.
First, create a simple copyright image which could be use on other images. I created some black text image and saved it as a PNG.
![]()
Copying the Copyright into the Image
Now, I open the original image that I want to watermark:
<?php
$myImage = imagecreatefromjpeg("fubbu.jpg");
I then open my copyright notice or logo. Because its a PNG file I use the imagecreatefrompng() function to open the file.
$myCopyRight = imagecreatefrompng("copyright.png");
You may want to position your copyright notice in the top-left hand corner of the image, in which case you don’t need the next steps. But if you want to position your notice on the right, bottom, or center of the image you have to know dimensions of each of your images. The function imagesx() returns the width of an image, although the function imagesy() returns the image height . In this example, I’ll position the copyright in the center of my image, so I must get the width and height of both the original image and the copyright notice:
$myImageWidth = imagesx($myImage); $myImageHeight = imagesy($myImage); $myCopyRightWidth = imagesx($myCopyRight); $myCopyRightHeight = imagesy($myCopyRight); $positionX = ($myImageWidth - $myCopyRightWidth) / 2; $positionY = ($myImageHeight - $myCopyRightHeight) / 2;
Once you know where you need to put the copyright notice, you can go ahead and copy it into the image to be watermarked. The function to do is imagecopy():
bool imagecopy ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h )
Copy a part of src_im onto dst_im starting at the x,y coordinates src_x , src_y with a width of src_w and a height of src_h . The portion defined will be copied onto the x,y coordinates, dst_x and dst_y .
imagecopy($myImage, $myCopyRight, $positionX, $positionY, 0, 0, $myCopyRightWidth, $myCopyRightHeight);
After the data is copied across, you can output the image as usual. Remember to clean up the memory that the copyright image has used:
header("Content-type:image/png");
imagepng($myImage);
imagedestroy($myImage);
imagedestroy($myCopyRight);
?>

That’s it guys. If you see any room for improvement, or if you can add something than go ahead and comment and I will definitely give it a look for possible inclusion.
Share Some Love