Image Tag Launches PHP Script
An image tag can launch a PHP script as the image is loaded.
There are a number of potential specialized uses. Some are listed here.
-
Logging IP address, user agent string, or other information for unique page loads by real browsers. (Robots are unlikely to load images, unless it's an image bot.)
-
Serving a different image for a specific IP address, user agent, cookie, or other unique identification. (This is addressed in the article.)
-
Generating a custom image on the fly to display on the web page.
-
Running pretty much any custom PHP code.
The PHP script will run without customization. But to do specialized tasks it needs to be customized.
The PHP Script
The PHP Image Proxy script needs no customization to run. Upload it now so you can construct img
tags for testing. It can be updated later for specialized functions.
Save the PHP script as image.php (or other .php file name that you prefer) and upload it to your server. Make a note of the uploaded image.php script's URL (for the img
tags.)
<?php
/*
Image Proxy
Version 1.0
February 24, 2018
Will Bontrager Software LLC
https://www.willmaster.com/
Copyright 2018 Will Bontrager Software LLC
This software is provided "AS IS," without
any warranty of any kind, without even any
implied warranty such as merchantability
or fitness for a particular purpose.
Will Bontrager Software LLC grants
you a royalty free license to use or
modify this software provided this
notice appears on all copies.
*/
$ImageFileForBrowser = urldecode(@$_SERVER['QUERY_STRING']);
/* **********************************
Your own PHP code can be placed
here to do whatever needs doing
whenever an image is requested.
(Remove the ******... lines so
your code won't be commented out.)
************************************* */
$lineExploded = explode('.',$ImageFileForBrowser);
if( strpos($ImageFileForBrowser,'/') === 0 ) { $ImageFileForBrowser = $_SERVER['DOCUMENT_ROOT'].$ImageFileForBrowser; }
$ext = array_pop($lineExploded);
$ext = strtolower($ext);
$BrowserFileType = 'image/';
switch($ext)
{
case 'jpg' : case 'jpeg' : $BrowserFileType .= 'jpeg'; break;
case 'svg' : $BrowserFileType .= 'svg+xml'; break;
case 'png' : $BrowserFileType .= 'png'; break;
case 'gif' : $BrowserFileType .= 'gif';
}
header("Content-type:$BrowserFileType");
echo file_get_contents($ImageFileForBrowser);
exit;
?>
How to Construct the img
Tag
To construct the img
tag, you'll need the URL of the Image Proxy script and the location of an image. Here is the format:
<img src="http://example.com/image.php?/images/image.png" style="width:300px; height:100px;" alt="an image">
-
Replace
http://example.com/image.php
with the URL to Image Proxy. -
Replace
/images/image.png
with the location of the image to display. -
The
style
andalt
attributes are likely to need updating.
That's how easy it is to construct the img
tag.
A couple things to note:
-
Image Proxy can handle all images types normally acceptable by browsers, files with these file name extensions:
.jpg .jpeg .png .gif .svg
-
If the file name extension is
.svg
, then the image location must be a relative URL (without protocol and domain name —/directory/image.png
instead ofhttp://example.com/directory/image.png
). The other image types should work even if you specify absolute http:// or https:// URL locations.
Customizing Image Proxy
The Image Proxy source code further above has several consecutive lines colored blue. The blue colored lines may be replaced with custom PHP code to run when an image is requested by the browser.
A potential specialized use listed near the beginning of this article is to serve a different image for a specific IP address, user agent, cookie, or other unique identification.
Here is one way to do it. The example serves a different image if the request is made by Google's image crawler.
In the Image Proxy source code, replace these 7 lines
/* **********************************
Your own PHP code can be placed
here to do whatever needs doing
whenever an image is requested.
(Remove the ******... lines so
your code won't be commented out.)
************************************* */
with these 4 lines
if(strpos($_SERVER['HTTP_USER_AGENT'],'Googlebot-Image')!==false)
{
$ImageFileForBrowser = '/images/alternate-image.png';
}
In those 4 lines, the /images/alternate-image.png image location needs to be replaced with location of the image you want to display only to the Google image bot.
Similarly for other custom PHP code; put the code into that section of the script. Pretty much anything a PHP script can do can be implemented here. (Exceptions would be custom code that prevents the rest of the code from executing.)
Image Proxy will work unmodified, as copied from this article. Customization is optional.
(This article first appeared with an issue of the Possibilities newsletter.)
Will Bontrager