• PHP GD Image Resizing and/or Cropping... Done many times, but never perfect. Well here's a function
    5 replies, posted
Hello, hello. So many damn image resizing functions and classes out there, they all seem to be flawed one way or another. So I decided to make my own. This isn't perfect either, however. There's no transparency support as of yet. But for everything else it'll be fine. [URL]http://atomiku.com/2012/11/php-function-for-resizing-and-cropping-images-with-gd/[/URL] Feel free to add the transparency support in yourself, it'd only take 5 minutes - I'm just being a real lazy b'stard. Well I hope this ends up helping somebody. Peace out! Quick Edit: Incase you can't figure out the params, the useage info is on the atomiku.com link I posted. This function is perfect for scaling down to a ratio, then cropping to a <b>fixed</b> width and height. OR: If you want to resize just by a width, and the height will just be the same ratio. [PHP] function resize_image($file, $destination, $w, $h) { //Get the original image dimensions + type list($source_width, $source_height, $source_type) = getimagesize($file); //Figure out if we need to create a new JPG, PNG or GIF $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION)); if ($ext == "jpg" || $ext == "jpeg") { $source_gdim=imagecreatefromjpeg($file); } elseif ($ext == "png") { $source_gdim=imagecreatefrompng($file); } elseif ($ext == "gif") { $source_gdim=imagecreatefromgif($file); } else { //Invalid file type? Return. return; } //If a width is supplied, but height is false, then we need to resize by width instead of cropping if ($w && !$h) { $ratio = $w / $source_width; $temp_width = $w; $temp_height = $source_height * $ratio; $desired_gdim = imagecreatetruecolor($temp_width, $temp_height); imagecopyresampled( $desired_gdim, $source_gdim, 0, 0, 0, 0, $temp_width, $temp_height, $source_width, $source_height ); } else { $source_aspect_ratio = $source_width / $source_height; $desired_aspect_ratio = $w / $h; if ($source_aspect_ratio > $desired_aspect_ratio) { /* * Triggered when source image is wider */ $temp_height = $h; $temp_width = ( int ) ($h * $source_aspect_ratio); } else { /* * Triggered otherwise (i.e. source image is similar or taller) */ $temp_width = $w; $temp_height = ( int ) ($w / $source_aspect_ratio); } /* * Resize the image into a temporary GD image */ $temp_gdim = imagecreatetruecolor($temp_width, $temp_height); imagecopyresampled( $temp_gdim, $source_gdim, 0, 0, 0, 0, $temp_width, $temp_height, $source_width, $source_height ); /* * Copy cropped region from temporary image into the desired GD image */ $x0 = ($temp_width - $w) / 2; $y0 = ($temp_height - $h) / 2; $desired_gdim = imagecreatetruecolor($w, $h); imagecopy( $desired_gdim, $temp_gdim, 0, 0, $x0, $y0, $w, $h ); } /* * Render the image * Alternatively, you can save the image in file-system or database */ if ($ext == "jpg" || $ext == "jpeg") { ImageJpeg($desired_gdim,$destination,100); } elseif ($ext == "png") { ImagePng($desired_gdim,$destination); } elseif ($ext == "gif") { ImageGif($desired_gdim,$destination); } else { return; } ImageDestroy ($desired_gdim); } [/PHP]
Thank you!
Don't you also need to free up the resources for the original source image you created from file? [php] ImageDestroy ($source_gdim);[/php]
PHP case sensitivity is so funky.
[QUOTE=PsyKzz;38916631]Don't you also need to free up the resources for the original source image you created from file? [php] ImageDestroy ($source_gdim);[/php][/QUOTE] In a way, you are correct.. but PHP will clean up everything once the script has finished anyway. I'm not sure how PHP's garbage collection works but, it may even destroy the image after the function has returned because there's no way the variable could be accessed anymore.
Thanks for this! [editline]19th January 2013[/editline] It'd be neat if you added a parameter to only resize if it's greater than the size passed (ie. you want your image to be resized to 200px, but only if it's 201+px) [editline]19th January 2013[/editline] Actually never mind, that's already kinda there, thanks again!
Sorry, you need to Log In to post a reply to this thread.