This script pumps out a random image from a specified folder and resizes it as a thumbnail.

To execute it, you simply enter the following code in html
<img src="http://website.com/images/refresh.php" width="200" border="1">

All is well, except, there is no url to the larger/original image. Its just the resized image.

Is there anyway to have the original image url/filename provided so its not only an image, but a linked image to the original image?

I'm just not sure where to go from here. :quiet:

<? 

// Opens the current folder
$folder=opendir("."); 

// Stores all the files in the directory and counts their number
while ($file = readdir($folder)) 
$names[count($names)] = $file; 

// Closes the directory
closedir($folder);

// Sorts the names alphabetically
sort($names);

// Removes all non-image files
$tempvar=0;
for ($i=0;$names[$i];$i++){
$ext=strtolower(substr($names[$i],-4));
if ($ext==".jpg"||$ext==".gif"||$ext=="jpeg"||$ext==".png"){$names1[$tempvar]=$names[$i];$tempvar++;}
}

// Choses one random image
srand ((double) microtime() * 10000000);
$rand_keys = array_rand ($names1, 2);
$slika=$names1[$rand_keys[0]]; 

// Checks if you've choosen to resize the image
if (!isset($tnsize)) {

// If not, it displays the choosen image
	header ("Location: $slika");

} else {

// Checks if the given size is valid
	$tnsize = (integer) $tnsize;
	if (($tnsize<20) or ($tnsize>300)){

// If not, it creates an error image and displays it
		 $img = ImageCreate(100, 100);
		 $red = ImageColorAllocate($img, 255, 0, 0);
		 $yellow = ImageColorAllocate($img, 255,255, 0);
		 ImageString($img, 4, 20, 20, "Dimensione", $yellow);
		 ImageString($img, 4, 20, 40, "fornita", $yellow);
		 ImageString($img, 4, 20, 60, "errata!", $yellow);
		 ImagePNG($img);
		 ImageDestroy($img);
		 exit();
	}

// We're now sure the image is valid. Now we have to resize it, we're about to use GD libraries.

// It now checks the image format and loads it into a variable
	if ($ext==".jpg" || $ext=="jpeg"){
		$bigimage = @ImageCreateFromJPEG($slika);
	}
	if ($ext==".gif"){
		$bigimage = @ImageCreateFromGIF($slika);
	}
	if ($ext==".png" || $ext=="jpeg"){
		$bigimage = @ImageCreateFromPNG($slika);
	}

// It now creates an empty image of the given size

$tnimage = ImageCreate($tnsize, $tnsize);
$darkblue = ImageColorAllocate($tnimage, 0,0, 127);
ImageColorTransparent($tnimage,$darkblue);

// It now calculates the resizing image factor
	$sz = GetImageSize($slika);
	$x = $sz[0];
	$y = $sz[1];
	if ($x>$y) {
		 $dx = 0;
		 $w = $tnsize;
		 $h = ($y / $x) * $tnsize;
		 $dy = ($tnsize - $h) / 2;
	}else{
		 $dy = 0;  
$h = $tnsize; $w = ($x / $y) * $tnsize; $dx = ($tnsize - $w) / 2; } // Resizes the image ImageCopyResized($tnimage, $bigimage, $dx, $dy, 0, 0, $w, $h,$x, $y); // Displays the image ImagePNG($tnimage); // Clears the variables ImageDestroy($tnimage); ImageDestroy($bigimage); } ?>

    no, as it simply displays an image you would have to wrap the img html in a link with the image url, but you don't know what image is chosen. not a good idea to write the thumbnail on the fly every time either better to create the file

      Write a Reply...