source: extensions/write_metadata/main.inc.php @ 29135

Last change on this file since 29135 was 28872, checked in by plg, 10 years ago

new plugin write_metadata to write IPTC from Piwigo photo properties

File size: 6.3 KB
Line 
1<?php
2/*
3Plugin Name: Write Metadata
4Description: Write Piwigo photo properties (title, description, author, tags) into IPTC fields
5Author: plg
6Plugin URI: http://piwigo.org/ext/extension_view.php?eid=
7Version: auto
8*/
9
10// +-----------------------------------------------------------------------+
11// | Define plugin constants                                               |
12// +-----------------------------------------------------------------------+
13
14defined('WRITE_METADATA_ID') or define('WRITE_METADATA_ID', basename(dirname(__FILE__)));
15define('WRITE_METADATA_PATH' , PHPWG_PLUGINS_PATH.basename(dirname(__FILE__)).'/');
16
17// +-----------------------------------------------------------------------+
18// | Edit Photo                                                            |
19// +-----------------------------------------------------------------------+
20
21add_event_handler('loc_begin_admin_page', 'wm_add_link', 60);
22function wm_add_link()
23{
24        global $template, $page;
25
26        $template->set_prefilter('picture_modify', 'wm_add_link_prefilter');
27
28  if (isset($page['page']) and 'photo' == $page['page'])
29  {
30    $template->assign(
31      'U_WRITEMETADATA',
32      get_root_url().'admin.php?page=photo-'.$_GET['image_id'].'-properties&amp;write_metadata=1'
33      );
34  }
35}
36
37function wm_add_link_prefilter($content, &$smarty)
38{
39  $search = '{if !url_is_remote($PATH)}';
40 
41  $replacement = '{if !url_is_remote($PATH)}
42<li><a class="icon-arrows-cw" href="{$U_WRITEMETADATA}">{\'Write metadata\'|@translate}</a></li>';
43
44  return str_replace($search, $replacement, $content);
45}
46
47add_event_handler('loc_begin_admin_page', 'wm_picture_write_metadata');
48function wm_picture_write_metadata()
49{
50  global $page, $conf;
51
52  load_language('plugin.lang', dirname(__FILE__).'/');
53 
54  if (isset($page['page']) and 'photo' == $page['page'] and isset($_GET['write_metadata']))
55  {
56    check_input_parameter('image_id', $_GET, false, PATTERN_ID);
57    wm_write_metadata($_GET['image_id']);
58
59    $_SESSION['page_infos'][] = l10n('Metadata written into file');
60    redirect(get_root_url().'admin.php?page=photo-'.$_GET['image_id'].'-properties');
61  }
62}
63
64// +-----------------------------------------------------------------------+
65// | Batch Manager                                                         |
66// +-----------------------------------------------------------------------+
67
68add_event_handler('loc_begin_element_set_global', 'wm_element_set_global_add_action');
69function wm_element_set_global_add_action()
70{
71  global $template, $page;
72 
73  $template->set_filename('writeMetadata', realpath(WRITE_METADATA_PATH.'element_set_global_action.tpl'));
74
75  if (isset($_POST['submit']) and $_POST['selectAction'] == 'writeMetadata')
76  {
77    $page['infos'][] = l10n('Metadata written into file');
78  }
79
80  $template->assign(
81    array(
82      'WM_PWG_TOKEN' => get_pwg_token(),
83      )
84    );
85
86  $template->append(
87    'element_set_global_plugins_actions',
88    array(
89      'ID' => 'writeMetadata',
90      'NAME' => l10n('Write metadata'),
91      'CONTENT' => $template->parse('writeMetadata', true),
92      )
93    );
94}
95
96add_event_handler('ws_add_methods', 'wm_add_methods');
97function wm_add_methods($arr)
98{
99  include_once(WRITE_METADATA_PATH.'ws_functions.inc.php');
100}
101
102// +-----------------------------------------------------------------------+
103// | Common functions                                                      |
104// +-----------------------------------------------------------------------+
105
106/**
107 * inspired by convert_row_to_file_exiftool method in ExportImageMetadata
108 * class from plugin Tags2File. In plugin write_medata we just skip the
109 * batch command file, and execute directly on server (much more user
110 * friendly...).
111 */
112function wm_write_metadata($image_id)
113{
114  global $conf;
115 
116  $query = '
117SELECT
118    img.name,
119    img.comment,
120    img.author,
121    img.date_creation,
122    GROUP_CONCAT(tags.name) AS tags,
123    img.path
124  FROM '.IMAGES_TABLE.' AS img
125    LEFT OUTER JOIN '.IMAGE_TAG_TABLE.' AS img_tag ON img_tag.image_id = img.id
126    LEFT OUTER JOIN '.TAGS_TABLE.' AS tags ON tags.id = img_tag.tag_id
127  WHERE img.id = '.$image_id.'
128  GROUP BY img.id, img.name, img.comment, img.author, img.path
129;';
130  $result = pwg_query($query);
131  $row = pwg_db_fetch_assoc($result);
132
133  $name = wm_prepare_string($row['name'], 256);
134  $description = wm_prepare_string($row['comment'], 2000);
135  $author = wm_prepare_string($row['author'], 32);
136  $tags = wm_prepare_string($row['tags'], 64);
137
138  $command = isset($conf['exiftool_path']) ? $conf['exiftool_path'] : 'exiftool';
139
140  if (strlen($name) > 0)
141  {
142    # 2#105 in iptcparse($imginfo['APP13'])
143    $command.= ' -IPTC:Headline="'.$name.'"';
144
145    # 2#005 in iptcparse($imginfo['APP13'])
146    $command.= ' -IPTC:ObjectName="'.wm_cutString($name, 64).'"';
147  }
148
149  if (strlen($description) > 0)
150  {
151    # 2#120 in iptcparse($imginfo['APP13'])
152    $command.= ' -IPTC:Caption-Abstract="'.$description.'"';
153  }
154
155  if (strlen($author) > 0)
156  {
157    # 2#080 in iptcparse($imginfo['APP13'])
158    $iptc_field = 'By-line';
159
160    if (
161      $conf['use_iptc']
162      and isset($conf['use_iptc_mapping']['author'])
163      and '2#122' == $conf['use_iptc_mapping']['author']
164      )
165    {
166      # 2#122 in iptcparse($imginfo['APP13'])
167      $iptc_field = 'Writer-Editor';
168    }
169
170    $command.= ' -IPTC:'.$iptc_field.'="'.$author.'"';
171  }
172 
173  if (strlen($tags) > 0)
174  {
175    # 2#025 in iptcparse($imginfo['APP13'])
176    $command.= ' -IPTC:Keywords="'.$tags.'"';
177  }
178
179  $command.= ' "'.$row['path'].'"';
180  // echo $command;
181
182  $exec_return = exec($command, $output);
183  // echo '$exec_return = '.$exec_return.'<br>';
184  // echo '<pre>'; print_r($output); echo '</pre>';
185
186  return true;
187}
188
189function wm_prepare_string($string, $maxLen)
190{
191  return wm_cutString(
192    wm_explode_description(
193      wm_decode_html_string_to_unicode($string)
194      ),
195    $maxLen
196    );
197}
198
199function wm_cutString($description, $maxLen)
200{
201  if (strlen($description) > $maxLen)
202  {
203    $description = substr($description, 0, $maxLen);
204  }
205  return $description;
206}
207
208function wm_explode_description($description)
209{
210  return str_replace(
211    array('<br>', '<br />', "\n", "\r"),
212    array('', '', '', ''),
213    $description
214    );
215}
216
217function wm_decode_html_string_to_unicode($string)
218{
219  if (isset($string) and strlen($string) > 0)
220  {
221    $string = html_entity_decode(trim($string), ENT_QUOTES, 'UTF-8');
222    $string = stripslashes($string);
223  }
224  else
225  {
226    $string = '';
227  }
228 
229  return($string);
230}
231?>
Note: See TracBrowser for help on using the repository browser.