<?php
/*
=============================================================================================
Script Name: randomimage.php4
Verion: 2.0
Author: Ryan Doutt
Date: January 2007
Acknowledge: Ian Anderson - See modification at http://www.hotscripts.com/Detailed/18800.html
which this script is based.
Matthew Dingley - See the original script at
http://www.hotscripts.com/Detailed/22466.html on which this script is based.
This script is a self-contained, random, thumbnail, image generator. It should be called as
follows:
<img SRC="/path/to/randomimage.php4?gd=N&src=path/to/image/dir/&maxw=NNN">
or simply:
<?php include "randomimage.php4"; ?>
where N = the GD library version (supported values are 1 and 2)
NNN = the desired maximum width of the thumbnail
src = path to your image directory where you'd like to select a random image, keep
the trailing /
If the actual image is narrower than the desired maximum width then the original image size
is used for the thumbnail copy.
This script checks for the following errors and generates an error JPEG image accordingly ...
GD version selected neither 1 nor 2;
Image create functions not supported;
Image file not found at the selected location;
GD version 2 functions not supported on the running version of PHP.
To see this script in action please point your favorite browser in the general direction of
one of the following pages:
http://www.1800getryan.com/News_From_Home.html
http://www.1800getryan.com/Overview.html
http://www.1800getryan.com/Travel_and_Adventure.html
This script is available for use as freeware subject to the retention of the preceding
information and acknowledgements in any copy or modification that is made to this code.
v2.0 includes the addition of function fastimagecopyresampled
Acknowledge: Tim Eckel - Date: 12/17/04 - Project: FreeRingers.net - Freely distributable.
=============================================================================================
*/
function GetImagePath ($src) {
$dir=opendir("$src");
$directory=("$src");
$pattern="\.(GIF|JPG|gif|jpg|jpeg|png|bmp|swf)$";
if(!$dir)
{
die("Failed to read directory");
}
$s=readdir($dir);
$count="0";
$image;
while($s)
{
if(ereg($pattern, $s))
{
$image[$count]=$s;
$count++;
}
$s=readdir($dir);
}
closedir($dir);
//Spit it out
$limit=count($image);
$limit--;
$randNum=mt_rand(0,$limit);
return "$directory$image[$randNum]";
}
function ErrorImage ($text) {
global $maxw;
$len = strlen ($text);
if ($maxw < 154) $errw = 154;
$errh = 30;
$chrlen = intval (5.9 * $len);
$offset = intval (($errw - $chrlen) / 2);
$im = imagecreate ($errw, $errh); /* Create a blank image */
$bgc = imagecolorallocate ($im, 153, 63, 63);
$tc = imagecolorallocate ($im, 255, 255, 255);
imagefilledrectangle ($im, 0, 0, $errw, $errh, $bgc);
imagestring ($im, 2, $offset, 7, $text, $tc);
header ("Content-type: image/jpeg");
imagejpeg ($im);
imagedestroy ($im);
exit;
}
function fastimagecopyresampled (&$dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h, $quality = 1) {
// Plug-and-Play fastimagecopyresampled function replaces much slower imagecopyresampled.
// Just include this function and change all "imagecopyresampled" references to "fastimagecopyresampled".
// Typically from 30 to 60 times faster when reducing high resolution images down to thumbnail size using the default quality setting.
// Author: Tim Eckel - Date: 12/17/04 - Project: FreeRingers.net - Freely distributable.
//
// Optional "quality" parameter (defaults is 3). Fractional values are allowed, for example 1.5.
// 1 = Up to 600 times faster. Poor results, just uses imagecopyresized but removes black edges.
// 2 = Up to 95 times faster. Images may appear too sharp, some people may prefer it.
// 3 = Up to 60 times faster. Will give high quality smooth results very close to imagecopyresampled.
// 4 = Up to 25 times faster. Almost identical to imagecopyresampled for most images.
// 5 = No speedup. Just uses imagecopyresampled, highest quality but no advantage over imagecopyresampled.
if (empty($src_image) || empty($dst_image)) { return false; }
if ($quality <= 1) {
$temp = imagecreatetruecolor ($dst_w + 1, $dst_h + 1);
imagecopyresized ($temp, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w + 1, $dst_h + 1, $src_w, $src_h);
imagecopyresized ($dst_image, $temp, 0, 0, 0, 0, $dst_w, $dst_h, $dst_w, $dst_h);
imagedestroy ($temp);
} elseif ($quality < 5 && (($dst_w * $quality) < $src_w || ($dst_h * $quality) < $src_h)) {
$tmp_w = $dst_w * $quality;
$tmp_h = $dst_h * $quality;
$temp = imagecreatetruecolor ($tmp_w + 1, $tmp_h + 1);
imagecopyresized ($temp, $src_image, $dst_x * $quality, $dst_y * $quality, $src_x, $src_y, $tmp_w + 1, $tmp_h + 1, $src_w, $src_h);
imagecopyresampled ($dst_image, $temp, 0, 0, 0, 0, $dst_w, $dst_h, $tmp_w, $tmp_h);
imagedestroy ($temp);
} else {
imagecopyresampled ($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
}
return true;
}
function thumbnail ($gdver, $src, $maxw=190) {
$src = GetImagePath($src);
$gdarr = array (1,2);
for ($i=0; $i<count($gdarr); $i++) {
if ($gdver != $gdarr[$i]) $test.="|";
}
$exp = explode ("|", $test);
if (count ($exp) == 3) {
ErrorImage ("Incorrect GD version!");
}
if (!function_exists ("imagecreate") || !function_exists ("imagecreatetruecolor")) {
ErrorImage ("No image create functions!");
}
$size = @getimagesize ($src);
if (!$size) {
ErrorImage ("Image File Not Found!");
} else {
if ($size[0] > $maxw) {
$newx = intval ($maxw);
$newy = intval ($size[1] * ($maxw / $size[0]));
} else {
$newx = $size[0];
$newy = $size[1];
}
if ($gdver == 1) {
$destimg = imagecreate ($newx, $newy );
} else {
$destimg = @imagecreatetruecolor ($newx, $newy ) or die (ErrorImage ("Cannot use GD2 here!"));
}
if ($size[2] == 1) {
if (!function_exists ("imagecreatefromgif")) {
ErrorImage ("Cannot Handle GIF Format!");
} else {
$sourceimg = imagecreatefromgif ($src);
if ($gdver == 1)
imagecopyresized ($destimg, $sourceimg, 0,0,0,0, $newx, $newy, $size[0], $size[1]);
else
@fastimagecopyresampled ($destimg, $sourceimg, 0,0,0,0, $newx, $newy, $size[0], $size[1]) or die (ErrorImage ("Cannot use GD2 here!"));
ImageAlphaBlending($destimg, true);
$logoImage = ImageCreateFrompng('logo_1800getryan_thumb.PNG');
$logoW = ImageSX($logoImage);
$logoH = ImageSY($logoImage);
$white = imageColorAllocate ($logoImage, 255, 255, 255);
imagecolortransparent($logoImage,$white);
ImageCopyMerge($destimg, $logoImage, $newx-$logoW, $newy-$logoH, 0, 0, $logoW, $logoH, 90);
header ("content-type: image/gif");
imagegif ($destimg);
}
}
elseif ($size[2]==2) {
$sourceimg = imagecreatefromjpeg ($src);
if ($gdver == 1)
imagecopyresized ($destimg, $sourceimg, 0,0,0,0, $newx, $newy, $size[0], $size[1]);
else
@fastimagecopyresampled ($destimg, $sourceimg, 0,0,0,0, $newx, $newy, $size[0], $size[1]) or die (ErrorImage ("Cannot use GD2 here!"));
ImageAlphaBlending($destimg, true);
$logoImage = ImageCreateFrompng('logo_1800getryan_thumb.PNG');
$logoW = ImageSX($logoImage);
$logoH = ImageSY($logoImage);
$white = imageColorAllocate ($logoImage, 255, 255, 255);
imagecolortransparent($logoImage,$white);
ImageCopyMerge($destimg, $logoImage, $newx-$logoW, $newy-$logoH, 0, 0, $logoW, $logoH, 90);
header ("content-type: image/jpeg");
imagejpeg ($destimg);
}
elseif ($size[2] == 3) {
$sourceimg = imagecreatefrompng ($src);
if ($gdver == 1)
imagecopyresized ($destimg, $sourceimg, 0,0,0,0, $newx, $newy, $size[0], $size[1]);
else
@fastimagecopyresampled ($destimg, $sourceimg, 0,0,0,0, $newx, $newy, $size[0], $size[1]) or die (ErrorImage ("Cannot use GD2 here!"));
ImageAlphaBlending($destimg, true);
$logoImage = ImageCreateFrompng('logo_1800getryan_thumb.PNG');
$logoW = ImageSX($logoImage);
$logoH = ImageSY($logoImage);
$white = imageColorAllocate ($logoImage, 255, 255, 255);
imagecolortransparent($logoImage,$white);
ImageCopyMerge($destimg, $logoImage, $newx-$logoW, $newy-$logoH, 0, 0, $logoW, $logoH, 90);
header ("content-type: image/jpeg");
imagejpeg ($destimg);
header ("content-type: image/png");
imagepng ($destimg);
}
else {
ErrorImage ("Image Type Not Handled!");
}
}
imagedestroy ($destimg);
imagedestroy ($sourceimg);
imageDestroy ($logoImage);
}
thumbnail ($_GET["gd"], $_GET["src"], $_GET["maxw"]);
?>
Comments
Wow, this is really nice you
Wow, this is really nice you placed this on the web.
You're welcome! I can tell
You're welcome! I can tell from my logs that people find this site because they are interested in either thumbnailing images, or selecting images at random. Though I wasn't sure if people were having problems getting these scripts up and running, so I configured this "sub-site" to allow interaction and questions. I'll also be posting information on customizing "SimpleViewer". You can check out the current implementation by clicking on the *NEW* Gallery link at the top of the page. It's a customized implementation that works with non-standard directories. Something I figure would be of interest to people who have already implemented a large online photo collection.
Hello, thanks for the
Hello, thanks for the script. I'm still struggling with this part:
/path/to/randomimage.php4?gd=N&src=path/to/image/dir/&maxw=NNN">
Can you give me an example of the structure?
Say it's structured like this:
index.php (homepage)
foto/images/list/selection/ (all images are here)
foto/images/list/selection/randomimage.php (your php script is here)
where index.php and foto folder sit at the route of the site.
Thanks a million
Nelson Cardoso
From your information (foto/
From your information (foto/ is your root directory), it would appear that you might call it this way:
<img SRC="images/list/selection/randomimage.php?gd=2&src=images/list/selection/&maxw=160">That should smack a random image from the directory on the page. The two important parameters are the GD library level and the maximum width. If your server isn't using GD version 2 (hmmm, possible but unlikely) you'll need to change this parameter to gd=1 (instead of 2). The other thing to remember is to change the maxw parameter to a reasonable number, maybe try 160 to begin. Once you get this working, you might want folks to be able to click on the image that brings them to the full gallery (or whatever you'd like). Something like this might be helpful/useful:
<a href="mygallery.html"><img src="images/list/selection/randomimage.php?gd=2&src=images/list/selection/&maxw=160" alt="A random image from my gallery"></a><br><a href="mygallery.html">My Gallery</a>If I have mis-understood let me know. Sounds like index.php is in this directory named foto. If not you might need to change the path information to: foto/images/list/selection/