Documente Academic
Documente Profesional
Documente Cultură
Page 1 of 15
<?php
create_image();
print "<img src=image.png?".date("U").">";
function create_image(){
$im = @imagecreate(200, 200) or die
("Cannot Initialize new GD image stream");
$background_color =
imagecolorallocate($im, 255, 255, 0); //
yellow
imagepng($im,"image.png");
imagedestroy($im);
}
?>
https://mail.vit.ac.in/exchange/pradeep/Inbox/Images%20Tutorial.EML/1_multipart_xF8F... 4/10/2012
z
z
Page 2 of 15
Lanes 5 to 10: contains the function which will create the new images.
z
Line 6: $im is the variable which will be used to store the image.
imagecreate() will start the creation process. imagecreatetruecolor() may be used instead
"@" is used before imagecreate() to avoid displaying errors (just in case).
Parameters within imagecreate(200,200) are the dimensions of the new image
{ The first number is the width in pixels.
{ The second number is the height in pixels.
In this case we have created a 200x200 pixels image
In case the image can not be created, and error is shown (die command).
z
z
z
z
Line 7: We will define a color to be used as background color of the image, and a variable to
contain it ($background_color).
When using imagecreate() command in line 6, the first color defined will be considered as the
background color of the image. In case imagecreatetruecolor() command is used instead, the
background color will be black (but we may latter draw a colored square area with a different
color and the dimensions of the image).
The imagecolorallocate() command is used to define colors and "reserve" them for future use.
RGB color model is used
Lane 8: imagepng() will save the info at $im (the image) in the file "image.png". Other image
types may be created by using commands imagegif(), imagewbmp(), imagejpeg(), or imagetypes
(). In this tutorial we have selected png due to its open source nature.
Lane 9: we have finish using the information at variable $im, so we will destroy it with
imagedestroy() to free any memory associated to it.
https://mail.vit.ac.in/exchange/pradeep/Inbox/Images%20Tutorial.EML/1_multipart_xF8F... 4/10/2012
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Page 3 of 15
Lanes 9 and 10: new colors are added to the image (as explained earlier for line 7).
Lane 11: horizontal line is draw.
Lane 12: diagonal line is draw.
To draw lines imageline() command is used. Check the picture bellow to understand the parameters used
with this command.
imageline ($im, X1, Y1, X2, Y2, $color);
where X1, Y1, X2 and Y2 are the positions
within the image.
For example, the red line starts at
X1=5 and Y1=5
and ends at
X2=195 and Y2=5
https://mail.vit.ac.in/exchange/pradeep/Inbox/Images%20Tutorial.EML/1_multipart_xF8F... 4/10/2012
Page 4 of 15
https://mail.vit.ac.in/exchange/pradeep/Inbox/Images%20Tutorial.EML/1_multipart_xF8F... 4/10/2012
Page 5 of 15
https://mail.vit.ac.in/exchange/pradeep/Inbox/Images%20Tutorial.EML/1_multipart_xF8F... 4/10/2012
Page 6 of 15
To draw the ellipses, position of the center of the ellipse, and its width and height are considered. Check
the picture bellow to understand the parameters used with those commands.
https://mail.vit.ac.in/exchange/pradeep/Inbox/Images%20Tutorial.EML/1_multipart_xF8F... 4/10/2012
Page 7 of 15
22 270, $red);
23
imagearc($im, 170, 50, 40, 60, 0,
24 360, $red);
25
imagefilledarc($im, 20, 150, 40,
60, 0, 90, $blue, IMG_ARC_PIE);
imagefilledarc($im, 70, 150, 40,
60, 0, 180, $blue, IMG_ARC_PIE);
imagefilledarc($im, 120, 150, 40,
60, 0, 270, $blue, IMG_ARC_PIE);
imagefilledarc($im, 170, 150, 40,
60, 0, 360, $blue, IMG_ARC_PIE);
imagepng($im,"image.png");
imagedestroy($im);
}
?>
Lanes 12-15: a red arc is drawn (only the outer line)..
Lanes 17-20: a blue arc is drawn (a filled one).
The basic codes used is
imagearc ( $im, X, Y, width, height, arc start, arc end, $color)
imagefilledarc ( $im, X, Y, width, height, arc start, arc end, $color, flag )
X and Y define thy position of the center of the arc within the image
Width and height are the dimensions of the complete circle formed by the arc.
arc start and arc end are the start and the end of the arc in degrees. If start is zero, as in the example,
the figure will start in the right.
Flags are specific options available for those commands. Visit the commands information to get
additional information about all possible flags.
Check the figure bellow for better understanding of the commands above.
https://mail.vit.ac.in/exchange/pradeep/Inbox/Images%20Tutorial.EML/1_multipart_xF8F... 4/10/2012
Page 8 of 15
The commands imagearc () and imagefilledarc () may also be used to draw circles (width=height, arc
start=0 and arc end=360).
Other figures you may draw:
imagepolygon () -- Draw a polygon
imagefilledpolygon () -- Draw a filled polygon
https://mail.vit.ac.in/exchange/pradeep/Inbox/Images%20Tutorial.EML/1_multipart_xF8F... 4/10/2012
18
19
20
21
Page 9 of 15
https://mail.vit.ac.in/exchange/pradeep/Inbox/Images%20Tutorial.EML/1_multipart_xF8F... 4/10/2012
Page 10 of 15
If the reference image has a different extension, other commands may be used:
imagecreatefromgif -- Create a new image from file or URL
imagecreatefromjpeg -- Create a new image from file or URL
imagecreatefrompng -- Create a new image from file or URL
imagecreatefromwbmp -- Create a new image from file or URL
imagecreatefromxbm -- Create a new image from file or URL
imagecreatefromxpm -- Create a new image from file or URL
Lane 6 and 7: the image is saved to a file and frees memory (as described in code 1).
Lane 9: Both images are shown (original and new).
https://mail.vit.ac.in/exchange/pradeep/Inbox/Images%20Tutorial.EML/1_multipart_xF8F... 4/10/2012
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Page 11 of 15
// width of the
// height of
Resized image
Lane 3: the information about the original image is stored in variable $original_image.
Lines 6: size of original image is obtained. The info is contained in an array as explained in the code.
Lanes 10 and 11: width and height of the image are extracted from the array and stored in variables
$width and $height.
Lanes 13 to 15: In this example, the image will be reduce to 70%, so new width and height are
computed as shown. To avoid decimals round command is used.
Lane 17: In this line starts the creation process of the new image. Command imagecreate() will define
the dimension of the new image, and variable $new_image will store all information related to the
image.
Lane 18: This is the core command of this script. Lets try to explain the command by using a picture
which contains all the elements in the command.
https://mail.vit.ac.in/exchange/pradeep/Inbox/Images%20Tutorial.EML/1_multipart_xF8F... 4/10/2012
Page 12 of 15
Where:
Xo1, Yo1, Xo2 and Yo2 are the dimensions of the original image
Xn1, Yn1, Xn2 and Yn2 are the dimensions of the new image
Lane 20 and 21: the image is saved to a file and frees memory (as described in code 1).
Lane 23: Both images are shown (original and new).
New image
https://mail.vit.ac.in/exchange/pradeep/Inbox/Images%20Tutorial.EML/1_multipart_xF8F... 4/10/2012
Page 13 of 15
Lane 4: the information about the original image is stored in variable $original_image.
Lane 7: a new 200x200 pixels image is created.
Lane 10: a color is defined for the new image.It will be also the background color (for better
understanding, check code 7)
Lanes 12: The core command of this script.
A square area from original image is selected. In this example, the selected area is defined by
position (0,0) and position (100,100).
The selected area is copied to position (75,75) of the new image and dimension of the area are
maintained (100x100).
Lane 14 and 15: the new image is saved to a file and frees memory (as described in code 1).
Lane 17: Both images are shown (original and new).
Modified image
https://mail.vit.ac.in/exchange/pradeep/Inbox/Images%20Tutorial.EML/1_multipart_xF8F... 4/10/2012
Page 14 of 15
?>
https://mail.vit.ac.in/exchange/pradeep/Inbox/Images%20Tutorial.EML/1_multipart_xF8F... 4/10/2012
Page 15 of 15
Cast My Vote!
PHPTutorial.info.
https://mail.vit.ac.in/exchange/pradeep/Inbox/Images%20Tutorial.EML/1_multipart_xF8F... 4/10/2012