Sponsored by NuSphere - PHP Software for PHP Application Developers - On Sale This Week for $100 off


PHP Tutorials and Scripts   




Title: Image Functions    Marked Cool    (Review this resource)
Author: econner
Posted On: 2004-11-25
Category: Home > PHP Tutorials

Popularity: 2 points out of 10    

Description: This tutorial will explore some of the image functions php offers.

Total Hits: 3600     Total Votes: 0     Total Points: 0 (0 reviews)    

Page Navigation:  [1]


Image manipulation

This tutorial will demonstrate some of the image functions php offers and how to create some of those spiffy verification images seen all over the internet. You will need a GD library for this tutorial which is available at http://www.boutell.com/gd/.

First we will look at the imagecreate function, probably the most fundamental of the creation functions (there are a lot more where you can create an image from a premade image and then add to it).
$im = @ imagecreate (100, 50); // this will create a new image that is 100 by 50 (pretty self explanatory)

Now for imagecolorallocate. This function allocates a color for the image and the first call to it will fill the background of the image. The syntax for imagecolorallocate looks like this:
imagecolorallocate ( resource image, red, green, blue)
So to create an image with a background of black you would do:
$im = @ imagecreate (100, 50); // this will create a new image that is 100 by 50 (pretty self explanatory)
$background = imagecolorallocate ($im, 0, 0, 0);

Another useful function is imagestring it will write a string to the image at a specified x, y location in a specified font and color. There are 5 built in fonts which are pretty crappy, but you can use the imagettftext function to output a string in a font you upload to your server.
The syntax for imagestring looks like this:
imagestring ( resource image, font, x, y, string to write, color to write it in)
Ok now we can create an image with a black background and write Hello in a white font:

$im = @ imagecreate (100, 50); // this will create a new image that is 100 by 50 (pretty self explanatory)
$background = imagecolorallocate ($im, 0, 0, 0);
$white = imagecolorallocate ($im, 255, 255 ,255);
imagestring ($im, 5, 0, 0, "Hello" , $white);


imagettftext function, the php manual gives a very good explanation of this one so I figured I would include it word for word:

"imagettftext -- Write text to the image using TrueType fonts
Description
array imagettftext ( resource image, float size, float angle, int x, int y, int color, string fontfile, string text)

image

The image resource. See imagecreate().
size

The font size. Depending on your version of GD, this should be specified as the pixel size (GD1) or point size (GD2).
angle

The angle in degrees, with 0 degrees being left-to-right reading text. Higher values represent a counter-clockwise rotation. For example, a value of 90 would result in bottom-to-top reading text.
x

The coordinates given by x and y will define the basepoint of the first character (roughly the lower-left corner of the character). This is different from the imagestring(), where x and y define the upper-left corner of the first character. For example, "top left" is 0, 0.
y

The y-ordinate. This sets the position of the fonts baseline, not the very bottom of the character.
color

The color index. Using the negative of a color index has the effect of turning off antialiasing. See imagecolorallocate().
fontfile

The path to the TrueType font you wish to use.

Depending on which version of the GD library that PHP is using, when fontfile does not begin with a leading / then .ttf will be appended to the filename and the library will attempt to search for that filename along a library-defined font path.

When using versions of the GD library lower than 2.0.18, a "space" character, rather than a semicolon, was used to define alternate paths to the font files. Unintentional use of this feature will result in the warning message: Warning: Could not find/open font.
text

The text string.

May include any UTF-8 character sequences (of the form: {) to access characters in a font beyond the first 255.

If a character is used in the string which is not supported by the font, a hollow rectangle will replace the character. " --php.net/imagettftext

Outputting the image:
Now that we've got our image created we need to output it. There are a number of functions for this; imagejpeg, imagegif, imagepng and more. For this tutorial we will use imagepng.
imagepng ($im); // will output the resource image $im to the browser

Ok now we will create an image verification script. The first step would be to create some sort of random text and insert it into a database. On your image page you would then select that text and create an image with it. Then output it to a browser with a text field so a user can enter in some text to verify that they are not a program. Assuming you've already come up with a way to insert and select the random text, we will create a script to make an image from that text.

php:

<?php
$im
= @ imagecreate (200, 50); // this will create a new image that is 200 by 50 (pretty self explanatory)
$background = imagecolorallocate ($im, 255, 255 ,255); // set the background to white

/*
* Initialize 2 colors, one red and one light red. One for the main text and
* one for a shadow
*/
$red = imagecolorallocate ($im, 255, 0, 0);
$lightred = imagecolorallocate ($im, 243, 138, 138);

$text = "My random text" ;
$font = "arial.ttf" ; // you would need to upload this file to your server in the same directory as this script

// Add some light red shadow to the text
imagettftext ($im, 11, 0, 17, 21, $lightred, $font, $text);
imagettftext ($im, 11, 0, 7, 21, $lightred, $font, $text);

// Add the text
imagettftext ($im, 11, 0, 11, 20, $red, $font, $text);

imagepng ($im); // will output the resource image $im to the browser
?>


The point of the shadows/extra text is to throw off an image recognition script. Alright now we will draw some lines and squares. There are two pretty self explanatory functions for this: imagefilledrectangle and imageline. Imagefilledrectangle will draw a filled rectangle from a certain point (x1, y1) to another point (x2, y2). (0,0 is the top left corner of the image)
imagefilledrectangle ($im, 0, 0, -20, 20, $black); // this will draw a 20x20 black rectangle (or whatever color the variable $black is allocated to) in the top left corner of the image

And now a line:
imageline() will draw a line from x1, y1 to x2, y2 in an image of a specified color.
imageline ($im, 0, 0, 30, 0, $black); // this will draw a line from the top left corner of the image to the right in the specified color $black

There are quite a few more functions viewable in the php manual. This is only an introduction. Have fun :).



Page Navigation:  [1]



© Copyright 2003-2008 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.