Announcement

#1 2023-02-26 14:57:27

BigIsland270972
Member
Norway
2022-03-15
374

[resolved] Write metadata plugin date_creation

Hello/Hi/Greetings,

I'm currently testing this extension and all is good exept its not writing any date data like date_creation and date original.
How to solve this?

https://piwigo.org/ext/extension_view.php?eid=769
Piwigo 13.6.0

Piwigo URL: https://arki.vet

Last edited by BigIsland270972 (2023-02-26 16:23:46)


Piwigo 14.0 | https://fotoarkiv.no | https://foto.arki.vet | http://Bergen.gallery  | http://Ålesund.gallery | http://geiranger.gallery | http://fjord.photos | http://foto.oslo.no
Apache | PHP 8.1 | MariaDB

Offline

 

#2 2023-02-26 16:19:45

BigIsland270972
Member
Norway
2022-03-15
374

Re: [resolved] Write metadata plugin date_creation

YES I was able too modify the code to include date_creation when writing to metadata via batchmanager.
Below is Before and after

/plugins/write_metadata/main.inc.php


BEFORE:

Code:

<?php
/*
Plugin Name: Write Metadata
Description: Write Piwigo photo properties (title, description, author, tags) into IPTC fields
Author: plg
Plugin URI: http://piwigo.org/ext/extension_view.php?eid=769
Version: 12.a
*/

// +-----------------------------------------------------------------------+
// | Define plugin constants                                               |
// +-----------------------------------------------------------------------+

defined('WRITE_METADATA_ID') or define('WRITE_METADATA_ID', basename(dirname(__FILE__)));
define('WRITE_METADATA_PATH' , PHPWG_PLUGINS_PATH.basename(dirname(__FILE__)).'/');

// +-----------------------------------------------------------------------+
// | Edit Photo                                                            |
// +-----------------------------------------------------------------------+

add_event_handler('loc_begin_admin_page', 'wm_add_link', 60);
function wm_add_link()
{
  global $template, $page;

  $template->set_prefilter('picture_modify', 'wm_add_link_prefilter');

  if (isset($page['page']) and 'photo' == $page['page'])
  {
    $template->assign(
      'U_WRITEMETADATA',
      get_root_url().'admin.php?page=photo-'.$_GET['image_id'].'-properties&amp;write_metadata=1'
      );
  }
}

function wm_add_link_prefilter($content)
{
  $search = '{if !url_is_remote($PATH)}';
  
  $replacement = '{if !url_is_remote($PATH)}
  <a class="icon-docs" href="{$U_WRITEMETADATA}" title="{\'Write metadata\'|translate}"></a>';

  return str_replace($search, $replacement, $content);
}

add_event_handler('loc_begin_admin_page', 'wm_picture_write_metadata');
function wm_picture_write_metadata()
{
  global $page, $conf;

  load_language('plugin.lang', dirname(__FILE__).'/');
  
  if (isset($page['page']) and 'photo' == $page['page'] and isset($_GET['write_metadata']))
  {
    check_input_parameter('image_id', $_GET, false, PATTERN_ID);
    list($rc, $output) = wm_write_metadata($_GET['image_id']);

    if (count($output) == 0)
    {
      $_SESSION['page_infos'][] = l10n('Metadata written into file');
      redirect(get_root_url().'admin.php?page=photo-'.$_GET['image_id'].'-properties');
    }
    else
    {
      $page['errors'] = array_merge($page['errors'], $output);
    }
  }
}

// +-----------------------------------------------------------------------+
// | Batch Manager                                                         |
// +-----------------------------------------------------------------------+

add_event_handler('loc_begin_element_set_global', 'wm_element_set_global_add_action');
function wm_element_set_global_add_action()
{
  global $template, $page;
  
  $template->set_filename('writeMetadata', realpath(WRITE_METADATA_PATH.'element_set_global_action.tpl'));

  if (isset($_POST['submit']) and $_POST['selectAction'] == 'writeMetadata')
  {
    $page['infos'][] = l10n('Metadata written into file');
  }

  $template->assign(
    array(
      'WM_PWG_TOKEN' => get_pwg_token(),
      )
    );

  $template->append(
    'element_set_global_plugins_actions',
    array(
      'ID' => 'writeMetadata',
      'NAME' => l10n('Write metadata'),
      'CONTENT' => $template->parse('writeMetadata', true),
      )
    );
}

add_event_handler('ws_add_methods', 'wm_add_methods');
function wm_add_methods($arr)
{
  include_once(WRITE_METADATA_PATH.'ws_functions.inc.php');
}

// +-----------------------------------------------------------------------+
// | Common functions                                                      |
// +-----------------------------------------------------------------------+

/**
 * inspired by convert_row_to_file_exiftool method in ExportImageMetadata
 * class from plugin Tags2File. In plugin write_medata we just skip the
 * batch command file, and execute directly on server (much more user
 * friendly...).
 */
