Library tutorials & articles
Image Generation on the FLY using PHP
- Introduction
- Getting Started
- Creating our image
- Displaying our image
- Generating the random number
- Changing the font
Displaying our image
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 type
Creating the actual image
Firstly, 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 don't want the browser to treat our page as HTML, because it doesn't 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 within your web browser, you should see a simple rectangle, like the one shown below:
If you don't 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.
Related articles
Related discussion
-
Create this kind of website
by maidentv (1 replies)
-
PHP London July Meetup
by webdeveloperit (1 replies)
-
Binary Studio | software development outsourcing Ukraine
by shane124 (4 replies)
-
SOFTWARE PHP WORK
by synctel (0 replies)
-
London PHP / MySQL Superstar needed!
by gatewaytechnolabs (1 replies)
Related podcasts
-
EarthClassMail.com - Moving from LAMP to .NET 3.5
Scott chats with Matt Davis, architect at EarthClassMail.com, about their move from a LAMP stack (Linux/Apache/mysql/PHP) to .NET 3.5. What's working, what's not, and what kinds of issues are they running into as their architect their solution.
Events coming up
-
Dec
3
The Auckland PHP December meetup
Auckland, New Zealand
Topic: Magento E-Commerce platform Speaker: Robert Popovic, LERO9, Robert is the Technical Director and co-founder of LERO9. Robert attended the Electrotechnical Faculty at The University of Belgrade where he graduated with a Masters in Computer Science and Information Technology. Robert has worked exclusively in the field of web and software development throughout his career.
Hi
I saw them using a image-on-the-fly script as (at www.worldspaceasia.com):
<a href="modules.php?name=Content&pa=listpagescategories&cid=2"><img src="includes/img.php?title=Western Classical/Jazz&textsize=8&border=1&imgype=randomTTF" border="0" alt="Western Classical/Jazz"></a>
The :
title=
imgype=randomTTF
are crucial!
So whts the secret of the img.php?
Regards
Very good tutorial and useful. But am wondering, if it can be converted to be used as a counter in a website, to get the number of visitors to the site. It will then start from a certain number and count sequencially upwards. It could start from 1 or 10.
Thanks
I used the script it was good. The random script is accessed as an image in another script, then how to access the number displayed on the image so that i can check that the user entered the correct displayed number.
very well-written & useful javascript:smilie('
')
thanx 4 ur time!
thanx 4 ur time!
hi:
I copied your code to my PHP4.3.x+Windows2000Server+Apache2, The picture does not show at a browser, only white page. I checked the sapce befor the "<?php" carefully.
In fact, befor I have a similar bad experience that I can't show picture at my machine. But If I put the code to a linux machine , it works. The situation is all my other php codes, including session,mysql, work fine. Only for this method, header(header("Content-type:image/jpeg"), the send out a image, it does not work.
I guess there must be some setting parameters problem at apache2. Does anyone have any idea?
Thanks
I think your posting in the wrong place buddy! this is for creating and showing PHP images on-the-fly not VB! If you DO really want to take a PHP created image and put it in a VB form you can either post back here or email me at webmaster@isgeeky.com
To my knowledge it is not possible to send an image created on-the-fly using PHP without the content-type header being modified. Why do you not want to use the "Header("Content-Type: Image/jpg")" code?
Actually I have seen your image generation on the fly and i have used it on my project. But i facing a problem is i don't want to use a Header("Content-Type: Image/jpg") . So if you have link please send me on sshti123@hotmail.com.
Vivek
Actually I have seen your image generation on the fly and i have used it on my project. But i facing a problem is i do want to use a Header("Content-Type: Image/jpg") . So if you have link please send me on sshti123@hotmail.com.
Vivek
Actually I have seen your image generation f\on the fly and i have used it on my project. But i facing a problem is i do want to use a Header("Content-Type: Image/jpg") . So if you have link please send me on sshti123@hotmail.com.
Vivek
i want to display an image on a form when an item is selected from a ComboBox. the image will be stored on the a:\drive (floppy). there will be multiple items in the combobox, each 1 relating to a specific picture. i just need the basic code to get the img to appear after selecting the item from the combobox. if n e 1 could email me the code or tell me where to get it, that would be great! oz_utester@hotmail.com
thanx 4 ur time!
This thread is for discussions of Image Generation on the FLY using PHP.