source: extensions/AMetaData/JpegMetaData/JpegMetaData.class.php @ 8969

Last change on this file since 8969 was 8969, checked in by grum, 13 years ago

release 0.5.9
fix bug:2141

  • Property svn:executable set to *
File size: 28.8 KB
Line 
1<?php
2/**
3 * --:: JPEG MetaDatas ::-------------------------------------------------------
4 *
5 * Version : 1.1.0
6 * Date    : 2010-07-29
7 *
8 *  Author    : Grum
9 *   email    : grum at piwigo.org
10 *   website  : http://photos.grum.fr
11 *
12 *   << May the Little SpaceFrog be with you ! >>
13 *
14 * +-----------------------------------------------------------------------+
15 * | JpegMetaData - a PHP based Jpeg Metadata manager                      |
16 * +-----------------------------------------------------------------------+
17 * | Copyright(C) 2010  Grum - http://www.grum.fr                          |
18 * +-----------------------------------------------------------------------+
19 * | This program is free software; you can redistribute it and/or modify  |
20 * | it under the terms of the GNU General Public License as published by  |
21 * | the Free Software Foundation                                          |
22 * |                                                                       |
23 * | This program is distributed in the hope that it will be useful, but   |
24 * | WITHOUT ANY WARRANTY; without even the implied warranty of            |
25 * | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
26 * | General Public License for more details.                              |
27 * |                                                                       |
28 * | You should have received a copy of the GNU General Public License     |
29 * | along with this program; if not, write to the Free Software           |
30 * | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
31 * | USA.                                                                  |
32 * +-----------------------------------------------------------------------+
33 *
34 *
35 * +-:: HISTORY ::--------+-----------------------------------------------------
36 * |         |            |
37 * | Release | Date       |
38 * +---------+------------+-----------------------------------------------------
39 * | 0.1.0a  | 2009-12-26 |
40 * |         |            |
41 * | 1.0.0   |            | * first public release
42 * |         |            |
43 * | 1.1.0   | 2010-07-29 | * mantis bug:1686
44 * |         |            |   . bug reported on IfdReader
45 * |         |            |     When sub IFD (0x8769) refers to a sub IFD with
46 * |         |            |     an offset lower than the current IFD, the reader
47 * |         |            |     loops on the current data block and terminate
48 * |         |            |     with an error 500 ; fix consist to ignore this
49 * |         |            |     kind of offset, but it's not the right solution
50 * |         |            |     (right solution: to be able to read negative
51 * |         |            |     offset)
52 * |         |            |
53 * |         |            | * mantis feature : 1719
54 * |         |            |   . Coding a DateTime class ; used only if there is
55 * |         |            |     no PHP built-in DateTime class
56 * |         |            |
57 * |         |            | * add the "schema" property to Tag class
58 * |         |            |
59 * |         |            | * fixed bug about filtering schema
60 * |         |            |   . when loading metadata, filter on schema are now
61 * |         |            |     applied ; 'magic' metadata are computed even if
62 * |         |            |     the other schema are filtered
63 * |         |            |
64 * |         |            | * add the "getTag" function
65 * |         |            |
66 * | 1.1.1   | 2010-09-13 | * mantis bug:1826
67 * |         |            |   . digiKam XMP tags are not recognized
68 * |         |            |
69 * |         |            | * mantis bug:1859
70 * |         |            |   . JpegMetadata class can't manage multiple IPTC
71 * |         |            |     keywords
72 * |         |            |
73 * |         |            | * mantis bug:1870
74 * |         |            |   . Xmp ISOSpeedRatings was not interpreted by
75 * |         |            |     'Magic' metadata
76 * |         |            |
77 * | 1.1.3   | 2010-09-30 | * mantis bug:1894
78 * |         |            |   . Error when filling the metadata repository
79 * |         |            |
80 * |         |            | * mantis bug:1898
81 * |         |            |   . Warning "division by zero" on Canon images
82 * |         |            |
83 * |         |            | * mantis bug:1911
84 * |         |            |   . Unable to read Jpeg file if there is extradata
85 * |         |            |     after the EOI marker
86 * |         |            |
87 * |         |            | * mantis bug:1863
88 * |         |            |   . Except "keywords" meta, all IPTC meta declared
89 * |         |            |     as "repeatable" are not managed
90 * |         |            |
91 * |         |            | * mantis bug:1955
92 * |         |            |   . Incorrect mapping for IPTC File format
93 * |         |            |
94 * |         |            | * mantis bug:1955
95 * |         |            |   . Incorrect mapping for IPTC File format
96 * |         |            |
97 * |         |            | * mantis bug:1956
98 * |         |            |   . IPTC "Subject Reference" is not implemented
99 * |         |            |
100 * |         |            | * mantis bug:1965
101 * |         |            |   . add XML <lr:hierarchicalSubject> markup
102 * |         |            |
103 * |         |            | * mantis bug:1975
104 * |         |            |   . Implement COM segment as a tag
105 * |         |            |
106 * |         |            | * mantis bug:1976
107 * |         |            |   . Implement keywords as magic tag
108 * |         |            |
109 * |         |            | * mantis bug:1978
110 * |         |            |   . Some meta names are not translated in french
111 * |         |            |
112 * | 1.1.4   | 2011-01-29 | * mantis bug:2141
113 * |         |            |   . MakerNotes on some Nikon file are not recognized
114 * |         |            |     and script is terminated with a memory allocation
115 * |         |            |     error
116 * |         |            |
117 * |         |            |
118 * |         |            |
119 * |         |            |
120 * +---------+------------+-----------------------------------------------------
121 *
122 *
123 * -----------------------------------------------------------------------------
124 *
125 * References about definition & interpretation of metadata tags :
126 *  - EXIF 2.20 Specification    => http://www.exif.org/Exif2-2.PDF
127 *  - TIFF 6.0 Specification     => http://partners.adobe.com/public/developer/en/tiff/TIFF6.pdf
128 *  - Exiftool by Phil Harvey    => http://www.sno.phy.queensu.ca/~phil/exiftool/
129 *                                  http://owl.phy.queensu.ca/~phil/exiftool/TagNames
130 *  - Exiv2 by Andreas Huggel    => http://www.exiv2.org/
131 *  - MetaData working group     => http://www.metadataworkinggroup.org/specs/
132 *  - Adobe XMP Developer Center => http://www.adobe.com/devnet/xmp/
133 *  - Gezuz                      => http://gezus.one.free.fr/?Plugin-EXIF-pour-Spip-1-9-2
134 *  - JPEG format                => http://crousseau.free.fr/imgfmt_jpeg.htm
135 *  - International Press Telecomunication Council specifications
136 *                               => http://www.iptc.org/
137 *  - IPTC headers structure     => http://www.codeproject.com/KB/graphics/iptc.aspx?msg=1014929
138 *  - CPAN                       => http://search.cpan.org/dist/Image-MetaData-JPEG/lib/Image/MetaData/JPEG/Structures.pod
139 *                               => http://search.cpan.org/~bettelli/Image-MetaData-JPEG/lib/Image/MetaData/JPEG/MakerNotes.pod
140 *
141 * -----------------------------------------------------------------------------
142 * To support internationalization the JpegMetaData package uses ".po" and ".mo"
143 * files, and use "php-gettext"
144 * ==> See files in External/php-gettext for more information about this project
145 * -----------------------------------------------------------------------------
146 *
147 * The JpegMetaData is the main class for reading metadata of a Jpeg file
148 *
149 * It provides two essentialy high level functions to read different kind of
150 * metadata (EXIF, IPTC, XMP) :
151 *  - (static) getTagList
152 *  - load
153 *  - getTags
154 *  - getTag
155 *
156 * -----------------------------------------------------------------------------
157 *
158 * .. Notes ..
159 *
160 * About tags and translation in local lang
161 * With the 'getTags()' method, the JpegMetaData returns an array of Tag objects
162 * found in the jpeg file.
163 *
164 * A Tag object have 2 properties that can be translated into a local language :
165 *  - the name, getted with 'getName()'
166 *  - the valueLabel, getted with 'getLabel()'
167 *
168 * Theses properties ARE NOT translated automatically.
169 *
170 * You can translate it with the Locale class, by using the static method 'get'
171 *
172 * Example :
173 *  Locale::get($myTag->getName()) will return the translated name of the Tag
174 *  Locale::get($myTag->getLabel()) will return the translated value of the Tag
175 *
176 * ===========> See Tag.class.php to know more about the Tag class <============
177 * ========> See Locale.class.php to know more about the Locale class <=========
178 *
179 *
180 * -----------------------------------------------------------------------------
181 */
182
183  define("JPEG_METADATA_DIR", dirname(__FILE__)."/");
184
185  require_once(JPEG_METADATA_DIR."Common/DateTime.class.php");
186  require_once(JPEG_METADATA_DIR."Readers/JpegReader.class.php");
187  require_once(JPEG_METADATA_DIR."TagDefinitions/MagicTags.class.php");
188
189  class JpegMetaData
190  {
191    const TAGFILTER_KNOWN       = 0x01;
192    const TAGFILTER_IMPLEMENTED = 0x02;
193    const TAGFILTER_ALL         = 0x03;
194
195    private $jpeg = null;
196    protected $tags = Array();
197    private $options = Array();
198
199    /**
200     * this static function returns an array of tags definitions
201     *
202     * the only parameter is an array to determine filter options
203     *
204     * ---------------------+---------------------------------------------------
205     * key                  | descriptions/values
206     * ---------------------+---------------------------------------------------
207     * filter               | Integer
208     *                      | This options is used to filter implemented tag
209     *                      |  JpegMetaData::TAGFILTER_ALL
210     *                      |  => returns all the tags
211     *                      |  JpegMetaData::TAGFILTER_IMPLEMENTED
212     *                      |  => returns only the implemented tags
213     *                      |
214     * optimizeIptcDateTime | Boolean
215     *                      | IPTC Date/Time are separated into 2 tags
216     *                      | if this option is set to true, only dates tags are
217     *                      | returned (assuming this option is used when an
218     *                      | image file is loaded)
219     *                      |
220     * exif                 | Boolean
221     * iptc                 | If set to true, the function returns all the tags
222     *                      | known for the specified type tag
223     * xmp                  |
224     * maker                | maker => returns specifics tags from all the known
225     * magic                |          makers
226     *                      |
227     * ---------------------+---------------------------------------------------
228     *
229     * returned value is an array
230     * each keys is a tag name and the associated value is a 2-level array
231     *  'implemented' => Boolean, allowing to know if the tags is implemented or
232     *                   not
233     *  'translatable'=> Boolean, allowing to know if the tag value can be
234     *                   translated
235     *  'name'        => String, the tag name translated in locale language
236     *
237     * @Param Array $options  (optional)
238     * @return Array(keyName => Array('implemented' => Boolean, 'name' => String))
239     */
240    static public function getTagList($options=Array())
241    {
242      $default=Array(
243        'filter' => self::TAGFILTER_ALL,
244        'optimizeIptcDateTime' => false,
245        'exif'  => true,
246        'iptc'  => true,
247        'xmp'   => true,
248        'maker' => true,
249        'magic' => true,
250        'com'   => true
251      );
252
253      foreach($default as $key => $val)
254      {
255        if(array_key_exists($key, $options))
256          $default[$key]=$options[$key];
257      }
258
259      $list=Array();
260      $returned=Array();
261
262      if($default['exif'])
263      {
264        $list[]="exif";
265        $list[]="gps";
266      }
267
268      if($default['maker'])
269      {
270        $list[]=MAKER_PENTAX;
271        $list[]=MAKER_NIKON;
272        $list[]=MAKER_CANON;
273      }
274
275      if($default['iptc'])
276        $list[]="iptc";
277
278      if($default['xmp'])
279        $list[]="xmp";
280
281      if($default['magic'])
282        $list[]="magic";
283
284      if($default['com'])
285        $list[]="com";
286
287      foreach($list as $val)
288      {
289        unset($tmp);
290
291        switch($val)
292        {
293          case "exif":
294            $tmp=new IfdTags();
295            $schema=Schemas::EXIF;
296            break;
297          case "gps":
298            $tmp=new GpsTags();
299            $schema=Schemas::EXIF_GPS;
300            break;
301          case "iptc":
302            $tmp=new IptcTags();
303            $schema=Schemas::IPTC;
304            break;
305          case "xmp":
306            $tmp=new XmpTags();
307            $schema=Schemas::XMP;
308            break;
309          case "magic":
310            $tmp=new MagicTags();
311            $schema=Schemas::MAGIC;
312            break;
313          case "com":
314            $tmp=new ComTags();
315            $schema=Schemas::COM;
316            break;
317          case MAKER_PENTAX:
318            include_once(JPEG_METADATA_DIR."TagDefinitions/PentaxTags.class.php");
319            $tmp=new PentaxTags();
320            $schema=Schemas::EXIF_MAKER.'.'.MAKER_PENTAX;
321            break;
322          case MAKER_NIKON:
323            include_once(JPEG_METADATA_DIR."TagDefinitions/NikonTags.class.php");
324            $tmp=new NikonTags();
325            $schema=Schemas::EXIF_MAKER.'.'.MAKER_NIKON;
326            break;
327          case MAKER_CANON:
328            include_once(JPEG_METADATA_DIR."TagDefinitions/CanonTags.class.php");
329            $tmp=new CanonTags();
330            $schema=Schemas::EXIF_MAKER.'.'.MAKER_CANON;
331            break;
332          default:
333            $tmp=null;
334            $schema="?";
335            break;
336        }
337
338        if(!is_null($tmp))
339          foreach($tmp->getTags() as $key => $tag)
340          {
341            if(self::filter(true, $tag['implemented'], $default['filter']))
342            {
343              if(array_key_exists('tagName', $tag))
344                $name=$tag['tagName'];
345              else
346                $name=$key;
347
348              if(array_key_exists('schema', $tag) and $val==Schemas::EXIF)
349                $subSchema=".".$tag['schema'];
350              else
351                $subSchema="";
352
353              if($val==Schemas::XMP)
354                $keyName=$schema.$subSchema.".".$key;
355              else
356                $keyName=$schema.$subSchema.".".$name;
357              $returned[$keyName]=Array(
358                'implemented' => $tag['implemented'],
359                'translatable' => $tag['translatable'],
360                'name' => $name
361              );
362            }
363          }
364      }
365
366      ksort($returned);
367
368      return($returned);
369    }
370
371
372    /**
373     * the filter function is used by the classe to determine if a tag is
374     * filtered or not
375     *
376     * @Param Boolean $known
377     * @Param Boolean $implemented
378     * @Param Integer $filter
379     *
380     */
381    static public function filter($known, $implemented, $filter)
382    {
383      return(($known and (($filter & self::TAGFILTER_KNOWN) == self::TAGFILTER_KNOWN )) or
384                ($implemented and (($filter & self::TAGFILTER_IMPLEMENTED) == self::TAGFILTER_IMPLEMENTED )));
385    }
386
387    /**
388     * the constructor need an optional filename and options
389     *
390     * if no filename is given, you can use the "load" function after the object
391     * is instancied
392     *
393     * if no options are given, the class use the default values
394     *
395     * ---------------------+---------------------------------------------------
396     * key                  | descriptions/values
397     * ---------------------+---------------------------------------------------
398     * filter               | Integer
399     *                      | This options is used to filter implemented tag
400     *                      |  JpegMetaData::TAGFILTER_ALL
401     *                      |  => returns all the tags
402     *                      |  JpegMetaData::TAGFILTER_IMPLEMENTED
403     *                      |  => returns only the implemented tags, not
404     *                      |     implemented tag are excluded
405     *                      |  JpegMetaData::TAGFILTER_KNOWN
406     *                      |  => returns only the known tags (implemented or
407     *                      |     not), unknown tag are excluded
408     *                      |
409     * optimizeIptcDateTime | Boolean
410     *                      | IPTC Date/Time are separated into 2 tags
411     *                      | if this option is set to true, only dates tags are
412     *                      | returned (in this case, time is included is the
413     *                      | date)
414     *                      |
415     * exif                 | Boolean
416     * iptc                 | If set to true, the function returns all the tags
417     * xmp                  | known for the specified type tag
418     * magic                | the exif parameter include the maker tags
419     *                      |
420     * ---------------------+---------------------------------------------------
421     *
422     * @Param String $file    (optional)
423     * @Param Array  $options (optional)
424     *
425     */
426    function __construct($file = "", $options = Array())
427    {
428      $this->load($file, $options);
429    }
430
431    function __destruct()
432    {
433      $this->unsetAll();
434    }
435
436    /**
437     * load a file
438     *
439     * options values are the same than the constructor's options
440     *
441     * @Param String $file
442     * @Param Array  $options (optional)
443     *
444     */
445    public function load($file, $options = Array())
446    {
447      $this->unsetAll();
448
449      $this->initializeOptions($options);
450      $this->tags = Array();
451      $this->jpeg = new JpegReader($file);
452
453      if($this->jpeg->isLoaded() and $this->jpeg->isValid())
454      {
455        foreach($this->jpeg->getAppMarkerSegments() as $key => $appMarkerSegment)
456        {
457          if($appMarkerSegment->dataLoaded())
458          {
459            $data=$appMarkerSegment->getData();
460
461            if($data instanceof TiffReader)
462            {
463              /*
464               * Load Exifs tags from Tiff block
465               */
466              if($data->getNbIFDs()>0 and
467                 ($this->options['magic'] or $this->options['exif']))
468              {
469                $this->loadIfdTags($data->getIFD(0), Schemas::EXIF_TIFF);
470              }
471            }
472            elseif($data instanceof XmpReader)
473            {
474              /*
475               * Load Xmp tags from Xmp block
476               */
477              if($this->options['magic'] or $this->options['xmp'])
478              {
479                $this->loadTags($data->getTags(), Schemas::XMP);
480              }
481            }
482            elseif($data instanceof IptcReader)
483            {
484              /*
485               * Load IPTC tags from IPTC block
486               */
487              if($this->options['optimizeIptcDateTime'])
488                $data->optimizeDateTime();
489
490              if($this->options['magic'] or $this->options['iptc'])
491              {
492                $this->loadTags($data->getTags(), Schemas::IPTC);
493              }
494            }
495            elseif($data instanceof ComReader)
496            {
497              $this->loadTags($data->getTags(), Schemas::COM);
498            }
499          }
500        }
501
502        if($this->options['magic'])
503        {
504          $this->processMagicTags();
505        }
506
507        // clean all unwanted metadata
508        foreach($this->tags as $key => $tag)
509        {
510          if(isset($this->options[$tag->getSchema()]) and !$this->options[$tag->getSchema()]) unset($this->tags[$key]);
511        }
512
513        ksort($this->tags);
514      }
515    }
516
517    /**
518     * This function returns an array of tags found in the loaded file
519     *
520     * It's possible to made a second selection to filter items
521     *
522     * ---------------------+---------------------------------------------------
523     * key                  | descriptions/values
524     * ---------------------+---------------------------------------------------
525     * tagFilter            | Integer
526     *                      | This options is used to filter implemented tag
527     *                      |  JpegMetaData::TAGFILTER_ALL
528     *                      |  => returns all the tags
529     *                      |  JpegMetaData::TAGFILTER_IMPLEMENTED
530     *                      |  => returns only the implemented tags, not
531     *                      |     implemented tag are excluded
532     *                      |  JpegMetaData::TAGFILTER_KNOWN
533     *                      |  => returns only the known tags (implemented or
534     *                      |     not), unknown tag are excluded
535     *                      |
536     * ---------------------+---------------------------------------------------
537     *
538     * Note, the filter is applied on loaded tags. If a filter was applied when
539     * the file was loaded, you cannot expand the tag list, only reduce
540     * example :
541     *  $jpegmd = new JpegMetadata($file, Array('filter' => JpegMetaData::TAGFILTER_IMPLEMENTED));
542     *     => the unknown tag are not loaded
543     *  $jpegmd->getTags(JpegMetaData::TAGFILTER_ALL)
544     *     => unknown tag will not be restitued because they are not loaded...
545     *
546     * the function returns an array of Tag.
547     *
548     *
549     * ===========> See the Tag.class.php to know all about a tag <=============
550     *
551     * @Param Integer $tagFilter (optional)
552     *
553     */
554    public function getTags($tagFilter = self::TAGFILTER_ALL)
555    {
556      $returned=Array();
557      foreach($this->tags as $key => $val)
558      {
559        if(self::filter($val->isKnown(), $val->isImplemented(), $tagFilter))
560        {
561          $returned[$key]=$val;
562        }
563      }
564      return($returned);
565    }
566
567
568    /**
569     * This function returns the tag object for the given tag name
570     *
571     * @param String $key : the tag name (ie: "exif.exif.ApertureValue")
572     * @return Tag : the tag object, null if the tag doesn't exist
573     */
574    public function getTag($key)
575    {
576      if(array_key_exists($key, $this->tags))
577      {
578        return($this->tags[$key]);
579      }
580      return(null);
581    }
582
583    /**
584     * initialize the options...
585     *
586     * @Param Array $options (optional)
587     *
588     */
589    private function initializeOptions($options=Array())
590    {
591      $this->options = Array(
592        'filter' => self::TAGFILTER_ALL,
593        'optimizeIptcDateTime' => false,
594        'exif'  => true,
595        'iptc'  => true,
596        'xmp'   => true,
597        'magic' => true,
598        'com'   => true
599      );
600
601      foreach($this->options as $key => $val)
602      {
603        if(array_key_exists($key, $options))
604          $this->options[$key]=$options[$key];
605      }
606    }
607
608    /**
609     * load tags from an IFD structure
610     *
611     * see Tiff.class.php and IfdReader.class.php for more informations
612     *
613     * @Param IfdReader $ifd
614     * @Param String    $exifKey
615     *
616     */
617    private function loadIfdTags($ifd, $exifKey)
618    {
619      foreach($ifd->getTags() as $key => $tag)
620      {
621        if((self::filter($tag->getTag()->isKnown(), $tag->getTag()->isImplemented(), $this->options['filter'])) or
622           ($tag->getTag()->getName()=='Exif IFD Pointer' or
623            $tag->getTag()->getName()=='MakerNote' or
624            $tag->getTag()->getName()=='GPS IFD Pointer'))
625        {
626          /*
627           * only tag responding to the filter are selected
628           * note the tags 'Exif IFD Pointer', 'MakerNote' & 'GPS IFD Pointer'
629           * are not declared as implemented (otherwise they are visible with
630           * the static 'getTagList' function) but must be selected even if
631           * filter says "implemented only"
632           */
633          if($tag->getTag()->getLabel() instanceof IfdReader)
634          {
635            switch($tag->getTag()->getName())
636            {
637              case 'Exif IFD Pointer':
638                $exifKey2=Schemas::EXIF_EXIF;
639                break;
640              case 'MakerNote':
641                $exifKey2=Schemas::EXIF_MAKER.".".$tag->getTag()->getLabel()->getMaker();
642                break;
643              case 'GPS IFD Pointer':
644                $exifKey2=Schemas::EXIF_GPS;
645                break;
646              default:
647                $exifKey2=$exifKey;
648                break;
649            }
650            $this->loadIfdTags($tag->getTag()->getLabel(), $exifKey2);
651          }
652          else
653          {
654            $this->tags[$exifKey.".".$tag->getTag()->getName()]=$tag->getTag();
655          }
656        }
657      }
658    }
659
660    /**
661     * Used to load tags from an IPTc or XMP structure
662     *
663     * see IptcReader.class.php and XmpReader.class.php
664     *
665     * @Param Tag[]  $ifd
666     * @Param String $tagKey
667     *
668     */
669    private function loadTags($tags, $tagKey)
670    {
671      foreach($tags as $key => $tag)
672      {
673        if(self::filter($tag->isKnown(), $tag->isImplemented(), $this->options['filter']))
674        {
675          $this->tags[$tagKey.".".$tag->getName()]=$tag;
676        }
677      }
678    }
679
680    /**
681     * MagicTags are built with this function
682     */
683    protected function processMagicTags()
684    {
685      $magicTags=new MagicTags();
686
687      foreach($magicTags->getTags() as $key => $val)
688      {
689        $tag=new Tag($key,0,$key);
690
691        for($i=0; $i<count($val['tagValues']); $i++)
692        {
693          $found=true;
694          preg_match_all('/{([a-z0-9:\.\s\/]*)(\[.*\])?}/i', $val['tagValues'][$i], $returned, PREG_PATTERN_ORDER);
695          foreach($returned[1] as $testKey)
696          {
697            $found=$found & (array_key_exists($testKey, $this->tags) and $this->tags[$testKey]->getValue()!='');
698          }
699          if(count($returned[1])==0) $found=false;
700
701          if($found)
702          {
703            $returned=trim(
704              preg_replace_callback(
705                '/{([a-z0-9:\.\s\/\[\]]*)}/i',
706                Array(&$this, "processMagicTagsCB"),
707                $val['tagValues'][$i]
708              )
709            );
710
711            $returned=$this->processSpecialMagicTag($key, $returned);
712
713            $tag->setValue($returned);
714            $tag->setLabel($returned);
715            $tag->setKnown(true);
716            $tag->setImplemented($val['implemented']);
717            $tag->setTranslatable($val['translatable']);
718            $tag->setSchema(Schemas::MAGIC);
719
720            $i=count($val['tagValues']);
721          }
722        }
723
724        if($tag->isImplemented() and $found)
725        {
726          $this->tags["magic.".$key]=$tag;
727        }
728
729        unset($tag);
730      }
731      unset($magicTags);
732    }
733
734    /**
735     * this function is called by the processMagicTags to replace tagId by the
736     * tag values
737     *
738     * @param Array $matches : array[1] = the tagId
739     * @return String : the tag value
740     */
741    protected function processMagicTagsCB($matches)
742    {
743      $label="";
744      preg_match_all('/([a-z0-9:\.\s\/]*)\[(.*)\]/i', $matches[1], $result, PREG_PATTERN_ORDER);
745      if(count($result[0])>0)
746      {
747
748        if(array_key_exists($result[1][0], $this->tags))
749        {
750          $tag=$this->tags[$result[1][0]]->getLabel();
751
752          preg_match_all('/([a-z0-9:\.\s\/]*)\[(.*)\]/i', $result[2][0], $result2, PREG_PATTERN_ORDER);
753
754          if(count($result2[0])>0)
755          {
756            if(array_key_exists($result2[2][0], $tag[$result2[1][0]] ))
757              $label=$tag[$result2[1][0]][$result2[2][0]];
758          }
759          else
760          {
761            if(array_key_exists($result[2][0], $tag))
762              $label=$tag[$result[2][0]];
763          }
764        }
765      }
766      else
767      {
768        if(array_key_exists($matches[1], $this->tags))
769        {
770          $label=$this->tags[$matches[1]]->getLabel();
771        }
772      }
773      if($label instanceof DateTime)
774        return($label->format("Y-m-d H:i:s"));
775
776      $label=XmpTags::getAltValue($label, L10n::getLanguage());
777
778      if(is_array($label))
779      {
780        return(implode(", ", $label));
781      }
782
783      return(trim($label));
784    }
785
786    /**
787     *
788     *
789     */
790    protected function processSpecialMagicTag($key, $value)
791    {
792      switch($key)
793      {
794        case "GPS.LatitudeNum":
795        case "GPS.LongitudeNum":
796          preg_match_all('/(\d{1,3})°\s*(\d{1,2})\'(?:\s*(\d+(?:\.\d+)?)")?[\s\|\.]*(N|S|E|W)/i', $value, $result);
797          $num=(float)$result[1][0] + ((float) $result[2][0])/60;
798          if($result[3][0]!="") $num+= ((float) $result[3][0])/3600;
799          if($result[4][0]=="W" or $result[4][0]=="S") $num=-$num;
800          return($num);
801        case "Author.Keywords":
802          $list=array_flip(array_flip(explode(',', str_replace(array('/', '|', ','), ',', $value))));
803          sort($list);
804          $tmp=array();
805          foreach($list as $key=>$val)
806          {
807            if(trim($val)!='') $tmp[]=trim($val);
808          }
809          return($tmp);
810          break;
811        default:
812          return($value);
813      }
814    }
815
816
817    /**
818     * used by the destructor to clean variables
819     */
820    private function unsetAll()
821    {
822      unset($this->tags);
823      unset($this->jpeg);
824      unset($this->options);
825    }
826
827
828  } // class JpegMetaData
829
830?>
Note: See TracBrowser for help on using the repository browser.