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

Hello again,

I am working on a class project involving the use of the GD module and CGI and have run into a bit of a brick wall.

In order to understand how to modify remote images I stole the following code from this tutorial the put in some ‘my’ statements and put in my own URL.

now I am getting a 500 error with the following message in the error log:

gd-jpeg: JPEG library reports unrecoverable error: Not a JPEG file: starts with 0x64 0x7 Can’t call method “colorResolve” on an undefined value at /my/perl/l/r/my.cgi line 7.

Premature end of script head /my/perl/l/r/my.cgi

#!/opt/bin/perl -w use strict; use CGI ':standard'; use GD; my $image = GD::Image->newFromJpegData("http://www.funnyandjokes.com/p +ictures/images/bush_lord-of-the-rings.jpg"); my $black = $image->colorResolve(0,0,0); $image->string(gdGiantFont,2,10,"Eat More Hobbits!",$black); open(FILE, ">text1.jpg") || die "Cannot open text1.jpg: $!\n"; print FILE $image->jpeg;

Could someone please clarify what is happening and tell me how to get this to work?

Thank you for your help

-mox

Replies are listed 'Best First'.
Re: Error log clarification needed...
by ikegami (Patriarch) on Dec 20, 2006 at 02:38 UTC

    newFromJpegData expects a JPEG to be in the variable passed as argument, not a URI.

    newFromJpeg won't work either, since that expects a file name to passed as argument, not a URI.

    You probably want something like

    my $image = GD::Image->newFromJpegData("/web/www.funnyandjokes.com/pic +tures/images/bush_lord-of-the-rings.jpg");

    If the image is remote, download it using LWP first.

      Thank you for your swift response. Do you mean using LWP's get method like this?

      use strict; use GD; use CGI; use LWP::Simple qw(get); my $image_web = get('http://www.funnyandjokes.com/pictures/images/bush +_lord-of-the-rings.jpg'); $image = GD::Image->newFromJpegData($image_web); my $black = $image->colorResolve(0,0,0); $image->string(gdGiantFont,2,10,"Eat More Hobbits!",$black); open(FILE, ">text1.jpg") || die "Cannot open text1.jpg: $!\n"; print FILE $image->jpeg;

      If this code is correct, then I should be able to use the resulting image in the cgi generated html by simply using this tag:

      <img src="text1.jpg">

      Is this correct?

      Thank you again!

      -mox

      UPDATE: Thank you all, I just managed to fix it! Thank you very much for pointing me towards LWP!

Re: Error log clarification needed...
by moklevat (Priest) on Dec 20, 2006 at 02:37 UTC
    The code in the tutorial that you link uses newFromJpeg and not newFromJpegData. What happens if you restore newFromJpeg?

    Update: ikegami points out below why this won't work either, and gives a suggestion for a fix.

Re: Error log clarification needed...
by derby (Abbot) on Dec 20, 2006 at 12:07 UTC

    I have nothing to add to the above answers just a kudos and ++ for actually asking a homework/class question in an appropriate way.

    -derby