Php Programming Code Examples
Php > Look and Feel Code Examples
JPEG Directory thumbnail system
JPEG Directory thumbnail system
thumbs.php
<?php
/*
Will generate a pages width of
thumbnailed GIF's and JPG's
from a secified directory. Will work
in a directory with other files in it
with no problems.
Thumbnail variables are set in gen.php
*/
// Comments will be throughout
// Set variables
$default_dir = "./images"; // Relative to current location
// Directory Scan
if(!($dp = opendir($default_dir))) die("Cannot open $dir");
// Place images into image tag
while($file = readdir($dp)){
if($file != '.' && $file != '..' && stristr($file,"jpg")) echo "<a href=\"$default_dir/$file\"><img src=\"./gen.php?$default_dir/$file\"></a>\r\n";
}
closedir($dp);
?>
end thumbs.php
gen.php
<?php
/*
Will generate a pages width of
thumbnailed GIF's and JPG's
from a secified directory. Will work
in a directory with other files in it
with no problems. Part 2. The image
creator.
Aspect ratio is not supported.
*/
// Comments will be throughout
$t_width = 100; // Thumbnail width
$t_height = 100; // Thumbnail height
// get and display jpeg images
if(stristr($QUERY_STRING,".jpg")){
header("Content-type: image/jpeg");
$srcimage = imagecreatefromjpeg($QUERY_STRING);
$destimage = imagecreate($t_width,$t_height);
$width = imageSX($srcimage);
$height = imageSY($srcimage);
imagecopyresized ($destimage,$srcimage,0,0,0,0,$t_width,$t_height,$width,$height);
ImageJPEG($destimage);
ImageDestroy($destimage);
}
/*
//Support for GIF unavailable at this time
elseif(stristr($QUERY_STRING,".gif")){
header("Content-type: image/gif");
$srcimage = imagecreatefromgif($QUERY_STRING);
$destimage = imagecreate(100,100);
$width = imageSX($srcimage);
$height = imageSY($srcimage);
imagecopyresized ($destimage,$srcimage,0,0,0,0,$t_width,$t_height,$width,$height);
ImageGIF($destimage);
ImageDestroy($destimage);
}
*/
// on a problem geneterate an image with ERROR in it
else {
$im = imagecreate(100,100);
$blue = imagecolorallocate($im,0,0,200);
$red = imagecolorallocate($im,255,0,0);
imagestring($im,2,2,5,"ERROR",$red);
imageGIF($im);
imagedestroy($im);
}
?>