views:

251

answers:

2

I have a question about IPTC metadata. Is it possible to search images that aren't in a database by their IPTC metadata (keywords) and show them and how would I go about doing this? I just need a basic idea.

I know there is the iptcparse() function for PHP.

I have already written a function to grab the image name, location, and extension for all images within a galleries folder and all subdirectories by .jpg extension.

I need to figure out how to extract the metadata without storing it in a database and how to search through it, grab the relevant images that match the search tag (their IPTC keywords should match) and how to display them. I know at the point that I have the final results (post search) i can echo an imagetag with src="$filelocation"> if i have the final results in an array.

Basically, I am not sure if I need to store all my images into a mysql database and also extract the keywords and store them in the database as well before I can actually search and display the results. Also, if you could guide me to any gallery that already is able to do this, that could help as well.

Thanks for any help regarding this issue.

A: 

Hi,

If you don't have extracted those IPTC data from your images, each time someone will search, you'll have to :

  • loop on every images
  • for each image, extract the IPTC data
  • see if the IPTC data for the current image matches

If you have more than a couple image, this will be really bad for performances, I'd say.


So, in my opinion, it would be far better to :

  • add a couple of fields in your database
  • extract the relevant IPTC data when the image is uploaded / stored
  • store the IPTC data in those DB fields
  • search in those DB fields
    • Or use some search engine like Lucene or Sphinx -- but that is another problem.

It'll mean a bit more work for you right now : you have more code to write...

... But it also means your website will have better chances to survive when there are several images and many users doing searches.

Pascal MARTIN
A: 

It is not clear what in particular is giving you problems, but perhaps this will give you some ideas:

<?php
# Images we're searching
$images = array('/path/to/image.jpg', 'another-image.jpg');

# IPTC keywords to values (from exiv2, see below)
$query = array('Byline' => 'Some Author');

# Perform the search
$result = select_jpgs_by_iptc_fields($images, $query);

# Display the results
foreach ($result as $path) {
    echo '<img src="', htmlspecialchars($path), '">';
}

function select_jpgs_by_iptc_fields($jpgs, $query) {
    $matches = array();
    foreach ($jpgs as $path) {
        $iptc = get_jpg_iptc_metadata($path);
        foreach ($query as $name => $values) {
            if (!is_array($values))
                $values = array($values);
            if (count(array_intersect($iptc[$name], $values)) != count($values))
                continue 2;
        }
        $matches[] = $path;
    }
    return $matches;
}

function get_jpg_iptc_metadata($path) {
    $size = getimagesize($path, $info);
    if(isset($info['APP13']))
    {
        return human_readable_iptc(iptcparse($info['APP13']));
    }
    else {
        return null;
    }
}

function human_readable_iptc($iptc) {
# From the exiv2 sources
static $iptc_codes_to_names =
array(0 => 'ModelVersion',
      5 => 'Destination',
      20 => 'FileFormat',
      22 => 'FileVersion',
      30 => 'ServiceId',
      40 => 'EnvelopeNumber',
      50 => 'ProductId',
      60 => 'EnvelopePriority',
      70 => 'DateSent',
      80 => 'TimeSent',
      90 => 'CharacterSet',
      100 => 'UNO',
      120 => 'ARMId',
      122 => 'ARMVersion',
      0 => 'RecordVersion',
      3 => 'ObjectType',
      4 => 'ObjectAttribute',
      5 => 'ObjectName',
      7 => 'EditStatus',
      8 => 'EditorialUpdate',
      10 => 'Urgency',
      12 => 'Subject',
      15 => 'Category',
      20 => 'SuppCategory',
      22 => 'FixtureId',
      25 => 'Keywords',
      26 => 'LocationCode',
      27 => 'LocationName',
      30 => 'ReleaseDate',
      35 => 'ReleaseTime',
      37 => 'ExpirationDate',
      38 => 'ExpirationTime',
      40 => 'SpecialInstructions',
      42 => 'ActionAdvised',
      45 => 'ReferenceService',
      47 => 'ReferenceDate',
      50 => 'ReferenceNumber',
      55 => 'DateCreated',
      60 => 'TimeCreated',
      62 => 'DigitizationDate',
      63 => 'DigitizationTime',
      65 => 'Program',
      70 => 'ProgramVersion',
      75 => 'ObjectCycle',
      80 => 'Byline',
      85 => 'BylineTitle',
      90 => 'City',
      92 => 'SubLocation',
      95 => 'ProvinceState',
      100 => 'CountryCode',
      101 => 'CountryName',
      103 => 'TransmissionReference',
      105 => 'Headline',
      110 => 'Credit',
      115 => 'Source',
      116 => 'Copyright',
      118 => 'Contact',
      120 => 'Caption',
      122 => 'Writer',
      125 => 'RasterizedCaption',
      130 => 'ImageType',
      131 => 'ImageOrientation',
      135 => 'Language',
      150 => 'AudioType',
      151 => 'AudioRate',
      152 => 'AudioResolution',
      153 => 'AudioDuration',
      154 => 'AudioOutcue',
      200 => 'PreviewFormat',
      201 => 'PreviewVersion',
      202 => 'Preview',
      );
   $human_readable = array();
   foreach ($iptc as $code => $field_value) {
       $code = (int)substr($code, strrpos($code, '#') + 1);
       $human_readable[$iptc_codes_to_names[$code]] = $field_value;
   }
   return $human_readable;
}
Inshallah