Jump to content

Archived

This topic is now archived and is closed to further replies.

Guest kilbad

Generating Thumbnails

Recommended Posts

Guest kilbad
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/index.php?id=photos&album=9.24.06.heartwalk&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


Share this post


Link to post
Share on other sites
I am doing this now here is what I am using. I am still writing this so hack away at it

createthumbs.php
[code]<?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;
}
?>[/code]

And here is my index page
[code]<?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);
}
?>[/code]

Still in the works

Ray

Share this post


Link to post
Share on other sites
Guest kilbad
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!

Share this post


Link to post
Share on other sites
This used the gd library so you may not have it loaded.

Ray

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.