function wm_write_metadata($image_id)
{
  global $conf, $logger;
  
  $query = '
SELECT
    img.name,
    img.comment,
    img.author,
    img.date_creation,
    GROUP_CONCAT(tags.name) AS tags,
    img.path,
    img.representative_ext
  FROM '.IMAGES_TABLE.' AS img
    LEFT OUTER JOIN '.IMAGE_TAG_TABLE.' AS img_tag ON img_tag.image_id = img.id
    LEFT OUTER JOIN '.TAGS_TABLE.' AS tags ON tags.id = img_tag.tag_id
  WHERE img.id = '.$image_id.'
  GROUP BY img.id, img.name, img.comment, img.author, img.path, img.representative_ext
;';
  $result = pwg_query($query);
  $row = pwg_db_fetch_assoc($result);

  $name = wm_prepare_string($row['name'], 256);
  $description = wm_prepare_string($row['comment'], 2000);
  $author = wm_prepare_string($row['author'], 32);

  $command = isset($conf['exiftool_path']) ? $conf['exiftool_path'] : 'exiftool';
  $command.= ' -q';

  if (strlen($name) > 0)
  {
    # 2#105 in iptcparse($imginfo['APP13'])
    $command.= ' -IPTC:Headline="'.$name.'"';

    # 2#005 in iptcparse($imginfo['APP13'])
    $command.= ' -IPTC:ObjectName="'.wm_cutString($name, 64).'"';
  }

  if (strlen($description) > 0)
  {
    # 2#120 in iptcparse($imginfo['APP13'])
    $command.= ' -IPTC:Caption-Abstract="'.$description.'"';
  }

  if (strlen($author) > 0)
  {
    # 2#080 in iptcparse($imginfo['APP13'])
    $iptc_field = 'By-line';

    if (
      $conf['use_iptc']
      and isset($conf['use_iptc_mapping']['author'])
      and '2#122' == $conf['use_iptc_mapping']['author']
      )
    {
      # 2#122 in iptcparse($imginfo['APP13'])
      $iptc_field = 'Writer-Editor';
    }

    $command.= ' -IPTC:'.$iptc_field.'="'.$author.'"';
  }
  
  if (strlen($row['tags']) > 0)
  {
    $tags = explode(',', $row['tags']);
    foreach ($tags as $tag)
    {
      $tag = wm_prepare_string($tag, 64);

      # 2#025 in iptcparse($imginfo['APP13'])
      $command.= ' -IPTC:Keywords="'.$tag.'"';
    }
  }

  $command.= ' "'.$row['path'].'"';
  $command.= ' 2>&1';
  // echo $command;
  $logger->info(__FUNCTION__.' command = '.$command);

  $exec_return = exec($command, $output, $rc);
  // echo '$exec_return = '.$exec_return.'<br>';
  // echo '<pre>'; print_r($output); echo '</pre>';

  // as derivatives may contain metadata, they must be reset
  delete_element_derivatives($row);

  return array($rc, $output);
}

function wm_prepare_string($string, $maxLen)
{
  return wm_cutString(
    wm_explode_description(
      wm_decode_html_string_to_unicode($string)
      ),
    $maxLen
    );
}

function wm_cutString($description, $maxLen)
{
  if (strlen($description) > $maxLen)
  {
    $description = substr($description, 0, $maxLen);
  }
  return $description;
}

function wm_explode_description($description)
{
  return str_replace(
    array('<br>', '<br />', "\n", "\r"),
    array('', '', '', ''),
    $description
    );
}

function wm_decode_html_string_to_unicode($string)
{
  if (isset($string) and strlen($string) > 0)
  {
    $string = html_entity_decode(trim($string), ENT_QUOTES, 'UTF-8');
    $string = stripslashes($string);
  }
  else
  {
    $string = '';
  }
  
  return($string);
}
?>

AFTER:


Code:

<?php
/*
Plugin Name: Write Metadata
Description: Write Piwigo photo properties (title, description, author, tags) into IPTC fields
Author: plg
Plugin URI: http://piwigo.org/ext/extension_view.php?eid=769
Version: 12.a
*/

// +-----------------------------------------------------------------------+
// | Define plugin constants                                               |
// +-----------------------------------------------------------------------+

defined('WRITE_METADATA_ID') or define('WRITE_METADATA_ID', basename(dirname(__FILE__)));
define('WRITE_METADATA_PATH' , PHPWG_PLUGINS_PATH.basename(dirname(__FILE__)).'/');

// +-----------------------------------------------------------------------+
// | Edit Photo                                                            |
// +-----------------------------------------------------------------------+

add_event_handler('loc_begin_admin_page', 'wm_add_link', 60);
function wm_add_link()
{
  global $template, $page;

  $template->set_prefilter('picture_modify', 'wm_add_link_prefilter');

  if (isset($page['page']) and 'photo' == $page['page'])
  {
    $template->assign(
      'U_WRITEMETADATA',
      get_root_url().'admin.php?page=photo-'.$_GET['image_id'].'-properties&amp;write_metadata=1'
      );
  }
}

function wm_add_link_prefilter($content)
{
  $search = '{if !url_is_remote($PATH)}';
  
  $replacement = '{if !url_is_remote($PATH)}
  <a class="icon-docs" href="{$U_WRITEMETADATA}" title="{\'Write metadata\'|translate}"></a>';

  return str_replace($search, $replacement, $content);
}

add_event_handler('loc_begin_admin_page', 'wm_picture_write_metadata');
function wm_picture_write_metadata()
{
  global $page, $conf;

  load_language('plugin.lang', dirname(__FILE__).'/');
  
  if (isset($page['page']) and 'photo' == $page['page'] and isset($_GET['write_metadata']))
  {
    check_input_parameter('image_id', $_GET, false, PATTERN_ID);
    list($rc, $output) = wm_write_metadata($_GET['image_id']);

    if (count($output) == 0)
    {
      $_SESSION['page_infos'][] = l10n('Metadata written into file');
      redirect(get_root_url().'admin.php?page=photo-'.$_GET['image_id'].'-properties');
    }
    else
    {
      $page['errors'] = array_merge($page['errors'], $output);
    }
  }
}

// +-----------------------------------------------------------------------+
// | Batch Manager                                                         |
// +-----------------------------------------------------------------------+

add_event_handler('loc_begin_element_set_global', 'wm_element_set_global_add_action');
function wm_element_set_global_add_action()
{
  global $template, $page;
  
  $template->set_filename('writeMetadata', realpath(WRITE_METADATA_PATH.'element_set_global_action.tpl'));

  if (isset($_POST['submit']) and $_POST['selectAction'] == 'writeMetadata')
  {
    $page['infos'][] = l10n('Metadata written into file');
  }

  $template->assign(
    array(
      'WM_PWG_TOKEN' => get_pwg_token(),
      )
    );

  $template->append(
    'element_set_global_plugins_actions',
    array(
      'ID' => 'writeMetadata',
      'NAME' => l10n('Write metadata'),
      'CONTENT' => $template->parse('writeMetadata', true),
      )
    );
}

