Jump to content

Create and save Thumbnails


freddykhalid

Recommended Posts

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.
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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:
[code]
<?


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);
   }
}
?>[/code]




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
Link to comment
Share on other sites

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

[b]edit:[/b]also, this bit wont work anyway as far as i know:

[code]
@imagecreate($filename);
[/code]as 'imagecreate' requires a width/height, not a filename.
Link to comment
Share on other sites

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?


[code] <?


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);
   }
}
?>[/code]
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.