Delving into Images with PHP, Exif and ImageSize – the not so secret art
Recently I have been tasked with automatically compiling, tagging and indexing images added to an application via FTP. The images had been tagged using Adobe Bridge or Photoshop, anyone with knowledge of Adobe products will be familiar with tagging images. For those who aren’t, you can add descriptions, titles, ratings, keywords and copyrights (to name a few) to the core of an image using Bridge or Photoshop. My challenge was extracting this information at the other end. As the user would be uploading 1000s of images, there was absolutely no way they would want to tag each image after waiting for it to upload via a web form. After a few cron scripts dumping errors and FTP permission issues I managed to configure a system that would listen in a drop folder on one of the servers and then chmod and move the file to a web friendly directory for processing. This is where the interesting stuff begins.
As if, exif!
One of two core PHP functions used to pull this information out is exif, which wasn’t the easiest thing to find as googling “PHP get image keywords” was surprisingly unsuccessful. PHP’s explanation of exif is as follows:
With the exif extension you are able to work with image meta data. For example, you may use exif functions to read meta data of pictures taken from digital cameras by working with information stored in the headers of the JPEG and TIFF images.
Exif may be quite a large library, but all I used was exif_read_data:
$path = '/path/to/image/img.jpg'; $exif = exif_read_data($path); //Lets pull out some information just as an example $description = $exif['ImageDescription']; $filesize = $exif['FileSize']; $xresolution = $exif['XResolution']; $yresolution = $exif['YResolution'];
I was fairly happy with the amount of information that EXIF could give me about an image, for more information about other values, see the documentation. But I am still missing quite a lot of the required information. After a lot more googling, I managed to find the poorly documented reference parameter in the getimagesize() function. Once getimagesize returns the data array, it will be meaningless unless we iptcparse it.
$path = '/path/to/image/img.jpg';
$imagesize = array();
//Lets pull out some more information just as an example
$tags = array();
$tags = $imagesize['2#025'];
$title = '';
$title = $imagesize['2#005'];
This may only be a simple example but, why would I do all of the work for you?! Happy var_dump()ing!
|Print article||This entry was posted by Matt on February 1, 2011 at 3:39 pm, and is filed under Web Dev. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site.|
about 5 years ago - 21 comments
The art to having a website with decent imagery is to allow the user/administrator to crop the images. Despite advancing web technologies, I still cannot trust a computer to scale, resize or crop an image without the resulting image being of someones foot. There are certain ‘off-the-shelf’ products, mainly for ASP.net, with very little introduced
about 5 years ago - 3 comments
The Wireless Universal Resource File (WURFL) is an open source device library which describes itself as follows: The WURFL is an “ambitious” configuration file that contains info about all known Wireless devices on earth. Of course, new devices are created and released at all times. While this configuration file is bound to be out of
about 5 years ago - 33 comments
For weeks I tolerated the annoyance of CodeIgniter’s Session library logging my out continuously, saying to myself “I works…kind of… ill fix it later”. Eventually the problem started affecting AJAX method calls, large file uploads and simple CRUD operation forms so I began trawling the internet for a fix. After hours and hours, I found
about 5 years ago - 108 comments
PayPal has a huge API reference to integrate it’s services into any website. But from personal experience I found that the theory is far more simple than the practice. There are various methods to implement PayPal’s services including Soap webservices with ‘Express Checkout’ and ‘Website Payment Pro’. This tutorial will focus on ‘Website Payment Pro’
about 5 years ago - 2 comments
I thought it was high time I released a list of the best plugins that I use with my WordPress installation. So here goes… 1. All in one webmaster This plugin thanks to ArpitShah is a bundle of all the best tracking and analytics solutions available to the web and I would highly recommend it.
about 5 years ago - 32 comments
The CodeIgniter Form_Validation library is an extremely useful tool especially the ability to extend all core routines. Some validation requires real time PHP & MySQL processing, for example checking an email against those already registered with your site. To use a PHP function to return values to the jQuery validate function, the only important theory
about 5 years ago - 10 comments
When data sets get large and MySQL database querying to search become too load heavy and slow, full indexing is required. Several solutions are available but in this article I will be demonstrating the Apache foundations Solr Java Lucene implementation. For this a Java build will be required. Linux or Mac is less of a