add_event_handler('ws_add_methods', 'wm_add_methods');
function wm_add_methods($arr)
{
  include_once(WRITE_METADATA_PATH.'ws_functions.inc.php');
}

// +-----------------------------------------------------------------------+
// | Common functions                                                      |
// +-----------------------------------------------------------------------+

/**
 * inspired by convert_row_to_file_exiftool method in ExportImageMetadata
 * class from plugin Tags2File. In plugin write_medata we just skip the
 * batch command file, and execute directly on server (much more user
 * friendly...).
 */
function wm_write_metadata($image_id)
{
  global $conf, $logger;
  
  $query = '
SELECT
    img.name,
    img.comment,
    img.author,
    img.date_creation,
    GROUP_CONCAT(tags.name) AS tags,
    img.path,
    img.representative_ext
  FROM '.IMAGES_TABLE.' AS img
    LEFT OUTER JOIN '.IMAGE_TAG_TABLE.' AS img_tag ON img_tag.image_id = img.id
    LEFT OUTER JOIN '.TAGS_TABLE.' AS tags ON tags.id = img_tag.tag_id
  WHERE img.id = '.$image_id.'
  GROUP BY img.id, img.name, img.comment, img.author, img.path, img.representative_ext
;';
  $result = pwg_query($query);
  $row = pwg_db_fetch_assoc($result);

  $name = wm_prepare_string($row['name'], 500);
  $description = wm_prepare_string($row['comment'], 10000);
  $author = wm_prepare_string($row['author'], 500);
  $date_creation = wm_prepare_string($row['date_creation'], 50);

  $command = isset($conf['exiftool_path']) ? $conf['exiftool_path'] : 'exiftool -overwrite_original';
  $command.= ' -q';

  if (strlen($name) > 0)
  {
    # 2#105 in iptcparse($imginfo['APP13'])
    $command.= ' -m -codedcharacterset=utf8 -IPTC:Headline="'.$name.'"';

    # 2#005 in iptcparse($imginfo['APP13'])
    $command.= ' -m -codedcharacterset=utf8 -IPTC:ObjectName="'.wm_cutString($name, 64).'"';
  }

  if (strlen($description) > 0)
  {
    # 2#120 in iptcparse($imginfo['APP13'])
    $command.= ' -m -codedcharacterset=utf8 -IPTC:Caption-Abstract="'.$description.'"';
  }

  if (strlen($date_creation) > 0)
  {
    # 2#055 in iptcparse($imginfo['APP13'])
    $command.= ' -IPTC:DateCreated="'.$date_creation.'"';
  }

  if (strlen($author) > 0)
  {
    # 2#080 in iptcparse($imginfo['APP13'])
    $iptc_field = 'By-line';

    if (
      $conf['use_iptc']
      and isset($conf['use_iptc_mapping']['author'])
      and '2#122' == $conf['use_iptc_mapping']['author']
      )
    {
      # 2#122 in iptcparse($imginfo['APP13'])
      $iptc_field = 'Writer-Editor';
    }

    $command.= ' -m -codedcharacterset=utf8 -IPTC:'.$iptc_field.'="'.$author.'"';
  }
  
  if (strlen($row['tags']) > 0)
  {
    $tags = explode(',', $row['tags']);
    foreach ($tags as $tag)
    {
      $tag = wm_prepare_string($tag, 64);

      # 2#025 in iptcparse($imginfo['APP13'])
      $command.= ' -m -codedcharacterset=utf8 -IPTC:Keywords="'.$tag.'"';
    }
  }

  $command.= ' "'.$row['path'].'"';
  $command.= ' 2>&1';
  // echo $command;
  $logger->info(__FUNCTION__.' command = '.$command);

  $exec_return = exec($command, $output, $rc);
  // echo '$exec_return = '.$exec_return.'<br>';
  // echo '<pre>'; print_r($output); echo '</pre>';

  // as derivatives may contain metadata, they must be reset
  delete_element_derivatives($row);

  return array($rc, $output);
}

function wm_prepare_string($string, $maxLen)
{
  return wm_cutString(
    wm_explode_description(
      wm_decode_html_string_to_unicode($string)
      ),
    $maxLen
    );
}

function wm_cutString($description, $maxLen)
{
  if (strlen($description) > $maxLen)
  {
    $description = substr($description, 0, $maxLen);
  }
  return $description;
}

function wm_explode_description($description)
{
  return str_replace(
    array('<br>', '<br />', "\n", "\r"),
    array('', '', '', ''),
    $description
    );
}

function wm_decode_html_string_to_unicode($string)
{
  if (isset($string) and strlen($string) > 0)
  {
    $string = html_entity_decode(trim($string), ENT_QUOTES, 'UTF-8');
    $string = stripslashes($string);
  }
  else
  {
    $string = '';
  }
  
  return($string);
}
?>

Last edited by BigIsland270972 (2023-02-26 16:22:41)


Piwigo 14.0 | https://fotoarkiv.no | https://foto.arki.vet | http://Bergen.gallery  | http://Ålesund.gallery | http://geiranger.gallery | http://fjord.photos | http://foto.oslo.no
Apache | PHP 8.1 | MariaDB

Offline

 

#3 2023-02-26 16:56:04

erAck
Only trying to help
2015-09-06
1995

Re: [resolved] Write metadata plugin date_creation

It might be clearer for some to see the actual diff:

Code:

