Some help!?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ceaserone
Forum Newbie
Posts: 3
Joined: Fri Jun 09, 2006 2:13 pm

Some help!?

Post 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:
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

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

Post 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 :)
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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;
}

?>
Post Reply