Some help!?
Posted: Fri Jun 09, 2006 2:16 pm
Can anyone tell me a good free php script to generate thumbnails out of a dir of images I cant find one anywhere 
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
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;
}
?>