--- main.inc.php.orig  2023-02-26 16:45:37.191087719 +0100
+++ main.inc.php  2023-02-26 16:46:03.551086318 +0100
@@ -138,26 +138,33 @@
   $result = pwg_query($query);
   $row = pwg_db_fetch_assoc($result);
 
-  $name = wm_prepare_string($row['name'], 256);
-  $description = wm_prepare_string($row['comment'], 2000);
-  $author = wm_prepare_string($row['author'], 32);
+  $name = wm_prepare_string($row['name'], 500);
+  $description = wm_prepare_string($row['comment'], 10000);
+  $author = wm_prepare_string($row['author'], 500);
+  $date_creation = wm_prepare_string($row['date_creation'], 50);
 
-  $command = isset($conf['exiftool_path']) ? $conf['exiftool_path'] : 'exiftool';
+  $command = isset($conf['exiftool_path']) ? $conf['exiftool_path'] : 'exiftool -overwrite_original';
   $command.= ' -q';
 
   if (strlen($name) > 0)
   {
     # 2#105 in iptcparse($imginfo['APP13'])
-    $command.= ' -IPTC:Headline="'.$name.'"';
+    $command.= ' -m -codedcharacterset=utf8 -IPTC:Headline="'.$name.'"';
 
     # 2#005 in iptcparse($imginfo['APP13'])
-    $command.= ' -IPTC:ObjectName="'.wm_cutString($name, 64).'"';
+    $command.= ' -m -codedcharacterset=utf8 -IPTC:ObjectName="'.wm_cutString($name, 64).'"';
   }
 
   if (strlen($description) > 0)
   {
     # 2#120 in iptcparse($imginfo['APP13'])
-    $command.= ' -IPTC:Caption-Abstract="'.$description.'"';
+    $command.= ' -m -codedcharacterset=utf8 -IPTC:Caption-Abstract="'.$description.'"';
+  }
+
+  if (strlen($date_creation) > 0)
+  {
+    # 2#055 in iptcparse($imginfo['APP13'])
+    $command.= ' -IPTC:DateCreated="'.$date_creation.'"';
   }
 
   if (strlen($author) > 0)
@@ -175,7 +182,7 @@
       $iptc_field = 'Writer-Editor';
     }
 
-    $command.= ' -IPTC:'.$iptc_field.'="'.$author.'"';
+    $command.= ' -m -codedcharacterset=utf8 -IPTC:'.$iptc_field.'="'.$author.'"';
   }
   
   if (strlen($row['tags']) > 0)
@@ -186,7 +193,7 @@
       $tag = wm_prepare_string($tag, 64);
 
       # 2#025 in iptcparse($imginfo['APP13'])
-      $command.= ' -IPTC:Keywords="'.$tag.'"';
+      $command.= ' -m -codedcharacterset=utf8 -IPTC:Keywords="'.$tag.'"';
     }
   }

Though I'd not use IPTC date_creation because it is only the date overriding the EXIF DateTime if present.


Running Piwigo at https://erack.net/gallery/

Offline

 

#4 2023-02-27 05:11:49

BigIsland270972
Member
Norway
2022-03-15
374

Re: [resolved] Write metadata plugin date_creation

Yes I was able to modfy the plungin Write Metadata to include Piwigo database image table: data_creation and data_available and mapped that to. 2#055 IPTC:DateCreated and 2#030 IPTC:ReleaseDate.

Here are the code:

/plugins/write_metadata/main.inc.php

Code:

<?php
/*
Plugin Name: Write Metadata
Description: Write Piwigo photo properties (title, description, author, tags) into IPTC fields
Author: plg
Plugin URI: http://piwigo.org/ext/extension_view.php?eid=769
Version: 12.a
*/

// +-----------------------------------------------------------------------+
// | Define plugin constants                                               |
// +-----------------------------------------------------------------------+

defined('WRITE_METADATA_ID') or define('WRITE_METADATA_ID', basename(dirname(__FILE__)));
define('WRITE_METADATA_PATH' , PHPWG_PLUGINS_PATH.basename(dirname(__FILE__)).'/');

// +-----------------------------------------------------------------------+
// | Edit Photo                                                            |
// +-----------------------------------------------------------------------+

add_event_handler('loc_begin_admin_page', 'wm_add_link', 60);
function wm_add_link()
{
  global $template, $page;

  $template->set_prefilter('picture_modify', 'wm_add_link_prefilter');

  if (isset($page['page']) and 'photo' == $page['page'])
  {
    $template->assign(
      'U_WRITEMETADATA',
      get_root_url().'admin.php?page=photo-'.$_GET['image_id'].'-properties&amp;write_metadata=1'
      );
  }
}

function wm_add_link_prefilter($content)
{
  $search = '{if !url_is_remote($PATH)}';
  
  $replacement = '{if !url_is_remote($PATH)}
  <a class="icon-docs" href="{$U_WRITEMETADATA}" title="{\'Write metadata\'|translate}"></a>';

  return str_replace($search, $replacement, $content);
}

add_event_handler('loc_begin_admin_page', 'wm_picture_write_metadata');
function wm_picture_write_metadata()
{
  global $page, $conf;

  load_language('plugin.lang', dirname(__FILE__).'/');
  
  if (isset($page['page']) and 'photo' == $page['page'] and isset($_GET['write_metadata']))
  {
    check_input_parameter('image_id', $_GET, false, PATTERN_ID);
    list($rc, $output) = wm_write_metadata($_GET['image_id']);

    if (count($output) == 0)
    {
      $_SESSION['page_infos'][] = l10n('Metadata written into file');
      redirect(get_root_url().'admin.php?page=photo-'.$_GET['image_id'].'-properties');
    }
    else
    {
      $page['errors'] = array_merge($page['errors'], $output);
    }
  }
}

// +-----------------------------------------------------------------------+
// | Batch Manager                                                         |
// +-----------------------------------------------------------------------+

