PHP – Random image
First published on Ka of Isis 140502
Note: As github.io
(where this website lives) does not support PHP, the scripts can not be shown with live examples.
Credit
This work is based on Marco Kuiper’s article PHP Random Image Rotation. As such, there is little new to learn here. A few small modifications, or localizations, rather, have been made to his external rotate.php
script, so please download the original for differing uses. On his web site, Kuiper also explains how to use the program for more than just images.
Update: It turns out that Kuiper (without reference) only echoes information in an earlier article, Random Image Rotation, by Dan Benjamin. Refer to that for an in-depth discussion. Benjamin has later built on his script in a follow-up article, A Better Image Rotator.
Two approaches are discussed in this here article. The first uses the PHP file as per Kuiper. The second method finds a modified PHP script internal to the HTML. The latter does away with an additional server request to read the script, thus saving bandwith.
HTML with reference to external PHP
<img src="random.php" alt="Random image">
If the script is not stored in the same directory as the calling file, use:
<img src="/path/from/root/to/random.php" alt="Random image">
Important: The script itself craves a path to the images via the $folder
variable. The default works out of the box when images and srcipt live in the same directory. The external script’s built-in error detection routine depends on the script being referenced as the source (aka src
) in an image tag. As it stands, it it not possible to use the same script for different image tags on the same page as this would serve an identical image for all the tags. Note also that the modified file has been renamed random.php
for Impossiblue.
PHP “internally” to HTML
$folder = ".";
$extList = array();
$extList["gif"] = "image/gif";
$extList["jpg"] = "image/jpeg";
$extList["png"] = "image/png";
$img = null;
if(substr($folder,-1) != "/") {
$folder = $folder."/";
}
if(isset($_GET["img"])) {
$imageInfo = pathinfo($_GET["img"]);
if(isset($extList[strtolower($imageInfo["extension"])]) && file_exists($folder.$imageInfo["basename"])) {
$img = $folder.$imageInfo["basename"];
}
} else {
$fileList = array();
$handle = opendir($folder);
while(false !== ($file = readdir($handle))) {
$file_info = pathinfo($file);
if(isset($extList[strtolower($file_info["extension"])])) {
$fileList[] = $file;
}
}
closedir($handle);
if(count($fileList) > 0) {
$imageNumber = time() % count($fileList);
$img = $folder.$fileList[$imageNumber];
}
}
if($img!=null) {
$imageInfo = pathinfo($img);
$contentType = "Content-type: ".$extList[$imageInfo["extension"]];
header($contentType);
echo '<img src="'.$img.'" alt="Random image">';
} else echo "Hmm… no image available.";
The original, elaborate error routine (excerpt below)…
else {
if ( function_exists('imagecreate') ) {
header ("Content-type: image/png");
$im = @imagecreate (100, 100)
or die ("Cannot initialize new GD image stream");
$background_color = imagecolorallocate ($im, 255, 255, 255);
$text_color = imagecolorallocate ($im, 0,0,0);
imagestring ($im, 2, 5, 5, "IMAGE ERROR", $text_color);
imagepng ($im);
imagedestroy($im);
}
}
…can be replaced with a simple text message:
else echo "Hmm… no image available.";
The message can be localized to suit the overall page design:
else echo '<p class="quote"><span class="nb">Hmm… </span>no image available.</p>';
Here on Impossiblue (had PHP been available) this would yield:
Hmm… no image available.
Note: The UTF-8 ellipsis (…) can not be employed in the external PHP script because text rendering for PHP-generated PNG images is restricted to what is informally referred to as Latin-2 encoding.
Any image with a gif-, jpg- or png-suffix would be considered on this page at Impossiblue, but jpeg
(shudder) or bmp
(smells like Windows), for instance, are supported with proper references to image types in the PHP script:
$extList["jpeg"] = "image/jpeg";
$extList["bmp"] = "image/bmp";