in reply to Re: Re: GD.pm copyResized / copyResampled errors
in thread GD.pm copyResized / copyResampled errors

I wrote the following program to test the copyResampled function in GD, and I have confirmed that the 'corruption' is really a bug in the GD library, most likely due to rounding errors (because of the resampling algorithm used) and I am getting an 'extra' line in both X and Y directions.

use strict; use warnings; use IO::File; use GD; my $src = GD::Image->newFromJpeg('077.jpg'); my ($srcw, $srch) = $src->getBounds(); # (550, 413) my $destw = int ($srcw/3); my $desth = int ($srch/3); my $dest = GD::Image->new($destw-1, $desth-1); $dest->copyResampled($src,0,0,0,0,$destw,$desth,$srcw,$srch); my $f = new IO::File "078.jpg", "w" or die "Can not create image: $!"; binmode $f; print $f $dest->jpeg(80);

To get around this problem, I just created the target canvas 1 pixel smaller in both X and Y directions. I was actually expecting the library to core dump on the resampling because the destination width and height are 1 pixel greater than the canvas, but the GD library has clipped the target bitmap properly. So excellent that's the fix.

Another fix is to leave the target canvas unchanged, but add 1 to both destination width and height in the resample function, which also gives a good result.
my $dest = GD::Image->new($destw, $desth); $dest->copyResampled($src,0,0,0,0,$destw+1,$desth+1,$srcw,$srch);

Replies are listed 'Best First'.
Re: Re: Re: Re: GD.pm copyResized / copyResampled errors
by brianviehland (Initiate) on Feb 05, 2004 at 01:38 UTC
    Well, that is one problem, but that wasn't the trouble I was having... Here you can see the original and the thumbnail that I ended up with: http://viehland.org/gd-images/index.html It's like the program forgets what it's doing and it just quits in the middle...
      Can you spot the problem in your code below?
      if ((($fileWidth gt $maxThumbSize) || ($fileHeight gt $maxThumbSize)) +|| ($retype eq "yes")) { if ($fileWidth gt $fileHeight) { $scalefactor=$maxThumbSize/$fileW +idth; } else { $scalefactor=$maxThumbSize/$fileHeight; } $thumbWidth = int($fileWidth*$scalefactor); $thumbHeight = int($fi +leHeight*$scalefactor); } else { $thumbWidth=$fileWidth; $thumbHeight=$fileHeight; }

      You are comparing numeric values with gt. That means '6 gt 12' is going to be true. 'gt' and 'lt' compare string values, not numeric values, you should always use '<' and '>' instead to compare numeric values.

      And also you can turn on binmode regardless of operating system. That will have no effect on Unix anyway, but will make a big difference under Windows.

        Again, that is one problem, but that wasn't the trouble I was having... Even after changing the gt to > and lt to <, I still am having the same trouble. Here you can see the original and the thumbnail that I ended up with: Images. It's like the program forgets what it's doing and it just quits in the middle... It does this on multiple servers and with different files. Any thoughts on this?