add_event_handler('loc_begin_element_set_global', 'wm_element_set_global_add_action');
function wm_element_set_global_add_action()
{
  global $template, $page;
  
  $template->set_filename('writeMetadata', realpath(WRITE_METADATA_PATH.'element_set_global_action.tpl'));

  if (isset($_POST['submit']) and $_POST['selectAction'] == 'writeMetadata')
  {
    $page['infos'][] = l10n('Metadata written into file');
  }

  $template->assign(
    array(
      'WM_PWG_TOKEN' => get_pwg_token(),
      )
    );

  $template->append(
    'element_set_global_plugins_actions',
    array(
      'ID' => 'writeMetadata',
      'NAME' => l10n('Write metadata'),
      'CONTENT' => $template->parse('writeMetadata', true),
      )
    );
}

add_event_handler('ws_add_methods', 'wm_add_methods');
function wm_add_methods($arr)
{
  include_once(WRITE_METADATA_PATH.'ws_functions.inc.php');
}

// +-----------------------------------------------------------------------+
// | Common functions                                                      |
// +-----------------------------------------------------------------------+

/**
 * inspired by convert_row_to_file_exiftool method in ExportImageMetadata
 * class from plugin Tags2File. In plugin write_medata we just skip the
 * batch command file, and execute directly on server (much more user
 * friendly...).
 */
function wm_write_metadata($image_id)
{
  global $conf, $logger;
  
  $query = '
SELECT
    img.name,
    img.comment,
    img.author,
    img.date_creation,
    img.date_available,
    GROUP_CONCAT(tags.name) AS tags,
    img.path,
    img.representative_ext
  FROM '.IMAGES_TABLE.' AS img
    LEFT OUTER JOIN '.IMAGE_TAG_TABLE.' AS img_tag ON img_tag.image_id = img.id
    LEFT OUTER JOIN '.TAGS_TABLE.' AS tags ON tags.id = img_tag.tag_id
  WHERE img.id = '.$image_id.'
  GROUP BY img.id, img.name, img.comment, img.author, img.path, img.representative_ext
;';
  $result = pwg_query($query);
  $row = pwg_db_fetch_assoc($result);

  $name = wm_prepare_string($row['name'], 500);
  $description = wm_prepare_string($row['comment'], 10000);
  $author = wm_prepare_string($row['author'], 500);
  $date_creation = wm_prepare_string($row['date_creation'], 50);
  $date_available = wm_prepare_string($row['date_available'], 50);


  $command = isset($conf['exiftool_path']) ? $conf['exiftool_path'] : 'exiftool -overwrite_original';
  $command.= ' -q';

  if (strlen($name) > 0)
  {
    # 2#105 in iptcparse($imginfo['APP13'])
    $command.= ' -m -codedcharacterset=utf8 -IPTC:Headline="'.$name.'"';

    # 2#005 in iptcparse($imginfo['APP13'])
    $command.= ' -m -codedcharacterset=utf8 -IPTC:ObjectName="'.wm_cutString($name, 64).'"';
  }

  if (strlen($description) > 0)
  {
    # 2#120 in iptcparse($imginfo['APP13'])
    $command.= ' -m -codedcharacterset=utf8 -IPTC:Caption-Abstract="'.$description.'"';
  }

  if (strlen($date_creation) > 0)
  {
    # 2#055 in iptcparse($imginfo['APP13'])
    $command.= ' -IPTC:DateCreated="'.$date_creation.'"';
  }

  if (strlen($date_available) > 0)
  {
    # 2#030 in iptcparse($imginfo['APP13'])
    $command.= ' -IPTC:ReleaseDate="'.$date_available.'"';
  }

  if (strlen($author) > 0)
  {
    # 2#080 in iptcparse($imginfo['APP13'])
    $iptc_field = 'By-line';

    if (
      $conf['use_iptc']
      and isset($conf['use_iptc_mapping']['author'])
      and '2#122' == $conf['use_iptc_mapping']['author']
      )
    {
      # 2#122 in iptcparse($imginfo['APP13'])
      $iptc_field = 'Writer-Editor';
    }

    $command.= ' -m -codedcharacterset=utf8 -IPTC:'.$iptc_field.'="'.$author.'"';
  }
  
  if (strlen($row['tags']) > 0)
  {
    $tags = explode(',', $row['tags']);
    foreach ($tags as $tag)
    {
      $tag = wm_prepare_string($tag, 64);

      # 2#025 in iptcparse($imginfo['APP13'])
      $command.= ' -m -codedcharacterset=utf8 -IPTC:Keywords="'.$tag.'"';
    }
  }

  $command.= ' "'.$row['path'].'"';
  $command.= ' 2>&1';
  // echo $command;
  $logger->info(__FUNCTION__.' command = '.$command);

  $exec_return = exec($command, $output, $rc);
  // echo '$exec_return = '.$exec_return.'<br>';
  // echo '<pre>'; print_r($output); echo '</pre>';

  // as derivatives may contain metadata, they must be reset
  delete_element_derivatives($row);

  return array($rc, $output);
}

function wm_prepare_string($string, $maxLen)
{
  return wm_cutString(
    wm_explode_description(
      wm_decode_html_string_to_unicode($string)
      ),
    $maxLen
    );
}

function wm_cutString($description, $maxLen)
{
  if (strlen($description) > $maxLen)
  {
    $description = substr($description, 0, $maxLen);
  }
  return $description;
}

function wm_explode_description($description)
{
  return str_replace(
    array('<br>', '<br />', "\n", "\r"),
    array('', '', '', ''),
    $description
    );
}

function wm_decode_html_string_to_unicode($string)
{
  if (isset($string) and strlen($string) > 0)
  {
    $string = html_entity_decode(trim($string), ENT_QUOTES, 'UTF-8');
    $string = stripslashes($string);
  }
  else
  {
    $string = '';
  }
  
  return($string);
}
?>

