I am using ImageCopyResized() to resize images uploaded by users so all images are the same size. I have everything working just fine, however, the quality of the resized image is really poor. The resized images are really jagged.

You can see my script at:

http://joel.savvynet.com/~joel/resize.php?size=640&string=hello

go ahead and change the size value to see the results, (1024 is the original size)

I know how to change the jpeg compression settings on the image, but that doesn't seem to help this problem.

Does anyone have any suggestions on how to improve quality? Thanks.

Here is my source code for that script.

<?
Header("Content-type: image/jpeg");

// $size and $string are passed from the URL

$font = "/home/joel/public_html/ARIALBD.TTF"; // load the font
$im = imagecreatefromjpeg("MVC-069X.JPG"); // create the source image from jpeg file

if(ImageSX($im) > ImageSY($im)) // image is wider than it is tall
{
$width = $size;
$height = $width (ImageSY($im) / ImageSX($im));
}
else // image is taller than it is wide, or both dimensions are the same
{
$height = $size;
$width = $height
(ImageSX($im) / ImageSY($im));
}

$im2=ImageCreate($width, $height); // create the destination image, with the correct dimensions

$white = ImageColorAllocate ($im2, 255, 255, 255); // allocate a white color
$black = ImageColorAllocate ($im2, 0, 0, 0); // allocate a black color

imagecopyresized($im2, $im, 0,0,0,0, $width, $height, ImageSX($im), ImageSY($im)); // do the resizing

$text_size = ImageTTFBBox(12, 0, $font, $string);

ImageTTFText ($im2, 12, 0, $width - 6 - $text_size[4], $height - 6 - $text_size[1], $white, $font,
$string);

Imagejpeg($im2,'', 75); // display the new, resized image

ImageDestroy($im);
ImageDestroy($im2);
?>

    Resizing always lowers the quality.

    If you make the image bigger, the resizing routine has to "create" new pixels to fil up the empty space.
    If you make an image smaller, pixels are dropped.

    Make sure that when you resize, you maintain the size ratio (x to y)
    (resize from 10x10 to 20x20 or from 10x20 to 20x40, not from 10x10 to 20x10)

    Also, experiment with the amount of resizing. Making an image exactly half the size of the original gives good results, making it 67% will probably not give a good result.

      Most good graphics programs will resample an image when resizing. Photoshop and also ImageMagick will do this. So, when making an image smaller, of course pixels are dropped, but there is not a noticable loss of quality in the resulting image. ie.. smooth lines and edges still look smooth, not pixelated.

      Apparently, it looks like PHP-GD does not do any resampling, but only drops pixels like you say.

      Are there any tips or tricks to make GD work any better? As it stands, GD does not produce acceptable quality for a profesional site. IMHO

      Joel

      Vincent wrote:

      Resizing always lowers the quality.

      If you make the image bigger, the resizing routine has to "create" new pixels to fil up the empty space.
      If you make an image smaller, pixels are dropped.

      Make sure that when you resize, you maintain the size ratio (x to y)
      (resize from 10x10 to 20x20 or from 10x20 to 20x40, not from 10x10 to 20x10)

      Also, experiment with the amount of resizing. Making an image exactly half the size of the original gives good results, making it 67% will probably not give a good result.

        If you want a "professional" quality, you'll have to use "professional" tools, not a free gd library.

          Here is a script that will attempt to resize and resample an image. However, this script runs really slow, so you may need to modify your php.ini file so the script doesn't time out.

          The quality still isn't the best, since GD is limited to only 256 colors. So, for other people out there with similar questions, I think you'd be better of to use ImageMagick for resizing images, especially jpeg images where you want to save some of the original quality.

          <?
          $size = 800;
          $source_image = "MVC-674X.JPG";
          $im = imagecreatefromjpeg($source_image);
          if(ImageSX($im) > ImageSY($im))
          {
          $fraction = ImageSX($im) / $size;
          }
          else
          {
          $fraction = ImageSY($im) / $size;
          }
          $int = (int)$fraction + 1;
          if($int < 3)
          {
          $int = 3;
          }
          $width = ImageSX($im) / $fraction;
          $height = ImageSY($im) / $fraction;
          $im2=ImageCreate($width, $height);
          ImageCopyResized($im2, $im, 0, 0, 0, 0, $width, $height, ImageSX($im), ImageSY($im));
          Imagejpeg($im2,'out1.jpg', 65);
          for($x = 0; $x < $width; $x++)
          {
          for($y = 0; $y < $height; $y++)
          {
          $red = 0;
          $green = 0;
          $blue = 0;
          for($f = 0; $f < $int; $f++)
          {
          for($f1 = 0; $f1 < $int; $f1++)
          {
          $color1_index = ImageColorAt($im, ($x $fraction) + $f1, ($y $fraction) + $f);
          $color1 = ImageColorsForIndex($im, $color1_index);
          $red += $color1["red"];
          $green += $color1["green"];
          $blue += $color1["blue"];
          }
          }
          $red = $red / ($int $int);
          $green = $green / ($int
          $int);
          $blue = $blue / ($int * $int);
          $new_color = ImageColorResolve($im2, $red, $green, $blue);
          ImageSetPixel($im2, $x, $y, $new_color);
          }
          }
          Imagejpeg($im2,'out2.jpg', 65);
          ImageDestroy($im);
          ImageDestroy($im2);
          ?>

            So, its ok that GD sucks because it's free, right? That says a lot about open source...

            Anyway, ImageMagick is free and it rocks. PHP needs built-in support for ImageMagick, not crappy GD.

              First, GD doesn't suck, it rocks! I'm not complaining about the qualty at all!

              My point is that you should not demand professional quality output if you don't support the development of a tool financially.

                4 months later

                Is there any way to use ImageMagick with php?

                Rich

                  6 days later

                  You can call it as an external program using the exec() command. Read through the documentation for ImageMagick to learn what the command line options are. For simple stuff like uploading an image, resizing it and moveing it somewhere, it's not too bad.

                    Write a Reply...