PHP Code:
//To create thubnail
<?php
/*
Tye (tye at tye . ca)
*/
function create_thumbnail($infile,$outfile,$maxw,$maxh,$stretch = FALSE) {
#clearstatcache();
if (!is_file($infile)) {
trigger_error("Cannot open file: $infile",E_USER_WARNING);
return FALSE;
}
if (is_file($outfile)) {
//trigger_error("Output file already exists: $outfile",E_USER_WARNING);
return FALSE;
}
$functions = array(
'image/png' => 'ImageCreateFromPng',
'image/jpeg' => 'ImageCreateFromJpeg',
);
// Add GIF support if GD was compiled with it
if (function_exists('ImageCreateFromGif')) { $functions['image/gif'] = 'ImageCreateFromGif'; }
$size = getimagesize($infile);
// Check if mime type is listed above
if (!$function = $functions[$size['mime']]) {
trigger_error("MIME Type unsupported: {$size['mime']}",E_USER_WARNING);
return FALSE;
}
// Open source image
if (!$source_img = $function($infile)) {
trigger_error("Unable to open source file: $infile",E_USER_WARNING);
return FALSE;
}
$save_function = "image" . strtolower(substr(strrchr($size['mime'],'/'),1));
// Scale dimensions
list($neww,$newh) = scale_dimensions($size[0],$size[1],$maxw,$maxh,$stretch);
// Create new image
$new_img = imagecreatetruecolor($neww,$newh);
// Copy and resize image
imagecopyresized($new_img,$source_img,0,0,0,0,$neww,$newh,$size[0],$size[1]);
// Save output file
if ($save_function == 'imagejpeg') {
// Change the JPEG quality here
if (!$save_function($new_img,$outfile,75)) {
trigger_error("Unable to save output image",E_USER_WARNING);
return FALSE;
}
} else {
if (!$save_function($new_img,$outfile)) {
trigger_error("Unable to save output image",E_USER_WARNING);
return FALSE;
}
}
chmod($infile,0777);
// Cleanup
imagedestroy($source_img);
imagedestroy($new_img);
return TRUE;
}
// Scales dimensions
function scale_dimensions($w,$h,$maxw,$maxh,$stretch = FALSE) {
if ((!$stretch) && (($w < $maxw) || (!$maxw)) &&
(($h < $maxh) || (!$maxh))) return array($w,$h);
// Scale Height
if ((!$maxw) || (($h > $w) && ($maxh)) ) {
$newh = $maxh;
$neww = floor($w * $newh /$h);
}
// Scale width
elseif ((!$maxh) || (($w >= $h) && ($maxw))) {
$neww = $maxw;
$newh = floor($h * $neww / $w);
} else
// Scale neither
return array($w,$h);
return array($neww,$newh);
}
?>
//To open the foldre and retrieve the images,converting to thumbnail and providing link
<?php
include("thumbnail.php");
if ($handle = opendir('.')) {
$i = 1;
while (false !== ($file = readdir($handle))) {
//if ($file != "." && $file != "..") {
if(substr($file,-3)=='jpg')
{
echo "file" . $file;
$time=time();
$infile = $file;
$outfile= $time . $i .".jpg";
create_thumbnail($infile,$outfile,60,60,$stretch=FALSE);
echo "<a href=$infile><img src=$outfile border=0></a>" . "<br>";
$i=$i+1;
}
//}
}
closedir($handle);
}
?>
Bookmarks