horbor has asked for the wisdom of the Perl Monks concerning the following question:

Hi! Hope it the right place to post my question. I have a problem resizing jpeg images. An image after resizing has grey pixels at its bottom. Everything is fine on another machine, and also there are no problems when using gd from PHP. I use Perl and I get is "Premature end of JPEG file" in apache log. The system is running under SuSE 9.2, Apache 2.2,gd 2.032, GD module 2.3. Here is the resulting image

Here is the code:

open ( UPLOADFILE, ">".$site->{cfg}{var}{full_content_path}.$site- +>{cfg}{images}{original_dir}.$sourcename ) or die "Cannot open image file $!"; binmode (UPLOADFILE); while ( <$filehandle> ){ print UPLOADFILE; } # proceed, convert if needed, resize etc. if (-e $site->{cfg}{var}{full_content_path}.$site->{cfg}{images}{o +riginal_dir}.$sourcename){ my $method = $gd_ext{$extension}; my $original = GD::Image->$method($site->{cfg}{var}{full_conte +nt_path}.$site->{cfg}{images}{original_dir}.$sourcename,1) or die "Can't load original $sourcename: $!"; # calculate new height multiplication factor my ($width, $height) = $original->getBounds(); my $factor = ($width > $height) ? ($height / $width) : ($heigh +t / $width); foreach my $pre(@prefix){ my ($w,$h) = split('x',$site->{cfg}{images}{$pre.'_wh'}); my $n_height=int($factor*$w); my $image = GD::Image->new($w, $n_height, 1); $image->copyResampled($original, 0, 0, 0, 0, $w, $n_height +, $original->width, $original->height); open(FH, ">".$site->{cfg}{var}{full_content_path}.$site->{ +cfg}{images}{$pre.'_dir'}.$pre.'_'.$filename.'.jpg') or die "Cannot open image file ".$pre."_$filename $!"; binmode(FH); print FH $image->jpeg(100); close(FH); }

Any help is appreciated.

Replies are listed 'Best First'.
Re: GD resize problems
by BrowserUk (Patriarch) on Jan 17, 2011 at 11:58 UTC

    This almost certainly isn't a GD related problem, but rather a problem with your file handling code. Specifically:

    while ( <$filehandle> ){ print UPLOADFILE; }

    While that will work fine for text files containing lines, it will likely screw up for image files that don't contain lines.

    I'd suggest you make that:

    { local $/ = \65536; local $\; while ( <$filehandle> ){ print UPLOADFILE; } }

    And see how you get on.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Thank you one more time for your reply to my problem, BrowserUK. Just 20 mins back my friend proposed to use GD from the very beginning to save the original file and then use it to resize. And it worked! So now it is:

      my $method = $gd_ext{$extension}; my $original = GD::Image->$method($filehandle,1) or die "Can't load original $sourcename: $!"; open(FH, ">".$site->{cfg}{var}{full_content_path}.$site->{cfg} +{images}{original_dir}.$sourcename) or die "Cannot open image file $!"; binmode(FH); print FH $original->jpeg(100); close(FH);

      Have a nice day!

      Thank you BrowserUK for your reply! Unfortunately it doesn't work for me. The same error "Premature end of JPEG file". Maybe you have other suggestions?
Re: GD resize problems
by Crackers2 (Parson) on Jan 17, 2011 at 14:30 UTC

    I know you already have a solution, but I think the original problem is buffering. After the

    while ( <$filehandle> ){ print UPLOADFILE; }
    loop you don't close UPLOADFILE before trying to use the file it's writing to. So likely the last part of the file just isn't flushed to disk yet when GD reads it.

    Just to check, could you try adding a close UPLOADFILE after the while loop and see if that fixes things?

      Cracker2, you are absolutely right, its just a careless mistake and I just could not see it. Sorry people for taking your time and thank you very much!