Jump to content


Generating Thumbnails


  • Please log in to reply
3 replies to this topic

#1 Guest_kilbad_*

Guest_kilbad_*
  • Guests

Posted 25 September 2006 - 05:52 PM

I have a script that I wrote that allows me to display all the photos in any particular directory on several pages with pagination.  Currently, the photos are displayed four on a page, and, if you want to see a larger size, you can click on any particular photo (see:: http://kilbad.com/in...eartwalk&page=1 ).  However, the script uses no thumbnails, and I know that it needs to because of loading speed issues (you'll notice if you visit that page that it is quite slow).

Only the original photos are in the directory, no thumbnails.  Therefore, here is my question..  how can I automatically generate thumbnails of the photos I have uploaded?  Is there a way to generate them temporarily each time the script runs?  Or is there a script that I can code, and run once to generate thumbnails when I initially upload the photos?

Basically, I want to be able to make thumbnails with PHP in an automated way so I do not have to do each one individually in photoshop (or another program).

I am not sure if this is relevant, but just fyi, the sever I am on has Image Magick and NetPBM.  Also, any other feedback about my site, or the link I provided above, would be greatly appreciated too!

Thanks again for all the help in the past!

Brendan




#2 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 25 September 2006 - 06:04 PM

I am doing this now here is what I am using. I am still writing this so hack away at it

createthumbs.php
<?php
$imagefolder='pics/';
$thumbsfolder='thumbs/';
$pics=directory($imagefolder,"jpg,JPG,JPEG,jpeg,png,PNG");
$pics=ditchtn($pics,"tn_");
if ($pics[0]!="")
{
	foreach ($pics as $p)
	{
		createthumb($imagefolder.$p,$thumbsfolder.'tn_'.$p,150,150);
	}
}

/*
	Function ditchtn($arr,$thumbname)
	filters out thumbnails
*/
function ditchtn($arr,$thumbname)
{
	foreach ($arr as $item)
	{
		if (!preg_match("/^".$thumbname."/",$item)){$tmparr[]=$item;}
	}
	return $tmparr;
}

/*
	Function createthumb($name,$filename,$new_w,$new_h)
	creates a resized image
	variables:
	$name		Original filename
	$filename	Filename of the resized image
	$new_w		width of resized image
	$new_h		height of resized image
*/
function createthumb($name,$filename,$new_w,$new_h)
{
	$system=explode(".",$name);
	if (preg_match("/jpg|jpeg/",$system[1])){$src_img=imagecreatefromjpeg($imagefolder.$name);}
	if (preg_match("/png/",$system[1])){$src_img=imagecreatefrompng($imagefolder.$name);}
	$old_x=imageSX($src_img);
	$old_y=imageSY($src_img);
	if ($old_x > $old_y)
	{
		$thumb_w=$new_w;
		$thumb_h=$old_y*($new_h/$old_x);
	}
	if ($old_x < $old_y) 
	{
		$thumb_w=$old_x*($new_w/$old_y);
		$thumb_h=$new_h;
	}
	if ($old_x == $old_y) 
	{
		$thumb_w=$new_w;
		$thumb_h=$new_h;
	}
	$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
	imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
	if (preg_match("/png/",$system[1]))
	{
		imagepng($dst_img,$filename); 
	} else {
		imagejpeg($dst_img,$filename); 
	}
	imagedestroy($dst_img); 
	imagedestroy($src_img);
}

/*
        Function directory($directory,$filters)
        reads the content of $directory, takes the files that apply to $filter 
		and returns an array of the filenames.
        You can specify which files to read, for example
        $files = directory(".","jpg,gif");
                gets all jpg and gif files in this directory.
        $files = directory(".","all");
                gets all files.
*/
function directory($dir,$filters)
{
	$handle=opendir($dir);
	$files=array();
	if ($filters == "all"){while(($file = readdir($handle))!==false){$files[] = $file;}}
	if ($filters != "all")
	{
		$filters=explode(",",$filters);
		while (($file = readdir($handle))!==false)
		{
			for ($f=0;$f<sizeof($filters);$f++):
				$system=explode(".",$file);
				if ($system[1] == $filters[$f]){$files[] = $file;}
			endfor;
		}
	}
	closedir($handle);
	return $files;
}
?>

And here is my index page
<?php
include("createthumbs.php");
$dir = "pics/";
if ($handle = opendir($dir)) {
   while (false !== ($file = readdir($handle))) {
     if(substr($file, 0, 1) == "."){
     } else {
            print '<a href="'.$dir.$file.'"><img src="thumbs/tn_'.$file.'"><a><br /><br>';
            }
        }
   closedir($handle);
}
?>

Still in the works

Ray

#3 Guest_kilbad_*

Guest_kilbad_*
  • Guests

Posted 26 September 2006 - 01:38 PM

I tried your script and got many "not valid Image resource" errors (see: http://kilbad.com/DEVELOPMENT/ ).  Any suggestions?  Do I need to reference the location, or load, or somehow refer to the sever's Image Magick and/or NetPBM packages?  Do those have anything to do with this at all?

Also, is there any other way to do this so that thumbnails are just temporarily loaded when the script runs?

Additionally, if anyone else has any suggestions for me, please add them!

Thanks for all the help!

#4 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 26 September 2006 - 04:51 PM

This used the gd library so you may not have it loaded.

Ray




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users