Announcement

  •  » Extensions
  •  » Plugin to import XMP tags as Piwigo keywords

#1 2015-07-09 14:09:13

zottel
Guest

Plugin to import XMP tags as Piwigo keywords

Hi,

just wanted to say that I've written a very basic plugin that can import the keywords in <dc:Subject> in the XMP metadata to Piwigo keywords. It doesn't import any other XMP fields.

I've written it only for myself, and I don't have the time to support and maintain such a plugin, so I just post it here for anyone to use. If anybody wants to turn it into a "real" plugin that can be downloaded at Piwigo, feel free to do so.

I've adapted the code to read the XMP tags from what I found at http://surniaulula.com/2013/04/09/read- … ml-in-php/ , so if you want to make it support more metadata than just the keywords, look there.

To use the plugin as I wrote it, do the following:

1) Create a directory named XMPTags in the plugins/ directory in your Piwigo installation.
2) In that directory, create a file named main.inc.php with the following contents:

Code:

<?php
/*
Plugin Name: XMP Tags
Version: 0.1
Description: Add XMP tags in Xmp.dc.subject to Piwigo tags
Plugin URI:
Author: zottel
Author URI: https://zottel.red/channel/zottel
*/

if (!defined('PHPWG_ROOT_PATH'))
{
  die('Hacking attempt!');
}

add_event_handler(
  'format_exif_data',
  'xmptags_add_xmp_tags'
  );

function xmptags_add_xmp_tags($exif, $filename)
{
    // we don't provide all exif info, only want to add some
    if ($exif == null)
        return null;

    // code adapted from http://surniaulula.com/2013/04/09/read-adobe-xmp-xml-in-php/
    $max_size = 512000;
    $chunk_size = 65536;
    $start_tag = '<x:xmpmeta';
    $end_tag = '</x:xmpmeta>';
    $xmp_raw = null;
    $chunk = '';
    if ($file_fh = fopen( $filename, 'rb' ))
        {
        $file_size = filesize( $filename );
        while ( ( $file_pos = ftell( $file_fh ) ) < $file_size  && $file_pos < $max_size )
            {
            $chunk .= fread( $file_fh, $chunk_size );
            if ( ( $end_pos = strpos( $chunk, $end_tag ) ) !== false )
                {
                if ( ( $start_pos = strpos( $chunk, $start_tag ) ) !== false )
                    {
                    $xmp_raw = substr( $chunk, $start_pos, $end_pos - $start_pos + strlen( $end_tag ) );
                    }
                break;  // stop reading after finding the xmp data
                }
            }
        fclose( $file_fh );
        }
    if ($xmp_raw != null)
        {
        $dcSubject = preg_match("/<dc:subject>\s*<rdf:(?:Bag|Seq)>\s*(.*?)\s*<\/rdf:(?:Bag|Seq)>\s*<\/dc:subject>/is", $xmp_raw, $match) ? $match[1] : null;
        if ($dcSubject != null)
            {
            $exif['xmp_keywords'] = preg_match_all( "/<rdf:li[^>]*>([^>]*)<\/rdf:li>/is", $dcSubject, $match ) ? $match[1] : $dcSubject;
            if (is_array($exif['xmp_keywords']))
                $exif['xmp_keywords'] = implode(',', $exif['xmp_keywords']);
            }
        }
    return $exif;
}

?>

3) To actually import the keywords, add the following to your local/config/config.inc.php:

Code:

$conf['use_exif_mapping'] = array(
  'date_creation' => 'DateTimeOriginal',
  'keywords' => 'xmp_keywords'
  );

As I said, I won't support this. Maybe I happen to visit this forum again, by chance see a help request and have the time to to answer, but generally: Use it as you like; if it doesn't work, find out yourself why. ;-)

Best regards, zottel

 
  •  » Extensions
  •  » Plugin to import XMP tags as Piwigo keywords

Board footer

Powered by FluxBB

github twitter facebook google+ newsletter Donate Piwigo.org © 2002-2019 · Contact