Some help!?
Moderator: General Moderators
Some help!?
Can anyone tell me a good free php script to generate thumbnails out of a dir of images I cant find one anywhere 
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
I think webGENEius produced GOOP Gallery. I have never used it (though I am planning to).
-
alex.barylski
- DevNet Evangelist
- Posts: 6267
- Joined: Tue Dec 21, 2004 5:00 pm
- Location: Winnipeg
Here's one possible way to do it. This resizes all images to the same dimmensions irrespective of aspect ratio. This is great for when you want all your thumbs the same size for a gallery or something but might not be what you wanted. Also it outputs them all as jpeg irrespective of input format. Give it a try and if it's not what you want give a more in depth account of your needs.
Code: Select all
<?php
$source_directory = './images/';
$thumbnail_directory = './thumbs/';
convertImageDirectoryToThumbnails($source_directory, $thumbnail_directory);
function convertImageDirectoryToThumbnails($source, $destination)
{
ini_set('max_execution_time', 300);
if ($handle = opendir($source))
{
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != "..")
{
if(preg_match('/^(.*)(\.(?:jpg|jpeg|gif|png))$/i', $file, $match))
{
echo "$file..."; flush();
ResizeSemiAbstractTop($source.$file, $destination.$match[1].'_thumb.jpg');
echo " done!<br>\n"; flush();
}
}
}
closedir($handle);
echo 'All images converted';
}
}
function ResizeSemiAbstractTop($source, $destination = NULL, $w = 120, $h = 90, $quality =75)
{
if(!($source_image = @imagecreatefromstring(@file_get_contents($source))))
{
return false;
}
$sw = imagesx($source_image);
$sh = imagesy($source_image);
$ar = $sw/$sh;
$tar = $w/$h;
if($ar >= $tar)
{
$x1 = round(($sw - ($sw * ($tar/$ar)))/2);
$x2 = round($sw * ($tar/$ar));
$y1 = 0;
$y2 = $sh;
}
else
{
$x1 = 0;
$y1 = 0;
$x2 = $sw;
$y2 = round($sw/$tar);
}
$slate = @imagecreatetruecolor($w, $h);
imagecopyresampled($slate, $source_image, 0, 0, $x1, $y1, $w, $h, $x2, $y2);
$destination or header('Content-type: image/jpeg');
@imagejpeg($slate, $destination, $quality) or die('Directory permission problem');
ImageDestroy($slate);
ImageDestroy($source_image);
$destination or die;
}
?>