Page 1 of 1

Some help!?

Posted: Fri Jun 09, 2006 2:16 pm
by ceaserone
Can anyone tell me a good free php script to generate thumbnails out of a dir of images I cant find one anywhere :oops:

Posted: Fri Jun 09, 2006 2:35 pm
by RobertGonzalez
I think webGENEius produced GOOP Gallery. I have never used it (though I am planning to).

Posted: Fri Jun 09, 2006 2:39 pm
by alex.barylski
Who is it on here that has that GOOP as a link in their sig???

I looked at that the other day and was just about to suggest it :)

You beat me to it :)

Posted: Fri Jun 09, 2006 4:06 pm
by bokehman
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;
}

?>