boutell: (Default)
Image_JpegMarkerReader and Image_JpegXmpReader are out. These are PHP packages relating to JPEG images. Image_JpegXmpReader, which you may find useful, extracts the metadata (title, description, photographer credit, etc.) found in JPEGs produced by Photoshop and compatible programs (*). This is accomplished quickly and painlessly, without the need to load extensions written in C.

At P'unk Ave, we needed these capabilities to quickly and painlessly import photos into GPTMC Pressroom, a site we built for the Greater Philadelphia Tourism Marketing Corporation. The GPTMC has zillions of photos; their staff knows Photoshop. We could make them reenter all that metadata. But we don't, because we rock.

The Image_JpegMarkerReader package, which you are less likely to use directly, provides support for fetching individual "markers" from a JPEG file. A JPEG file is made up of two things: "markers," which contain descriptive information like XMP (among other things), and the actual image data. Most people won't need to deal with this package much, but perhaps you want to fetch something else from a JPEG marker, something Image_JpegXmpReader doesn't cover. In that case, Image_JpegMarkerReader is for you. You can subclass it just as I did or use it directly.

Soon you'll be able to install these packages with a single pear command, which will follow the dependencies and install both:

pear install Image_JpegXmpReader

For now, though, you'll get warnings that the package is in beta, an error message, and an explanation that you can manually install them like this:

pear install channel://pear.php.net/Image_JpegMarkerReader-0.5.0
pear install channel://pear.php.net/Image_JpegXmpReader-0.5.0

This is all it takes to do what 99% of programmers will ever want from Image_JpegXmpReader. If the image has no XMP marker or just doesn't contain these particular fields, these methods return false. Yes, there are fancier methods provided for fancier cases.

require 'Image/JpegXmpReader.php';
try {
  $xmp = new Image_JpegXmpReader($file);
  // Photographer credit
  $creator = $xmp->getCreator();
  // Long description
  $description = $xmp->getDescription();
  // Caption
  $title = $xmp->getTitle();
/ // Returns an array of tags (like "shopping" or "education") that
  // have been assigned to this image
  $tags = $xmp->getSubjects();
} catch (Exception $e) {
  echo("Exception thrown while parsing $file, it's probably not a JPEG\n");
}
And that's pretty much all there is to it. Hope you find it useful!

You can find a somewhat fancier test program here.

(Note: both packages require PHP 5. If you are still running PHP 4, I'll start feeling sorry for you whenever I get through laughing at you. Unless it's at your employer's insistence, in which case I'll skip straight to feeling sorry for you.)

(*) Photoshop used to save this stuff in EXIF format, which PHP can handle "out of the box," but Adobe decided to move to XMP instead. For flexibility and XML-ness, I suppose.

September 2014

S M T W T F S
 123456
78910111213
14151617181920
2122232425 2627
282930    

Syndicate

RSS Atom

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Oct. 17th, 2017 10:11 pm
Powered by Dreamwidth Studios