Software, your way.
burger menu icon
WillMaster

WillMaster > LibraryWeb Page and Site Features

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

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">
  1. Replace http://example.com/image.php with the URL to Image Proxy.

  2. Replace /images/image.png with the location of the image to display.

  3. The style and alt 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 of http://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

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC