0

So I have the following script to resize an incoming jpg and save it to the server in a new size. The created jpg file has terrible quality, even when I am making the quality 90 on the imagejpeg. I am wondering if I am messing it up earlier in my efforts.

// Get new sizes
    list($width, $height) = getimagesize($filename);
    $percentW=298/$width;
    $newwidth = $width * $percentW;
    $newheight = $height * $percentW;

    // Creating a blank canvas to put the new file. Is this an extra step?
    $thumb = imagecreatetruecolor($newwidth, $newheight);
    $source = imagecreatefromjpeg($filename);

    // Now I create the resized photo with the needed width and height on the blank canvas with the source file resized
    imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

    // Output to server, define 90% quality. My guess is the quality is destroyed before now.
    imagejpeg($thumb,"../../uploads/buckets/" . $_POST["bucket"] . "/" .$fileNameBucket,90);
    imagedestroy($thumb);

Am I messing up the quality before I even output the file to the server? Should I use resample instead of resize? I am stuck with using the GD library so ImageMagick is not an option. Thanks.

jeynon
  • 322
  • 6
  • 16
  • 1
    I tested your code with my GD configuration, and that works fine. So it could be an issue of how you've configured PHP. Mine is GD bundled (2.1.0 compatible), libJPEG 8. [PHP manual](http://www.php.net/imagecreatetruecolor) recommends GD 2.0.28 or later to use imagecreatetruecolor function. – Dereckson Sep 07 '13 at 13:44
  • I have found through trial and error that changing to imagecopyresampled instead of imagecopyresized seems to be helping the quality. I don't know enough about the differences to know why... – jeynon Sep 07 '13 at 17:00
  • 1
    I do the same as you and get good results. If you are trying to use an image smaller than 298px you will lose quality. Maybe you haven't figured it out. – Manolo Sep 07 '13 at 20:10

0 Answers0