in reply to Re: Grabbing Jpeg Captions
in thread Grabbing Jpeg Captions
#Function to pull the caption data from a jpg image. #Expects the filename to be passed to it sub get_photo_caption { $jpg_filename = shift; #Grab the filename passed open(PHOTO_IN,"$jpg_filename"); #Open the file binmode PHOTO_IN; #Set filemode to bin for good meas +ure $/= "\x42\x49\x4D\x03"; #Input Record Delimeter. Works for + Photoshop 5.5 - 7.0 $cnt=0; #We want 1st of 2 records. while (<PHOTO_IN>) { #Read Photo... if($_ =~ /\x4a\x46\x49\x46/) { #We are looking for JFIF in the he +ader to avoid miss reading captions $JFIF = 1; } if ("$cnt"<1) { # Have Photo Header... $photo_header="$_"; # Save Photo Header... ++$cnt; # Bump count to skip photo. } } $photo_header =~ s/\x1C\x02/XYZZY/g; # Replace hex markers with tex +t. $photo_header =~ s/[\n\r]/\x20/g; # Replace newlines with space. @fields=split(/XYZ/,$photo_header); # Set array using text marker. $n=2; # Start at 2 to skip j +unk. ITEM: while ( "$fields[$n]" ne "" ) { # Process all array el +ements. $fields[$n] =~ s/ZY\x78../Caption : /; # Set Caption Text Marker +. if ($fields[$n] =~ /^Caption/) { # Save Caption Text for databa +se. $caption=""; $caption=substr($fields[$n],10); #Apply the filters to the caption $caption =~ s/(\s*)$//g; $caption =~ s/'/''/g; $caption =~ s/\n//g; $caption =~ tr/a-zA-Z0-9\`\~\!\@\#\$\%\^\&\*\(\)\_\+\-\=\{\ +}\|\[\]\\\:\"\;\'\<\>\?\,\.\/ \t//cd; $caption =~ s/8BIM.*$//g; } $n++; }#endofwhile #If JFIF wasn't equal to 1 then we probably got bogus caption inform +ation so clear out the caption variable if($JFIF != 1) { $caption = ''; } #Photoshop only allows for 2000 character captions, if its bigger th +an that we probably got garbage. $captionsize = length($caption); if($captionsize > 2000) { $caption = ''; } chomp($caption); #For good measure remove any line feeds or CR that + may be on the end of the string close(PHOTO_IN); #Close our file $JFIF = 0; #Reset the JFIF value for the next time around. }#endofsub
|
|---|