Vanilla 1 is no longer supported or maintained. If you need a copy, you can get it here.

PHP Script - Resize JPG

edited May 2007 in Vanilla 1.0 Help
Anyone know of a good PHP script that is able to resize a JPEG?

I found a couple of popular ones, popular because they keep showing up in searches, but they either corrupt the image every so often or result in crummy quality.

Posted: Wednesday, 16 May 2007 at 8:02PM


  • i'm assuming u have tried phpthumb
  • Yes mate, phpthumb works well on the fly, but I need a script that permanently resizes a JPG in proportion so I can move it into a directory where it will be used as a table background.

    Posted: Wednesday, 16 May 2007 at 10:24PM

  • edited May 2007
    Have u tried as well. If yes then time to roll ur own. U just want to resize a jpg file thats it. nothing more.

    Use imagecopyresized function. Its straight forward and there is a image resize of jpeg example posted on
    Also check imagecopyresampled. they have an example of resizing via max width, or max height.
    this is ur best route. lot of people have posted code in the comments. They have exactly what ur looking for.
    Since its jpeg u will have to use imagecreatetruecolor to maintain quality

    From Comments
    <? function fastimagecopyresampled (&$dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h, $quality = 3) { // Plug-and-Play fastimagecopyresampled function replaces much slower imagecopyresampled. // Just include this function and change all "imagecopyresampled" references to "fastimagecopyresampled". // Typically from 30 to 60 times faster when reducing high resolution images down to thumbnail size using the default quality setting. // Author: Tim Eckel - Date: 12/17/04 - Project: - Freely distributable. // // Optional "quality" parameter (defaults is 3). Fractional values are allowed, for example 1.5. // 1 = Up to 600 times faster. Poor results, just uses imagecopyresized but removes black edges. // 2 = Up to 95 times faster. Images may appear too sharp, some people may prefer it. // 3 = Up to 60 times faster. Will give high quality smooth results very close to imagecopyresampled. // 4 = Up to 25 times faster. Almost identical to imagecopyresampled for most images. // 5 = No speedup. Just uses imagecopyresampled, highest quality but no advantage over imagecopyresampled. if (empty($src_image) || empty($dst_image)) { return false; } if ($quality <= 1) { $temp = imagecreatetruecolor ($dst_w + 1, $dst_h + 1); imagecopyresized ($temp, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w + 1, $dst_h + 1, $src_w, $src_h); imagecopyresized ($dst_image, $temp, 0, 0, 0, 0, $dst_w, $dst_h, $dst_w, $dst_h); imagedestroy ($temp); } elseif ($quality < 5 && (($dst_w * $quality) < $src_w || ($dst_h * $quality) < $src_h)) { $tmp_w = $dst_w * $quality; $tmp_h = $dst_h * $quality; $temp = imagecreatetruecolor ($tmp_w + 1, $tmp_h + 1); imagecopyresized ($temp, $src_image, $dst_x * $quality, $dst_y * $quality, $src_x, $src_y, $tmp_w + 1, $tmp_h + 1, $src_w, $src_h); imagecopyresampled ($dst_image, $temp, 0, 0, 0, 0, $dst_w, $dst_h, $tmp_w, $tmp_h); imagedestroy ($temp); } else { imagecopyresampled ($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); } return true; } ?>
  • Thanks mate, I couldn't get that one to work ($src_x, $src_y, ???) but there was another one there that did the trick.
    Strange they didn't show up in my search.

    Posted: Thursday, 17 May 2007 at 9:12AM

  • the code above is a function. So u will pass it src_x etc. since ur just resizing, dst_w will be same as src_w, and dst_x is same as src_x

    Well u got one that worked for u right.
    thats good. Now u can start working on a Gallery extension for Vanilla called Wanilla :P
  • The above script was written by me. It's drop-in compatible for PHP's imagecopyresampled command. For the syntax, just see the syntax for imagecopyresampled, it's the same, even uses the same variable names. There's one additional and optional parameter "quality" at the very end, but if you just use the standard imagecopyresampled syntax the quality will default to 3, which will yield very nice and very fast results. You can play with this later if you want more speed or a little more quality.
Sign In or Register to comment.