Robert


Piwigo 14.0 | https://fotoarkiv.no | https://foto.arki.vet | http://Bergen.gallery  | http://Ålesund.gallery | http://geiranger.gallery | http://fjord.photos | http://foto.oslo.no
Apache | PHP 8.1 | MariaDB

Offline

 

#5 2023-02-27 06:09:37

BigIsland270972
Member
Norway
2022-03-15
374

Re: [resolved] Write metadata plugin date_creation

the code -overwrite_original delete the original image file when processing


Piwigo 14.0 | https://fotoarkiv.no | https://foto.arki.vet | http://Bergen.gallery  | http://Ålesund.gallery | http://geiranger.gallery | http://fjord.photos | http://foto.oslo.no
Apache | PHP 8.1 | MariaDB

Offline

 

#6 2023-02-27 06:12:14

BigIsland270972
Member
Norway
2022-03-15
374

Re: [resolved] Write metadata plugin date_creation

The -m makes sure not to truncate the text and -codedcharacterset=utf8 makes sure the ÆØÅ is correctly shown.


Piwigo 14.0 | https://fotoarkiv.no | https://foto.arki.vet | http://Bergen.gallery  | http://Ålesund.gallery | http://geiranger.gallery | http://fjord.photos | http://foto.oslo.no
Apache | PHP 8.1 | MariaDB

Offline

 

#7 2023-02-27 06:46:10

BigIsland270972
Member
Norway
2022-03-15
374

Re: [resolved] Write metadata plugin date_creation

Next ISSUE is to get the code to write HTML code. I have 300 000 ship images that has HTML in the description and it's currently not written in IPTC.


Piwigo 14.0 | https://fotoarkiv.no | https://foto.arki.vet | http://Bergen.gallery  | http://Ålesund.gallery | http://geiranger.gallery | http://fjord.photos | http://foto.oslo.no
Apache | PHP 8.1 | MariaDB

Offline

 

#8 2023-02-27 08:26:12

BigIsland270972
Member
Norway
2022-03-15
374

Re: [resolved] Write metadata plugin date_creation

Tried to  add the html code manually in exifpilot to corresponding IPTC field and it shows correctly on website.
https://www.fotoarkiv.no/picture.php?/1 … earch/6376


Somehow the write metadata extension is stripping the html code.


Piwigo 14.0 | https://fotoarkiv.no | https://foto.arki.vet | http://Bergen.gallery  | http://Ålesund.gallery | http://geiranger.gallery | http://fjord.photos | http://foto.oslo.no
Apache | PHP 8.1 | MariaDB

Offline

 

#9 2023-02-27 21:12:29

BigIsland270972
Member
Norway
2022-03-15
374

Re: [resolved] Write metadata plugin date_creation

include/config_default.inc.php or local/config:

// use_iptc: Use IPTC data during database synchronization with files
// metadata
$conf['use_iptc'] = true;

// use_iptc_mapping : in which IPTC fields will Piwigo find image
// information ? This setting is used during metadata synchronisation. It
// associates a piwigo_images column name to a IPTC key
$conf['use_iptc_mapping'] = array(
  'keywords'        => '2#025',
  'author'          => '2#122',
  'name'            => '2#005',
  'comment'         => '2#120',
  'date_creation'   => '2#055',
  'date_available'  => '2#030'
  );

It was needed to change the data_available table in database to allow for NULL entries.(Otherwise you will get php errors and it fails when syncing images with no IPTC dates )

STILL NO NEWS on the html problem. Any images that contain html code gets rejected. :-)

Last edited by BigIsland270972 (2023-02-27 21:49:23)


Piwigo 14.0 | https://fotoarkiv.no | https://foto.arki.vet | http://Bergen.gallery  | http://Ålesund.gallery | http://geiranger.gallery | http://fjord.photos | http://foto.oslo.no
Apache | PHP 8.1 | MariaDB

Offline

 

#10 2023-02-27 21:36:53

BigIsland270972
Member
Norway
2022-03-15
374

Re: [resolved] Write metadata plugin date_creation

Syncronize

https://www.fotoarkiv.no//priv/sync.png

Exiftool GUI


https://www.fotoarkiv.no//priv/dump.png


Piwigo 14.0 | https://fotoarkiv.no | https://foto.arki.vet | http://Bergen.gallery  | http://Ålesund.gallery | http://geiranger.gallery | http://fjord.photos | http://foto.oslo.no
Apache | PHP 8.1 | MariaDB

Offline

 

#11 2023-02-27 21:52:42

BigIsland270972
Member
Norway
2022-03-15
374

Re: [resolved] Write metadata plugin date_creation

im not sure whats the latest working code was but here it is:

/plugins/write_metadata/main.inc.php

Code:

<?php
/*
Plugin Name: Write Metadata
Description: Write Piwigo photo properties (title, description, author, tags) into IPTC fields
Author: plg
Plugin URI: http://piwigo.org/ext/extension_view.php?eid=769
Version: 12.a
*/

// +-----------------------------------------------------------------------+
// | Define plugin constants                                               |
// +-----------------------------------------------------------------------+

defined('WRITE_METADATA_ID') or define('WRITE_METADATA_ID', basename(dirname(__FILE__)));
define('WRITE_METADATA_PATH' , PHPWG_PLUGINS_PATH.basename(dirname(__FILE__)).'/');

// +-----------------------------------------------------------------------+
// | Edit Photo                                                            |
// +-----------------------------------------------------------------------+

add_event_handler('loc_begin_admin_page', 'wm_add_link', 60);
function wm_add_link()
{
  global $template, $page;

  $template->set_prefilter('picture_modify', 'wm_add_link_prefilter');

  if (isset($page['page']) and 'photo' == $page['page'])
  {
    $template->assign(
      'U_WRITEMETADATA',
      get_root_url().'admin.php?page=photo-'.$_GET['image_id'].'-properties&amp;write_metadata=1'
      );
  }
}

