PHP Script for img
Tag
It is possible to specify the URL of a PHP script as the src
value of an img
tag. The reason for delivering an image with a PHP script instead of pulling in an image file directly is for additional functionality.
Here is an example image delivered with a PHP script:
And here is the image tag:
<img src="https://www.willmaster.com/library/example/PHPwithIMGtag.php">
The img
tag may have class
, style
, alt
, and other attributes just like it may for an src
URL straight to an image file.
The additional functionality the technique makes available is anything a PHP script can do. They include:
-
Logging the request IP address.
-
Reading and/or setting a cookie.
-
Sending an email to yourself when the image loads.
-
Limit the number of times the image can be shown.
If there is anything additional you want done when the image is published (except send non-image content to the browser), then this method is likely to work for you.
How the method works is that the PHP script sends an image header to the browser and delivers the image. But before it does that, it does whatever other chores you code it to do.
Later in the article, I'll include code you may use to send an email to yourself when an image is delivered. Also code to log the event upon delivering an image.
You may use your own code for your own functionality.
The Image-Delivering PHP Script
Here is the source code for the PHP script. It has minimum coding, with the place marked where you add your own code.
<?php /* PHP to Deliver Image Version 1.0 October 12, 2019 Will Bontrager Software LLC https://www.willmaster.com/ */ /* Specify image URL. */ $image = 'https://example.com/images/animage.gif'; /* Insert your custom code below here. */ /* Image delivery section. */ preg_match('/\.([^\.])+$/',$image,$match); $type = strtolower($match[1]); if($type=='jpg') { $type = "jpeg"; } header("Content-type: image/$type"); echo( file_get_contents($image) ); exit; ?>
https://example.com/images/animage.gif
is the URL to the image that the PHP script will deliver to the browser. Change the URL to the image you want to deliver on your website.
Below the /* Insert your custom code below here. */
line and above the /* Image delivery section. */
line is where you put any custom code for your PHP script to do just before it delivers the image. (It may be prudent to install the script and verify it delivers the image to your img
tag before inserting code for custom functionality.)
Upload the script to your server and make a note of its URL. For references in this article, I'll assume PHPwithIMGtag.php
is the file name you gave the script. And I'll assume https://example.com/PHPwithIMGtag.php
is the URL to the script.
With the PHP script on the server, put an image tag on a test web page.
<img src="https://example.com/PHPwithIMGtag.php">
Replace https://example.com/PHPwithIMGtag.php
with the URL of your PHP script and you're ready to verify it works as it should.
If no image shows up on the web page, it might be an error in the PHP script (check your error logs for clues) or it might be a mis-typing of the URL to the image (test the URL by copying and pasting into the browser address bar).
When you have the basic system working, here are a couple customizations you may wish to try before you do your own.
PHP Image-Delivering Script Customizations
Customization code gets inserted at the point marked in the PHP script source code — below the /* Insert your custom code below here. */
line and above the /* Image delivery section. */
line.
This first customization is one line of code to send you an email whenever the PHP script delivers an image. The email will contain the values in the PHP $_SERVER
array (several dozen values).
mail('name@example.com','image script',print_r($_SERVER,true),'From: name@example.com');
Change name@example.com
in both places to the email address where the email shall be delivered to.
Insert it into the PHP script at the point marked in the script source code.
The second customization logs some information in a plain text file on the server whenever the PHP script delivers an image.
if( ! ini_get('date.timezone') ) { date_default_timezone_set('UTC'); } $Logline = array(); $Logline[] = time(); $Logline[] = date('l, F j, Y \a\t H:i:s'); $Logline[] = $_SERVER['REMOTE_ADDR']; $Logline[] = $_SERVER['HTTP_USER_AGENT']; file_put_contents('./IMGlog_'.date('Y-m').'.txt',implode("\t",$Logline)."\n",FILE_APPEND);
The above code sets the timezone to UTC (like GMT) if your PHP doesn't have a timezone already set. Then it constructs information for a log line:
-
The Unix time stamp.
1733253570 -
The current date and time in long form.
Tuesday, December 3, 2024 at 19:19:30 -
The IP address of the browser.
3.147.6.176 -
The user agent string of the browser.
Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
The information is tab-separated on one line. It is stored in a file named IMGlog_2024-12.txt (with 2024-12 being the current year and day numbers). The file is in the same directory as the PHP script.
When you let your imagination wander, it is likely to stumble upon some exciting ideas to implement with this software. (An idea my imagination just stumbled upon is to change the image URL depending on the URL of the web page that has the img
tag, or depending on time of year, or day of week.)
OK, I can't just let that idea lay.
Here is a bonus customization, a line of code that will change the image URL when the current date is a Sunday:
if(date('l')=='Sunday') { $image = 'https://example.com/images/sundayimage.gif'; }
(The value in the date() function is the lower-case letter "l" — lower-case of "L" — not the digit "1".)
In the above code, replace https://example.com/images/sundayimage.gif
with the URL to the image you want to publish on Sundays. Insert it into the PHP script at the point marked for custom source code.
Then, on Sundays, the value of the $image
variable, originally initialized further up in the PHP script, is overwritten with the Sunday-image URL. And the Sunday image is what publishes.
(This article first appeared with an issue of the Possibilities newsletter.)
Will Bontrager