http://qs1969.pair.com?node_id=766886

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

Hi monks!

I have written a web app that I want to be able to upload pics into ldap. Gif's work just fine, but jpeg's aren't. Apache log says: "gd-jpeg: JPEG library reports unrecoverable error: Not a JPEG file: starts with 0x47 0x3a". I used File::Type to test the data type and for both gif's & jpeg's, it returns "application/octet-stream". I'm assuming the gif library can handle "application/octet-stream", but the jpeg lib can't??? Is there a way I can get this to work for both jpegs & gif's or am I going with the wrong approach?

sub uploadPhoto{ my $self = shift; my $q = $self->query; my $admin=($self->session->param('membership') =~ /$Intranet:: +Web::CONFIG{securityldapsuper}/); my $jpegimg=''; my $photo = $q->upload('photo'); my $dn=$q->param('dn'); if($photo && $admin && $dn){ binmode($photo); my $ft = File::Type->new(); # return "type=".$ft->checktype_contents($photo); #used + for testing my $image = GD::Image->newFromGif($photo); unless($image){$image = GD::Image->newFromJpegData($ph +oto);#tried this and newFromJpeg with the same result unless($image){return "Not a valid gif or jpeg file."; +}} $jpegimg=$image->jpeg(); undef $image; unless($jpegimg){return "Problem uploading image.";} my $ldap=Net::LDAP->new($Intranet::Web::CONFIG{ldaphos +t}) or die "error connecting to LDAP $Intranet::Web::CONFIG{ldaphost} +: $@\n"; my $mesg=$ldap->bind($Intranet::Web::CONFIG{ldapuser}, + password=>$Intranet::Web::CONFIG{ldappassword}) or die "error bindin +g: $@\n"; if($jpegimg){ my $mesg1=$ldap->modify($dn,replace=>{jpegPhot +o=> $jpegimg,thumbnailPhoto=>$jpegimg, }); $mesg1->code && die $mesg1->error; } $ldap->unbind; } return "Photo uploaded".(length($jpegimg)); }
and my html:
<form name='photoForm' action='[% userbase %]/ldap.pl' enctype="multip +art/form-data" method='post' id='photoForm'> <input type="hidden" name="rm" value="getDetailUser"> <input type="hidden" name="upload" value="0"> <input type="hidden" name="dn" value="[% user.dn %]"> <input type="file" name='photo'> <input type="button" value='Upl +oad' onclick='this.form.upload.value=1;this.form.submit();'> </form>

Replies are listed 'Best First'.
Re: Uploading pics to ldap through webapp
by ramlight (Friar) on May 29, 2009 at 17:10 UTC
    Are you sure that the jpeg that you are sending really is a jpeg?

    Jpegs start with the characters "JFIF" while Gifs start with the characters "GIF".

    Your error message suggests that your jpeg starts with "G:" (0x47 0x3a)

      I finally got it to work with just GD::Image->new($photo). I don't know why I didn't try that to begin with :-)
      I looked at the data and it sure enough starts with 'JFIF'. Of course I'm able to view the jpgs in any graphic editor that I tried. I even tried to 'save as...' another jpg in case the files were corrupted. I have tried dozens of jpgs all with the same result. None have worked so I doubt it's a problem with the files.

      That's interesting that the 'starts with' translates into 'G:'. That is the drive that I'm pulling the images from and if I try pics from my H drive, the error changes to 0x48. So apparently, $q->upload('photo') returns the filename. So why does newFromGif() return an image, but not newFromJpeg() when the documentation describes them as similar functions?