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

I have been trying to read in a base64 jpeg using the GD::Image->newFromJpegData routine. The code follows:
use strict; use GD; use Tk; use Tk::JPEG; use MIME::Base64; # Create the main window my $main = MainWindow->new(); open (FILE, 'd:\test\found.B64') or die "Cannot open found.b64: $!"; my $encoded = join "", <FILE>; close FILE; my $im = GD::Image->newFromJpegData($encoded);

I have also tried using:

use strict; use GD; use Tk; use Tk::JPEG; use MIME::Base64; # Create the main window my $main = MainWindow->new(); open(JPG, 'd:\test\found.b64') or die "Cannot open found.b64: $!"; my $jpegdat; read JPG, $jpegdat, -s JPG; my $im = GD::Image->newFromJpegData($jpegdat);

But in either case I get the following error on the newFromJpegData line. Can't locate object method "newFromJpegData" via package "GD::Image". I am on a Win 2000 system.

Has anyone encountered this, or have a suggestion on what may be wrong? I have all the modules loaded and tested it earlier in the code using GD::Image->newFromJpeg('found.jpg') successfully. I have also tested the file and the base64 is a valid jpeg.

-- Scott

Replies are listed 'Best First'.
Re: newFromJpegData error
by atcroft (Abbot) on Dec 18, 2001 at 11:57 UTC

    Not having tried it but just looking at the examples in the GD.pm docs, they would seem to indicate setting $im equal to either (from your first example) GD::Image->newFromJpegData("d:\test\found.b64") or GD::Image->newFromJpegData(\*FILE).

    Can you read a base-64 encoded JPEG, or do you have to unencode it first? (Just wondering aloud.)

    Any more experienced monks have ideas/experience with this?

    Hope this helps.

    Update: In response to Steve_p 's comment in Re: newFromJpegData error about not being able to find the GD::Image->newFromJpegData() function, I looked back at the documentation for it at http://search.cpan.org/doc/LDS/GD-1.36/GD.pm , and found, in the section entitled "Object Constructors : Creating Images":

    $image = GD::Image->newFromJpeg($file)
    $image = GD::Image->newFromJpegData($data)
        These methods will create an image from a JPEG file. 
    They work just like newFromPng() and newFromPngData(), and 
    will accept the same filehandle and pathname arguments.
    
        Bear in mind that JPEG is a 24-bit format, while GD is 
    8-bit. This means that photographic images will become 
    posterized.
    

    Thus being said, I find myself in error with the second assignment I offered above, which should read GD::Image->newFromJpeg(\*FILE) rather than my reference to GD::Image->newFromJpegData(). My appologies for any confusion I may have added.

Re: newFromJpegData error
by Steve_p (Priest) on Dec 18, 2001 at 18:17 UTC
    I looked at the documentation for the GD package, and I could not find the constructor named GD::Image->newFromJpegData. I also checked the code of the GD package. There is no such function. The only constructor/function I can see that's similar is GD::Image->newFromJpeg, which you mentioned. Have you tried using it instead?
      You correct... I did the same thing later last night, and discovered that the documentation says one thing??? But could not find the GD::Image->newFromJpegData in the GD.pm either.

      I had seen several web pages that said it worked. Perhaps it was taken out of GD.pm??

      And yes, I can bring it from a file just fine. The problem was that I was hoping to keep some images in base64 in the program.

      -- Scott