freddykhalid Posted April 16, 2006 Share Posted April 16, 2006 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.jpgI 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.jpgwhere _thb_*.jpg are the thumbnails.Please help. Quote Link to comment https://forums.phpfreaks.com/topic/7558-create-and-save-thumbnails/ Share on other sites More sharing options...
kenrbnsn Posted April 16, 2006 Share Posted April 16, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/7558-create-and-save-thumbnails/#findComment-27567 Share on other sites More sharing options...
freddykhalid Posted April 17, 2006 Author Share Posted April 17, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/7558-create-and-save-thumbnails/#findComment-27666 Share on other sites More sharing options...
redbullmarky Posted April 17, 2006 Share Posted April 17, 2006 hithe 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.cheersMark[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. Quote Link to comment https://forums.phpfreaks.com/topic/7558-create-and-save-thumbnails/#findComment-27669 Share on other sites More sharing options...
freddykhalid Posted April 17, 2006 Author Share Posted April 17, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/7558-create-and-save-thumbnails/#findComment-27707 Share on other sites More sharing options...
freddykhalid Posted April 17, 2006 Author Share Posted April 17, 2006 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 178Does anyone know how to solve this problem for TIF? Quote Link to comment https://forums.phpfreaks.com/topic/7558-create-and-save-thumbnails/#findComment-27730 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.