today, i have seen a new fangled question. http://community.9cbs.net/Expert/TopicView1.asp?id=3382782 hoho, so many high PHP fans, they r use their imaginations.But i think the client's realization isn 'T Useful.But Some Site Really NEED A Server-Side Class or Framework.But IT I Have Thought About The Way To Make It Real. Of Course We need TU Use gd.it is the Same Way AS we use GD to generate a image painted by some numbers to authenticate user.But we need to do more more ... more work than that.we must anylist all XHTML tags and their attributes.and then, we must do like DreamWeaver, paint the image according to the page's layerout.the function we developed can read the XHTML and paint it.hoho, what a pretty tool.someone is interested? but we still should know sth about GD.now, i will show a simple but useful example To Introduce Our Star --- GD! Refernece from: http://www.developerfusion.com/show/1951/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1
PHP, or PHP Hypertext Pre-processor, is one of the most popular scripting languages available to web developers. There are many reasons why PHP is now the most popular server-side scripting language for the Apache web server. Firstly, it supports database connectivity to a huge number of popular vendors, including Microsoft, Oracle and IBM. Secondly, with the correct libraries installed, PHP also supports real-time generation of images, PDF files, and even shockwave movie files! The only limit that bounds us when we 'Reveloping with php is out.
For Developers Like Myself, One of the Most Popular Features of PHP is Real-Time Image Generation. That IS, WE CAN CREATE A New Image Canvas, "Paint" IT, And Either Save It To A File, or Send It Directly To The browser. in this article, I will show you how to create an image in real-time with PHP. The image will be similar to the ones used on yahoo.com and paypal.com, where a random number is generated inside of an image . that image is displayed in the browser, and you have to enter that random number into a text box to create a new account. This is a method that is becoming more popular, and it's used to stop people from generating multiple user accounts in one Go, Which, IS Some Cases, Can Crash The Web Server (S) .To Generate Image IN Real-Time Using PHP, We need to have sales you have the gd library installed, Create a new PHP .
PHP PHPINFO (Info_Modules);?>
Run the checkgd.php page on your web server. If you can find a section for the GD Library (LIKE IN THE SAMPLE Shown Below), THEN You Have The GD Library Installed and Ready To Go:
On the other hand, if you do not see a section for the GD library, then you will need to download and install the GD library. If you are running windows, then installation instructions are available here. If you are running Linux, installation INSTRUCTIONS Are Available Here.
Both of the links to installation instructions shown above use an older version of the GD library. This is because with the newer versions, any support for GIF images has been removed due to copyright infringements.
As mentioned at the beginning of this article, we will be creating an image that contains a random number, similar to the methods used by yahoo.com and paypal.com when you register to become a member.So, now that we have the GD Library Installed AS Part of Our PHP Distribution, Let's Jump Right in and create Our first Image.
THE FIRST Step To Creating Our Image Is To create a canvas to work with. We do this by usding the imagecreate function, as shown Below:
PHP
$ IMG_NUMBER = ImageCreate (100, 50);
?>
The imagecreate function creates a blank canvas. The width and height of the canvas are determined by the two parameters passed to the function respectively. So, in our example above, we have created a new image that is 100 pixels wide, by 50 pixels high .
The next step is to allocate the colours that will be used to generate our random number image. We use the imagecolorallocate function to do this. The imagecolorallocate function takes four arguments. The first is the image identifier, which in our case is $ img_number. The Last Three Arguments Specify The RGB Values of The Color That We Are Creating. RGB (Red, Green and Blue) Values Range from 0 (The Lightest). For Example, Black IS 0, 0, 0 and White IS 255, 255, 255. Let's create a couple of colours:
PHP
$ Img_number = imagecreate (100,50); $ white = imagecolorallocate ($ img_number, 255,255,255); $ black = imagecolorallocate ($ img_number, 0,0,0); $ grey_shade = imagecolorallocate ($ img_number, 204,204,204);
?>
As You Can See, WE HAVE CREATED Three New Color-Allocated Variables: $ White, $ Black and $ GREY_SHADE. These Will Be buy to create the different shapes and text for out.
Now That WE KNOW How To Allocate Colors, Why Don't We Add A Rectangle or Two To Our Image Canvas? PHP
$ Img_number = imagecreate (100,50); $ white = imagecolorallocate ($ img_number, 255,255,255); $ black = imagecolorallocate ($ img_number, 0,0,0); $ grey_shade = imagecolorallocate ($ img_number, 204,204,204);
ImageFill ($ IMG_NUMBER, 0, 0, $ GREY_SHADE); ImageRectangle ($ IMG_NUMBER, 5, 5, 94, 44, $ Black); ImageRectangle ($ IMG_NUMBER, 0, 0, 99, 49, $ black);
?>
The imagefill function performs a complete fill of the canvas identified by the first argument, $ img_number. Arguments two and three specify the x and y co-ordinates to start filling from. The last parameter is a variable created using the imagecolorallocate function.
Next, we draw two rectangles using the ImageRectangle function. The first argument to the ImageRectangle is a reference to a drawing canvas created using the imagecreate function. The next four arguments specify the top left-hand corner of the rectangle, as well as the lower Right-hand Corner. The last parameter is a reference to a color, create function.
In our example, we have created a new image canvas, filled our canvas, and added some rectangles, but where is the image you might ask? Good question. At this point in time, our image is still stored in memory. It's really quite Easy to Display The Image in A Web Browser, So Let's Do That Now !!
Displaying Our Image In a Web Browser Is A Synch, And Takes Just Two Easy Steps:
Telling the browser that we are outputting an image, and not the default HTML content typeCreating the actual imageFirstly, let me explain why we need to change the content type of our PHP script. By default, PHP is configured to send HTML output to the browser . The web server does this by sending a content-type = text / html header along with your HTML code. This tells the browser that it will be receiving HTML, and to process anything that comes from the server as pure HTML. Nothing else. In our example, we do not want the browser to treat our page as HTML, because it does not contain any. Our page will simply spit out the results of our new image. We want the browser to render our image as a standard JPEG Image File, So We change The Content Type Using The Header Function, LIKE THIS:
Header ("Content-Type: Image / JPEG");
This line MUST be placed right at the top of your PHP script, before any output occurs for it to work correctly. There can be no spaces before this line, and it must be enclosed with the PHP Php and?> Tags respectively.
Secondly, we will want to actually output our image to the web browser We can do this by using the imagejpeg function The imagejpeg function simply takes one parameter, which is a reference to an image canvas created using the imagecreate function..:
ImageJPEG ($ IMG_NUMBER);
So, just to recap, our complete image generation script is shown below. Create a new file named random_number.php. Copy the code shown below into random_number.php and run the script from your web browser.
PHP
//random_number.php $ img_number = imagecreate (100,50); $ white = imagecolorallocate ($ img_number, 255,255,255); $ black = imagecolorallocate ($ img_number, 0,0,0); $ grey_shade = imagecolorallocate ($ img_number, 204,204,204 Imagefill ($ IMG_NUMBER, 0, 0, $ GREY_SHADE); ImageRectangle ($ IMG_NUMBER, 5, 5, 94, 44, $ Black); ImageRectangle ($ IMG_NUMBER, 0, 0, 99, 49, $ black);
Header ("Content-Type: Image / JPEG"); ImageJpeg ($ IMG_NUMBER);
?>
WHEN You Run Random_Number.php from Wtem Web Browser, You Should See A Simple Rectangle, LIKE The One Shown Below:
If you do not see the rectangle in your browser, or if any errors occur, then make sure you have copied the code shown above exactly as it appears. Also, make sure there is no white space before the first Php tag.
Now That We've Got Our Basic Image Out of the Way, Let's Actually Generate A Random Number To Display As Part of Our Image.
Generating our random number is very easy. We will simply create a function named get_random which will return a random number between 1 and the built-in PHP function, getrandmax (). Getrandmax () is an implementation specific function, and returns the largest seed Value available on the current system. ou Get_random function Looks like this:
Function get_random () {SRAND ()); $ max = getrandmax (); return Rand (1, $ max) rand (1, $ max);
TO ACTUALLY PLACE ANY PIECE OF TEXTUAL DATA ONTO ONTO OUR Image, WE Use The ImageString Function. The ImageString Function Takes Six Parameters, AS Shown Below:
INT ImageString (int in, int font, int x, int y, string s, int co)
im: A reference to an image canvas created using the imagecreate functionfont:. An integer value specifying the number of the font to draw the text in If font is 1, 2, 3, 4 or 5, then a built-in font is usedx : The horizontal displacement from the left of the image where the text will be drawn from, measured in pixels.y: The vertical displacement from the top of the image where the text will be drawn from, measured in pixels.s: The text to be drawn on the imagecol:.. The color that the text will be painted A reference to a color created using the imagecolorallocate function.The following code will use our get_random function to generate a random number It will also use the imagestring function to draw that Number Onto Our Image CANVAS:
$ Number = get_random (); ImageString ($ IMG_NUMBER, 9, 30, 15, $ Number, $ black;
For Your Convenience, The Complete PHP Script To Generate Our Image Containing A Random Number Is Shown Below:
PHP
//random_number.php $ img_number = imagecreate (100,50); $ white = imagecolorallocate ($ img_number, 255,255,255); $ black = imagecolorallocate ($ img_number, 0,0,0); $ grey_shade = imagecolorallocate ($ img_number, 204,204,204 );
ImageFill ($ IMG_NUMBER, 0, 0, $ GREY_SHADE); ImageRectangle ($ IMG_NUMBER, 5, 5, 94, 44, $ Black); ImageRectangle ($ IMG_NUMBER, 0, 0, 99, 49, $ black);
$ Number = get_random (); ImageString ($ IMG_NUMBER, 9, 30, 15, $ Number, $ black;
Header ("Content-Type: Image / JPEG"); ImageJpeg ($ IMG_NUMBER);
Function get_random () {SRAND ()); $ max = getrandmax (); return Rand (1, $ max) rand (1, $ max);
?>
Copy the code shown above into random_numer.php, save the file, and load it into your web browser. You should be presented with an image similar to the one below (remember that we are using a random number, so yours will be different to Mine): Actually Displaying The Image As Part of A HTML Page IS Simple. Instead of Specifying The Src Attribute of An Image As A FileName, You Specify The Name of Our PHP Script, Like this:
When the browser loads this image, it will call the random_number.php script, which would return our image as a stream of binary data. The browser would then write this binary data to the screen as the image.
ONE LAST Thing I Will Discuss in this article is changing the font of the Random Number As It Appers IN OUR Image. This Step is Optional, But Demonstrates The flexibility of php and the gd library.
Changing the font of be random Number
To control the look and style attributes of our random number, we can replace the imagestring function with the imagettftext function, which lets us control some of the aspects of the font used to render the text.
The imagetftext function take election take: AS Described Below:
Array ImageTfText (int IM, INT Size, Int Angle, Int x, Int Y, INT COL, STRING FONTFILE, STRING TEX)
im: A reference to an image canvas created using the imagecreate functionsize: The height of the font, in pixelsangle: The rotation of the font Use 0 for standard horizontal displayx:. The starting x co-ordinate of where the text will be drawny: The starting y co-ordinate of where the text will be drawncol: A reference to a color created using the imagecolorallocate functionfontfile: The path and filename to the font that the text will be rendered in (more on this in a minute) text: The Actual String of Text To Be DrawNSo, for Our Example, We Would Used The ImageTfText Function Instead of The ImageString Function, Like this:
ImageTfText ($ IMG_NUMBER, 30, 10, 10, 10, $ black, 'Trebuchet.ttf', $ Number);
Notice the seventh argument? It's the location of the true-type font file that will be used to render our text onto the image. In our example, the file trebuchet.ttf is in the same directory as our script. Trebuchet.ttf is contained In The Support Material at the end of this article.
.,........................
Once You Have Saved The Random_Number.php File with The New ImageTftext Function Instead of The ImageStriing Function, Reload The page in Your Browser. You Should See Something Like this: