PHP: Create images |
Code 1: create
a 200x200 square
(and a bit more) |
||
1 2 3 4 5 6 7 8 9 10 11 |
<?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); } ?> |
Code 2: draw lines lines 9 to 11 added to code 1 above |
||
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?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 $red = imagecolorallocate($im, 255, 0, 0); // red $blue = imagecolorallocate($im, 0, 0, 255); // blue imageline ($im, 5, 5, 195, 5, $red); imageline ($im, 5, 5, 195, 195, $blue); imagepng($im,"image.png"); imagedestroy($im); } ?> |
|
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 |
Code 3: draw rectangles similar to code 2, but lines 11 and 12 has been changed |
||
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?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 $red = imagecolorallocate($im, 255, 0, 0); // red $blue = imagecolorallocate($im, 0, 0, 255); // blue imagerectangle ($im, 5, 10, 195, 50, $red); imagefilledrectangle ($im, 5, 100, 195, 140, $blue); imagepng($im,"image.png"); imagedestroy($im); } ?> |
|
imagerectangle
($im,
X1, Y1, X2, Y2, $color); imagefilledectangle ($im, X1, Y1, X2, Y2, $color); For example, the red rectangle starts at X1=5 and Y1=10 and ends at X2=195 and Y2=50 |
Code 4: draw ellipses similar to code 2, but lines 11 and 12 has been changed |
||
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?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 $red = imagecolorallocate($im, 255, 0, 0); // red $blue = imagecolorallocate($im, 0, 0, 255); // blue imageellipse($im, 50, 50, 40, 60, $red); imagefilledellipse($im, 150, 150, 60, 40, $blue); imagepng($im,"image.png"); imagedestroy($im); } ?> |
|
imageellipse($im,
X, Y, width, height, $color); imagefilledellipse($im, X, Y, width, height,$color); For example, the center of the red ellipse is located at X=50 Y=50 and the dimensions are width = 40 height= 60 |
Code 5: draw arcs similar to code 2, but lines 12 to 20 has been added |
||
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?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 $red = imagecolorallocate($im, 255, 0, 0); // red $blue = imagecolorallocate($im, 0, 0, 255); // blue imagearc($im, 20, 50, 40, 60, 0, 90, $red); imagearc($im, 70, 50, 40, 60, 0, 180, $red); imagearc($im, 120, 50, 40, 60, 0, 270, $red); imagearc($im, 170, 50, 40, 60, 0, 360, $red); 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); } ?> |
|
Code 6: add text to the image lines 8 to 16 has been added to code 1 |
||
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?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 $red = imagecolorallocate($im, 255, 0, 0); // red imagestring($im, 1,file:///C:/apache2triad/htdocs/0image/tutorial1.html 5, 10, "Hello !", $red); imagestring($im, 2, 5, 50, "Hello !", $red); imagestring($im, 3, 5, 90, "Hello !", $red); imagestring($im, 4, 5, 130, "Hello !", $red); imagestring($im, 5, 5, 170, "Hello !", $red); imagestringup($im, 5, 140, 150, "Hello !", $red); imagepng($im,"image.png"); imagedestroy($im); } ?> |
Code 7: rotate image This is new code |
||
1 2 3 4 5 6 7 8 9 10 11 |
<?php $im = imagecreatefrompng("image.png"); $yellow = imagecolorallocate($im, 255, 255, 0); $rotate = imagerotate($im, 90,$yellow); imagepng($rotate,"image_rotated.png"); imagedestroy($im); print "<img src=image.png> - <img src=image_rotated.png>"; ?> |
-
|
Code 8: resize image This is new code |
||
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?php $original_image = imagecreatefrompng("image.png"); // obtain data from selected image $image_info = getimagesize("image.png"); // data contained in array $image_info may be displayed in next line // print_r($image_info) $width = $image_info[0]; // width of the image $height = $image_info[1]; // height of the image // we will reduce image size to 70%, so new dimensions must be calculate $new_width = round ($width*0.7); $new_height = round ($height*0.7); $new_image = imagecreate($new_width, $new_height); imagecopyresized($new_image, $original_image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); imagepng($new_image,"resized_image.png"); imagedestroy($new_image); print "<img src=image.png> <br>Resized image<BR> <img src=resized_image.png>"; ?> |
Resized image |
Code 9: get a portion of the
image Quite similar to code 8, but with a different result |
||
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php // get the original image $original_image = imagecreatefrompng("image.png"); // create new image $new_image = imagecreate(200, 200); // define red color (it will be the background of the new image) $red = imagecolorallocate($new_image, 255, 0, 0); imagecopyresized($new_image, $original_image, 75, 75, 0, 0, 100, 100, 100, 100); imagepng($new_image,"new_image.png"); imagedestroy($new_image); print "<img src=image.png> <br>New image<BR> <img src=new_image.png>"; ?> |
New image |
Code 10: modify an image A rectangle and a text will be added to an already existing image |
||
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php // get the original image $im = imagecreatefrompng("image.png"); // new color $blue = imagecolorallocate($im, 0, 0, 255); // blue $red = imagecolorallocate($im, 255, 0, 0); // red imagefilledrectangle ($im, 80, 5, 195, 60, $blue); imagestring($im, 5, 80, 5, "Modified!", $red); imagepng($im,"modified_image.png"); imagedestroy($im); print "<img src=image.png> <br>Modified image<BR> <img src=modified_image.png>"; ?> |
Modified image |
Code 11: create and show images in the
fly The image is not save to the disk |
||
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php header("Content-type: image/png"); $im = @imagecreate(200, 200) or die("Cannot Initialize new GD image stream"); $background_color = imagecolorallocate($im, 255, 255, 0); // yellow $blue = imagecolorallocate($im, 0, 0, 255); // blue imagestring($im, 3, 5, 5, "My Text String", $blue); imagepng($im); imagedestroy($im);
?> |