function wm_add_link_prefilter($content)
{
  $search = '{if !url_is_remote($PATH)}';
  
  $replacement = '{if !url_is_remote($PATH)}
  <a class="icon-docs" href="{$U_WRITEMETADATA}" title="{\'Write metadata\'|translate}"></a>';

  return str_replace($search, $replacement, $content);
}

add_event_handler('loc_begin_admin_page', 'wm_picture_write_metadata');
function wm_picture_write_metadata()
{
  global $page, $conf;

  load_language('plugin.lang', dirname(__FILE__).'/');
  
  if (isset($page['page']) and 'photo' == $page['page'] and isset($_GET['write_metadata']))
  {
    check_input_parameter('image_id', $_GET, false, PATTERN_ID);
    list($rc, $output) = wm_write_metadata($_GET['image_id']);

    if (count($output) == 0)
    {
      $_SESSION['page_infos'][] = l10n('Metadata written into file');
      redirect(get_root_url().'admin.php?page=photo-'.$_GET['image_id'].'-properties');
    }
    else
    {
      $page['errors'] = array_merge($page['errors'], $output);
    }
  }
}

// +-----------------------------------------------------------------------+
// | Batch Manager                                                         |
// +-----------------------------------------------------------------------+

add_event_handler('loc_begin_element_set_global', 'wm_element_set_global_add_action');
function wm_element_set_global_add_action()
{
  global $template, $page;
  
  $template->set_filename('writeMetadata', realpath(WRITE_METADATA_PATH.'element_set_global_action.tpl'));

  if (isset($_POST['submit']) and $_POST['selectAction'] == 'writeMetadata')
  {
    $page['infos'][] = l10n('Metadata written into file');
  }

  $template->assign(
    array(
      'WM_PWG_TOKEN' => get_pwg_token(),
      )
    );

  $template->append(
    'element_set_global_plugins_actions',
    array(
      'ID' => 'writeMetadata',
      'NAME' => l10n('Write metadata'),
      'CONTENT' => $template->parse('writeMetadata', true),
      )
    );
}

add_event_handler('ws_add_methods', 'wm_add_methods');
function wm_add_methods($arr)
{
  include_once(WRITE_METADATA_PATH.'ws_functions.inc.php');
}

// +-----------------------------------------------------------------------+
// | Common functions                                                      |
// +-----------------------------------------------------------------------+

/**
 * inspired by convert_row_to_file_exiftool method in ExportImageMetadata
 * class from plugin Tags2File. In plugin write_medata we just skip the
 * batch command file, and execute directly on server (much more user
 * friendly...).
 */
function wm_write_metadata($image_id)
{
  global $conf, $logger;
  
  $query = '
SELECT
    img.name,
    img.comment,
    img.author,
    img.date_creation,
    img.date_available,
    GROUP_CONCAT(tags.name) AS tags,
    img.path,
    img.representative_ext
  FROM '.IMAGES_TABLE.' AS img
    LEFT OUTER JOIN '.IMAGE_TAG_TABLE.' AS img_tag ON img_tag.image_id = img.id
    LEFT OUTER JOIN '.TAGS_TABLE.' AS tags ON tags.id = img_tag.tag_id
  WHERE img.id = '.$image_id.'
  GROUP BY img.id, img.name, img.comment, img.author, img.path, img.representative_ext
;';
  $result = pwg_query($query);
  $row = pwg_db_fetch_assoc($result);

  $name = wm_prepare_string($row['name'], 500);
  $description = wm_prepare_string($row['comment'], 10000);
  $author = wm_prepare_string($row['author'], 500);
  $date_creation = wm_prepare_string($row['date_creation'], 50);
  $date_available = wm_prepare_string($row['date_available'], 50);


  $command = isset($conf['exiftool_path']) ? $conf['exiftool_path'] : 'exiftool -overwrite_original';
  $command.= ' -q';

  if (strlen($name) > 0)
  {
    # 2#105 in iptcparse($imginfo['APP13'])
    $command.= ' -m -codedcharacterset=utf8 -IPTC:Headline="'.$name.'"';

    # 2#005 in iptcparse($imginfo['APP13'])
    $command.= ' -m -codedcharacterset=utf8 -IPTC:ObjectName="'.wm_cutString($name, 64).'"';
  }

  if (strlen($description) > 0)
  {
    # 2#120 in iptcparse($imginfo['APP13'])
    $command.= ' -m -codedcharacterset=utf8 -IPTC:Caption-Abstract="'.$description.'"';
  }

  if (strlen($date_creation) > 0)
  {
    # 2#055 in iptcparse($imginfo['APP13'])
    $command.= ' -IPTC:DateCreated="'.$date_creation.'"';
  }

  if (strlen($date_available) > 0)
  {
    # 2#030 in iptcparse($imginfo['APP13'])
    $command.= ' -IPTC:ReleaseDate="'.$date_available.'"';
  }

  if (strlen($author) > 0)
  {
    # 2#080 in iptcparse($imginfo['APP13'])
    $iptc_field = 'By-line';

    if (
      $conf['use_iptc']
      and isset($conf['use_iptc_mapping']['author'])
      and '2#122' == $conf['use_iptc_mapping']['author']
      )
    {
      # 2#122 in iptcparse($imginfo['APP13'])
      $iptc_field = 'Writer-Editor';
    }

    $command.= ' -m -codedcharacterset=utf8 -IPTC:'.$iptc_field.'="'.$author.'"';
  }
  
  if (strlen($row['tags']) > 0)
  {
    $tags = explode(',', $row['tags']);
    foreach ($tags as $tag)
    {
      $tag = wm_prepare_string($tag, 64);

      # 2#025 in iptcparse($imginfo['APP13'])
      $command.= ' -m -codedcharacterset=utf8 -IPTC:Keywords="'.$tag.'"';
    }
  }

  $command.= ' "'.$row['path'].'"';
  $command.= ' 2>&1';
  // echo $command;
  $logger->info(__FUNCTION__.' command = '.$command);

  $exec_return = exec($command, $output, $rc);
  // echo '$exec_return = '.$exec_return.'<br>';
  // echo '<pre>'; print_r($output); echo '</pre>';

  // as derivatives may contain metadata, they must be reset
  delete_element_derivatives($row);

  return array($rc, $output);
}

