glassfish Posted October 11, 2014 Share Posted October 11, 2014 Does anybody know a good tutorial for creating thumbnails for these three file extensions "jpg, gif and png" in PHP? I am only finding ones for "jpg". Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 11, 2014 Share Posted October 11, 2014 (edited) The tutorials you're have found most likely uses the GD image extension for creating the thumbnails. In that case all you need to do is swap out the jpeg functions to the gif or png equivalent Such as change imagecreatefromjpeg and imagejpeg functions to imagecreatefrompng and imagepng - for png images imagecreatefromgif and imagegif - for gif images Edited October 11, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
glassfish Posted October 11, 2014 Author Share Posted October 11, 2014 (edited) Here I found this script: function make_thumb($src, $dest, $desired_width) { /* read the source image */ $source_image = imagecreatefromjpeg($src); $width = imagesx($source_image); $height = imagesy($source_image); /* find the "desired height" of this thumbnail, relative to the desired width */ $desired_height = floor($height * ($desired_width / $width)); /* create a new, "virtual" image */ $virtual_image = imagecreatetruecolor($desired_width, $desired_height); /* copy source image at a resized size */ imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height); /* create the physical thumbnail image to its destination */ imagejpeg($virtual_image, $dest); } I found it at: http://davidwalsh.name/create-image-thumbnail-php Can this script be modified so all three file extensions can be used? Or I could be using this script three times? I will try that in a minute. Thanks for the answer so far. Edited October 11, 2014 by glassfish Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 11, 2014 Share Posted October 11, 2014 You'd make a separate function which will determine which image functions to use depending on the image extension. Example (untest code). function getImageFunctionsByExtension($src) { $extension = pathinfo($src, PATHINFO_EXTENSION); switch($extension) { case 'gif': $functions = array('imagecreatefromgif', 'imagegif'); // list of gif function names break; case 'png': $functions = array('imagecreatefrompng', 'imagepng'); // list of png function names break; case 'jpg': case 'jpeg': $functions = array('imagecreatefromjpeg', 'imagejpeg'); // list of jpeg function names break; } return $functions; } Now replace $source_image = imagecreatefromjpeg($src); with list($imagecreatefromfunc, $imagefunc) = getImageFunctionsByExtension($src); $source_image = $imagecreatefromfunc($src); And then replace imagejpeg($virtual_image, $dest); with $imagefunc($virtual_image, $dest); The code will now use the correct image functions based on the extension of the image. Quote Link to comment 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.