Jump to content


Photo

Create and save Thumbnails


  • Please log in to reply
5 replies to this topic

#1 freddykhalid

freddykhalid
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 16 April 2006 - 07:09 PM

Hey guys, is there an easy way to copy and convert all the images within a directory to thumbnails and save them?

Scenario:

Images Directory: 1.jpg , 2.jpg

I want to have thumbnails with lower quality as well in the same directory and eventually:

Images Directory: 1.jpg , _thb_1.jpg , 2.jpg , _thb_2.jpg


where _thb_*.jpg are the thumbnails.

Please help.

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 16 April 2006 - 09:24 PM

Look at the [a href=\"http://www.php.net/glob\" target=\"_blank\"]glob()[/a] function and the [a href=\"http://www.php.net/image\" target=\"_blank\"]image[/a] functions. Using the examples in the manual should give you some ideas and let you code it yourself.

If, once you have a script, you have problems, post here again with the problems and include the script that you wrote. Then we will help you solve your problems.

Ken

#3 freddykhalid

freddykhalid
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 17 April 2006 - 11:27 AM

I was able to successfully do what I intended to do using the following function for JPEG, PNG, GIF But I wasn't able to do so for BMP and TIF. The code is the following:
<?


function thumb($filename, $destination, $th_width, $th_height, $forcefill)
{   
   list($width, $height) = getimagesize($filename);

   if (!($source = @imagecreatefromjpeg($filename))) {
    if (!($source = @imagecreatefrompng($filename))) {
        if (!($source = @imagecreatefromgif($filename))) {    
            if (!($source = @imagecreatefromwbmp($filename))) {
                $source = @imagecreate($filename); } } } }
   if($width > $th_width || $height > $th_height){
     $a = $th_width/$th_height;
     $b = $width/$height;

     if(($a > $b)^$forcefill)
     {
         $src_rect_width  = $a * $height;
         $src_rect_height = $height;
         if(!$forcefill)
         {
           $src_rect_width = $width;
           $th_width = $th_height/$height*$width;
         }
     }
     else
     {
         $src_rect_height = $width/$a;
         $src_rect_width  = $width;
         if(!$forcefill)
         {
           $src_rect_height = $height;
           $th_height = $th_width/$width*$height;
         }
     }

     $src_rect_xoffset = ($width - $src_rect_width)/2*intval($forcefill);
     $src_rect_yoffset = ($height - $src_rect_height)/2*intval($forcefill);

     $thumb  = imagecreatetruecolor($th_width, $th_height);
     imagecopyresized($thumb, $source, 0, 0, $src_rect_xoffset, $src_rect_yoffset, $th_width, $th_height, $src_rect_width, $src_rect_height);

     imagejpeg($thumb,$destination);
   }
}
?>




An error occurs in the line when I try to apply the function for bmp or tif. The function then outputs a black picture.

imagecopyresized($thumb, $source, 0, 0, $src_rect_xoffset, $src_rect_yoffset, $th_width, $th_height, $src_rect_width, $src_rect_height);



Warning: imagecopyresized(): supplied argument is not a valid Image resource in c:\wamp\www\Test\Admin\Uploads\upload.php on line 185

#4 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 17 April 2006 - 11:43 AM

hi
the error would be occuring as you have no check in the event that the picture is NEITHER format. add an extra trap to cater for the event that the picture is not of any valid format.
cheers
Mark

edit:also, this bit wont work anyway as far as i know:

@imagecreate($filename);
as 'imagecreate' requires a width/height, not a filename.
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#5 freddykhalid

freddykhalid
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 17 April 2006 - 03:14 PM

Hi again....
I'm trying to use phpThumbs downloaded from: [a href=\"http://phpthumb.sourceforge.net/\" target=\"_blank\"]http://phpthumb.sourceforge.net/[/a]

Still no result but still didn't lose hope !!!

Anyone has an answer for this?


<?


function thumb($filename, $destination, $th_width, $th_height, $forcefill)
{   
   list($width, $height) = getimagesize($filename);

   if (!($source = @imagecreatefromjpeg($filename))) {
    if (!($source = @imagecreatefrompng($filename))) {
        if (!($source = @imagecreatefromgif($filename))) {    
            if (!include_once('phpthumbs.bmp.php')) {
oops("You do not appear to have phpthumbs.bmp.php installed in the same path as upload.php. Please add this file, or don't try to upload .bmp files into your image directory!"); }
    

$phpThumbBMP = new phpthumb_bmp();



$source = @imagecreatefromgd2($phpThumbBMP->phpthumb_bmpfile2gd($filename)); 
            


}
         }       
                   }
  
   if($width > $th_width || $height > $th_height){
     $a = $th_width/$th_height;
     $b = $width/$height;

     if(($a > $b)^$forcefill)
     {
         $src_rect_width  = $a * $height;
         $src_rect_height = $height;
         if(!$forcefill)
         {
           $src_rect_width = $width;
           $th_width = $th_height/$height*$width;
         }
     }
     else
     {
         $src_rect_height = $width/$a;
         $src_rect_width  = $width;
         if(!$forcefill)
         {
           $src_rect_height = $height;
           $th_height = $th_width/$width*$height;
         }
     }

     $src_rect_xoffset = ($width - $src_rect_width)/2*intval($forcefill);
     $src_rect_yoffset = ($height - $src_rect_height)/2*intval($forcefill);

     $thumb  = imagecreatetruecolor($th_width, $th_height);
     imagecopyresized($thumb, $source, 0, 0, $src_rect_xoffset, $src_rect_yoffset, $th_width, $th_height, $src_rect_width, $src_rect_height);

     imagejpeg($thumb,$destination);
   }
}
?>


#6 freddykhalid

freddykhalid
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 17 April 2006 - 03:54 PM

I managed to get the BMP working, That should be enough.

But 1 more thing, when I try this for a TIF picture this error comes up:

Warning: imagecopyresized(): supplied argument is not a valid Image resource in c:\wamp\www\Test\Admin\Uploads\upload.php on line 178

Does anyone know how to solve this problem for TIF?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users