Gossamer Forum
Home : General : Internet Technologies :

calling a script through an html image tag

Quote Reply
calling a script through an html image tag
Can anyone tell me what issues arise when you call a script by referencing it with an html <img> tag? I've written a very simple traffic tracking script in PHP that I'm current calling in every page with something like:

<img src="....tracker.php" height="1" width="1">

So far it appears to be working just fine. No broken images, and the script is doing its job. But this seems rather unorthodox, so I'm wondering if perhaps there's something I'm missing. Does anyone have experience with this kind of thing?

Thanks in advance for any advice or ideas.

Fractured Atlas :: Liberate the Artist
Services: Healthcare, Fiscal Sponsorship, Marketing, Education, The Emerging Artists Fund
Quote Reply
Re: [hennagaijin] calling a script through an html image tag In reply to
Thats how I would do it....you could also add something in that php script which sends back a blank image...either with GD, or maybe by using some HTML within the script to call it....all depends how you want to do it...

Another way would be to use a IFRAME/ILAYER bit of code, that calls a simple blank page.... its up to you really Smile

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] calling a script through an html image tag In reply to
Thanks. What does "GD" stand for?

Fractured Atlas :: Liberate the Artist
Services: Healthcare, Fiscal Sponsorship, Marketing, Education, The Emerging Artists Fund
Quote Reply
Re: [hennagaijin] calling a script through an html image tag In reply to
Not actually sure. I wouldn't recommend it though if its for a distributed package, as quite a few people don't support the GD Library Wink

A good tutorial is at: http://www.devshed.com/...eneration/page1.html

This is where I learned how to use it (great site BTW...lots of cool PHP articles).

Hope that helps....

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] calling a script through an html image tag In reply to
So you're saying that if the PHP script itself generates, say, a 1x1 transparent gif, that gif will appear on the page? That seems odd to me, since that image tag works for tracking purposes on html or even cgi pages. But for the PHP code to actually execute on the page calling the image tag, wouldn't it have to have a .php extention? Or maybe I'm missing something...

Fractured Atlas :: Liberate the Artist
Services: Healthcare, Fiscal Sponsorship, Marketing, Education, The Emerging Artists Fund
Quote Reply
Re: [hennagaijin] calling a script through an html image tag In reply to
Yup, it would show the image. Rather than 'echo'ing the image output, you would simply 'return' it, so that the entire scripts output is the image....it wouldn't matter what the type of page calling it is, as long as its plain HTML, with the image src being the script....

Hope that helps Smile The best way is to experiment with it....

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [hennagaijin] calling a script through an html image tag In reply to
Easiest thing, if you wanted to be strict, is to create a transparent 1x1 gif. Then encode that into php compliant binary string. In perl it would be something like "\x12\x123\x12..." and so on.

Browser's sometimes use file extension to determine how to display a file, but not usually. Nowadays, any self-respecting browser will base how it displays a file based upon some extra headers provided by the host (always provided, though you never seem them). The most important being "mime-type".

So in essence the the script would receive the call from the img tag, do what it wants to do. Then, it would send a mimetype header like "image/gif" (you ming be familiar with "text/html") then print the binary string.
Quote Reply
Re: [Aki] calling a script through an html image tag In reply to
Yikes. Thanks for the advice, but you're gone a bit over my head here. Still, I definitely like the idea of doing things "the right way" (or "strict" as you put it).

Headers and mime-types I understand - no problem. I did a little bit of poking through Google searches and on http://www.php.net, but wasn't able to find anything about encoding images in php compliant binary strings. I'm not even sure I really know what that means, to tell the truth.

The script works as is, so it's not urgent. But if you feel like giving me a pointer or two about binary strings, I'd be glad to hear it. =)

Fractured Atlas :: Liberate the Artist
Services: Healthcare, Fiscal Sponsorship, Marketing, Education, The Emerging Artists Fund
Quote Reply
Re: [hennagaijin] calling a script through an html image tag In reply to
My favourite 1x1 gif generating perl code (adapted from a posting on perlmonks.com):
Code:
my ($r,g,b,t);

#Adjust the colors (red, green, blue) here, from 0-255
$r = 255;
$g = 0;
$b = 0;

# Set $t to 1 for a transparent gif, 0 for normal
$t = 1;

printf "Content-Length: %d\nContent-Type: image/gif\n\n", $t ? 43 : 35;
printf "GIF89a\1\0\1\0%c\0\0%c%c%c\0\0\0%s,\0\0\0\0\1\0\1\0\0%c%c%c\1\0;",144,$r,$g,$b,$t ? pack("c8",33,249,4,5,16,0,0,0) : "",2,2,4;
The resulting gif is only 43 or 35 bytes, so it's not a problem for your bandwidth. Works perfect for tracking purposes in an <img src="script.cgi"> tag.

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] calling a script through an html image tag In reply to
He's doing it in PHP though Wink

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] calling a script through an html image tag In reply to
Translate it, then. Wink

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] calling a script through an html image tag In reply to
Thanks for the code. I think I can translate perl functions to php functions without too much trouble. But do you know if I also need to translate the binary string for the image? Is that something that's universal, or is it language-dependent? Thanks.

Fractured Atlas :: Liberate the Artist
Services: Healthcare, Fiscal Sponsorship, Marketing, Education, The Emerging Artists Fund
Quote Reply
Re: [hennagaijin] calling a script through an html image tag In reply to
 
GD for php has it's own functions to create images - it's really quite simple. I'd go to the GD page and just read through the docs before translating anything. I'm using it quite extensively and you can pretty much do anything.

cheers,
r.
Quote Reply
Re: [ryel01] calling a script through an html image tag In reply to
Thanks. Unfortunately, the GD library isn't installed on the server where my site is hosted (shared host). I suppose I could try doing some sort of local installation, but that just seems like far more trouble than it's worth.

Fractured Atlas :: Liberate the Artist
Services: Healthcare, Fiscal Sponsorship, Marketing, Education, The Emerging Artists Fund
Quote Reply
Re: [hennagaijin] calling a script through an html image tag In reply to
I think it's a pretty direct translation. The following works for me:

Code:
<?
$r = 255;
$g = 0;
$b = 0;
header("Content-Length: 43");
header("Content-Type: image/gif");
printf("GIF89a\1\0\1\0%c\0\0%c%c%c\0\0\0%s,\0\0\0\0\1\0\1\0\0%c%c%c\1\0;",
144,
$r, $g, $b,
pack("c8",33,249,4,5,16,0,0,0),
2,2,4
);
?>


Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] calling a script through an html image tag In reply to
You rock my world, you sexy Canadian! Wink

One of these days I'll have to spend some time on php.net and figure out what all that actually means. In the meantime, though, we're got ourselves a fully-functional 1x1 transparent gif!

EDIT: I guess Yogi's sexiness deserves some acknowledgement too, since I just realized that's almost identical to what he wrote...

Fractured Atlas :: Liberate the Artist
Services: Healthcare, Fiscal Sponsorship, Marketing, Education, The Emerging Artists Fund

Last edited by:

hennagaijin: Nov 22, 2002, 2:58 PM