Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Linking to an image that is dynamic (i.e. <img src="counter.cgi"&gt

  • Thread starter Thread starter member 141630
  • Start date Start date
Status
Not open for further replies.
M

member 141630

Guest
Hi,
I have seen web page counters (for example) that have an html link to a cgi app - i.e. <img src="counter.cgi"> which would result in a dynamically generated image.

I can't see a way to make a php app become an image - i.e. what I would like to do is have a link to an image that is dynamically generated?

So in a nutshell, it's a link to a PHP script that results in an image being returned.


Thanks in advance

Phil Blythe


 
A PHP app doesn't become an image. A PHP script can output image data, though.

All the script must do is sent an appropriate "Content-Type" HTTP header (ie "Content-Type: image/jpeg" for a JPEG image), then stream the image data out to the browser.


To solve this problem you can do it one of two ways:[ul][li]have the PHP script produce the counter result as a single image. This will require the use of PHP's image family of functions, the use of which requires that you provide GD functionality to PHP.[/li][li]Create by hand ahead of time images for the digits 0 through 9, and have a PHP script output a set of image tags side-by-side which represents the current count.[/li][/ul]

The first way gives you the advantage of having a single image. The second way uses fewer server resources.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 

>All the script must do is sent an appropriate "Content->Type" HTTP header (ie "Content-Type: image/jpeg" for a >JPEG image), then stream the image data out to the browser.

OK, I'm understanding that fine - is there an example bit of code that you know of that I could view? I'm still grey and fuzzy about how a PHP script can stream image data out...

 
Your primary source for simple PHP example code should always be the PHP online manual. Take a look at the online manual entry for imagecreate().



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
I'll go take a look - my problem is not knowing what to look for in the php.net manual.

Thanks!
 
PHP's functions are organized into families, the families based largely on similar functionality or similar data structures on which they operate. (For example, function names beginning with "image" are all in the same family.)

The PHP online manual is organized the same way. If you go to the table of contents, it can give you a good starting place for looking for a function.

Don't forget, too, that the manual's search services are excellent.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Thats fine but looking at the online manual and what I've been told so far raises more questions than answers :-(

OK, so I call a PHP script using <IMG SRC="counter.php"> within my html.

I've included the correct header (I think) and some simple code in counter.php

<?php
Header("Content-type: image/gif");
?>
<IMG SRC="count.gif">

Doesn't work though.....


I've tried to make sense of the online manual but it really doesn't answer my question....... which is how to call a PHP script as though it were an image.
 
Your metaphors are not right.

The <IMG> tag on the main page tells the browser that an image is to be placed on the page in that location, and the tag's "src" attribute tells the browser where to find the image.

The browser opens a separate connection to the web server and fetches the image data. If the "src" attribute points to a static image file, the server sends a set of HTTP headers and a stream of binary image data. And nothing else.

If the <IMG> tag's "src" attribute points to a PHP script, then that PHP script will produce only the output of the appropriate content-type HTTP header and a stream of binary image data. It will not send any HTML or anything else.

Again, look at the example code on the online manual page for imagecreate() -- the example script on that page is everything necessary for PHP to create an image dynamically and stream it to a browser.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Heres a random message board avatar that I use elsewhere ....

Its created using PHP with the the GD library installed, and requires a textfile with captions one per line, plus a base .png image as the background.

This can be called as <img src=url/to/avatar.jpeg>

Ths PHP file is called avatar.jpeg and a .htaccess file specifies which application should process this type of file.(it could be called avatar.php but some boards wont allow anthing other than .gif or .jp(e)g)

Also required is a local font file.

Oh, I also generate some random colour too.

Code:
<?php
// Set the static variables 
$font_file = $_SERVER[DOCUMENT_ROOT]."/Playtoy.ttf";
$font_size = 15;
$y_start = 34;
$angle = 0;
$max_width = 80;
$picfile='avatar.png';

// Retrieve random fortune from text file 
$caption_array = file("avatar.txt");
srand((double) microtime() * 10000000);
$caption = array_rand($caption_array);


// Calculate horizontal starting point for the text 
$line_width = imagettfbbox($font_size, 0, $font_file, $caption_array[$caption]);
$x_start = (($max_width - $line_width[2] - $line_width[0]) / 4) + 4;


// Create the image resource and assign colors to variables 
header("Content-type: image/png");
$im = imagecreatefrompng($picfile);

$col1=rand(0,255);
$col2=rand(0,255);
$col3=rand(0,255);

$color = imagecolorallocate($im, $col1,$col2,$col3);


// Write the text to the image 
imagettftext($im, $font_size, $angle ,$x_start, $y_start, $color, $font_file, $caption_array[$caption]);


// Create and then destroy the image 
imagepng($im);
imagedestroy($im);


?>

avatar.jpeg



all good fun :-)

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
oh, hint .. refresh :-)

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
To summarise you need to call the "image" like
<img src="fred.php">

fred.php is a SEPERATE script which sets the content type header, abd then outputs the binary e.g.

$fp=fopen("mymage.jpg","rb");
header("content-type: image/jpg");
fpassthru($fp);
fclose($fp);

This example uses an image file, it could be from the database if you want, use some stock numbers or generate from scratch (I shall be knicking KarveR's script !)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top