function wm_prepare_string($string, $maxLen)
{
  return wm_cutString(
    wm_explode_description(
      wm_decode_html_string_to_unicode($string)
      ),
    $maxLen
    );
}

function wm_cutString($description, $maxLen)
{
  if (strlen($description) > $maxLen)
  {
    $description = substr($description, 0, $maxLen);
  }
  return $description;
}

function wm_explode_description($description)
{
  return str_replace(
    array('<br>', '<br />', "\n", "\r"),
    array('', '', '', ''),
    $description
    );
}

function wm_decode_html_string_to_unicode($string)
{
  if (isset($string) and strlen($string) > 0)
  {
    $string = html_entity_decode(trim($string), ENT_QUOTES, 'UTF8');
    $string = stripslashes($string);
  }
  else
  {
    $string = '';
  }
  
  return($string);
}
?>

Piwigo 14.0 | https://fotoarkiv.no | https://foto.arki.vet | http://Bergen.gallery  | http://Ålesund.gallery | http://geiranger.gallery | http://fjord.photos | http://foto.oslo.no
Apache | PHP 8.1 | MariaDB

Offline

 

#12 2023-02-28 12:01:51

BigIsland270972
Member
Norway
2022-03-15
374

Re: [resolved] Write metadata plugin date_creation

BigIsland270972 wrote:

Next ISSUE is to get the code to write HTML code. I have 300 000 ship images that has HTML in the description and it's currently not written in PTC:Caption-Abstract

@PLG ??


Piwigo 14.0 | https://fotoarkiv.no | https://foto.arki.vet | http://Bergen.gallery  | http://Ålesund.gallery | http://geiranger.gallery | http://fjord.photos | http://foto.oslo.no
Apache | PHP 8.1 | MariaDB

Offline

 

#13 2023-02-28 17:32:45

BigIsland270972
Member
Norway
2022-03-15
374

Re: [resolved] Write metadata plugin date_creation

I think  it's this part of the code that removes the html code from exiftool, but if I try to delete it, the whole script is failing. What to do?


Code:

function wm_prepare_string($string, $maxLen)
{
  return wm_cutString(
    wm_explode_description(
      wm_decode_html_string_to_unicode($string)
      ),
    $maxLen
    );
}

function wm_cutString($description, $maxLen)
{
  if (strlen($description) > $maxLen)
  {
    $description = substr($description, 0, $maxLen);
  }
  return $description;
}

function wm_explode_description($description)
{
  return str_replace(
    array('<br>', '<br />', "\n", "\r"),
    array('', '', '', ''),
    $description
    );
}

function wm_decode_html_string_to_unicode($string)
{
  if (isset($string) and strlen($string) > 0)
  {
    $string = html_entity_decode(trim($string), ENT_QUOTES, 'UTF-8');
    $string = stripslashes($string);
  }
  else
  {
    $string = '';
  }
  
  return($string);
}

Last edited by BigIsland270972 (2023-02-28 17:34:05)


Piwigo 14.0 | https://fotoarkiv.no | https://foto.arki.vet | http://Bergen.gallery  | http://Ålesund.gallery | http://geiranger.gallery | http://fjord.photos | http://foto.oslo.no
Apache | PHP 8.1 | MariaDB

Offline

 

#14 2023-02-28 19:45:25

erAck
Only trying to help
2015-09-06
1995

Re: [resolved] Write metadata plugin date_creation

Fwiw, your adding of -codedcharacterset=utf8 all over the place is most likely useless when reading metadata, that is an IPTC metadata tag to be written when writing tags or converting tags from other character encodings. If you want to force UTF-8 while reading data (and are sure that data is actually in UTF-8) then use the option -charset iptc=UTF8 instead. See man exiftool and https://exiftool.org/faq.html#Q10

Though your screenshot says CodedCharacterSet would be UTF8 already, having tag values with content other than UTF-8 encoded (e.g. Latin1, Exiftool's default IPTC encoding, or cp1252 or other Windows crap) may make the PHP function html_entity_decode(..., 'UTF-8') fail and have it return an empty string. Try to add ENT_SUBSTITUTE to its flags, see https://www.php.net/manual/en/function. … decode.php

If that helps (but you'll see the U+FFFD � replacement character) then convert the metadata tags to UTF-8 with Exiftool before reading with Piwigo. Or if you always read the same other encoding change the call to html_entity_decode() passing the proper charset, but that's a temporary hack.


Running Piwigo at https://erack.net/gallery/

Offline

 

#15 2023-03-01 08:27:42

BigIsland270972
Member
Norway
2022-03-15
374

Re: [resolved] Write metadata plugin date_creation

Thanx Remember that write metadata is WRITING NOT READING so if I dont specify -codedcharacterset=utf8 I have to convert to UTF8 using Exiftool for the ÆØÅ charcters after.

Last edited by BigIsland270972 (2023-03-01 08:30:29)


Piwigo 14.0 | https://fotoarkiv.no | https://foto.arki.vet | http://Bergen.gallery  | http://Ålesund.gallery | http://geiranger.gallery | http://fjord.photos | http://foto.oslo.no
Apache | PHP 8.1 | MariaDB

Offline

 

Board footer

Powered by FluxBB

github twitter newsletter Donate Piwigo.org © 2002-2024 · Contact