xref: /dokuwiki/inc/JpegMeta.php (revision 9c9753d6eadb7a5b7495594bd7f1bc4480edd4a2)
1<?php
2/**
3 * JPEG metadata reader/writer
4 *
5 * @license    BSD <http://www.opensource.org/licenses/bsd-license.php>
6 * @link       http://github.com/sd/jpeg-php
7 * @author     Sebastian Delmont <sdelmont@zonageek.com>
8 * @author     Andreas Gohr <andi@splitbrain.org>
9 * @author     Hakan Sandell <hakan.sandell@mydata.se>
10 * @todo       Add support for Maker Notes, Extend for GIF and PNG metadata
11 */
12
13// Original copyright notice:
14//
15// Copyright (c) 2003 Sebastian Delmont <sdelmont@zonageek.com>
16// All rights reserved.
17//
18// Redistribution and use in source and binary forms, with or without
19// modification, are permitted provided that the following conditions
20// are met:
21// 1. Redistributions of source code must retain the above copyright
22//    notice, this list of conditions and the following disclaimer.
23// 2. Redistributions in binary form must reproduce the above copyright
24//    notice, this list of conditions and the following disclaimer in the
25//    documentation and/or other materials provided with the distribution.
26// 3. Neither the name of the author nor the names of its contributors
27//    may be used to endorse or promote products derived from this software
28//    without specific prior written permission.
29//
30// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
31// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
32// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
33// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
36// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
37// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
38// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
39// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
40// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
41
42class JpegMeta {
43    var $_fileName;
44    var $_fp = null;
45    var $_fpout = null;
46    var $_type = 'unknown';
47
48    var $_markers;
49    var $_info;
50
51
52    /**
53     * Constructor
54     *
55     * @author Sebastian Delmont <sdelmont@zonageek.com>
56     *
57     * @param $fileName
58     */
59    function __construct($fileName) {
60
61        $this->_fileName = $fileName;
62
63        $this->_fp = null;
64        $this->_type = 'unknown';
65
66        unset($this->_info);
67        unset($this->_markers);
68    }
69
70    /**
71     * Returns all gathered info as multidim array
72     *
73     * @author Sebastian Delmont <sdelmont@zonageek.com>
74     */
75    function & getRawInfo() {
76        $this->_parseAll();
77
78        if ($this->_markers == null) {
79            return false;
80        }
81
82        return $this->_info;
83    }
84
85    /**
86     * Returns basic image info
87     *
88     * @author Sebastian Delmont <sdelmont@zonageek.com>
89     */
90    function & getBasicInfo() {
91        $this->_parseAll();
92
93        $info = array();
94
95        if ($this->_markers == null) {
96            return false;
97        }
98
99        $info['Name'] = $this->_info['file']['Name'];
100        if (isset($this->_info['file']['Url'])) {
101            $info['Url'] = $this->_info['file']['Url'];
102            $info['NiceSize'] = "???KB";
103        } else {
104            $info['Size'] = $this->_info['file']['Size'];
105            $info['NiceSize'] = $this->_info['file']['NiceSize'];
106        }
107
108        if (@isset($this->_info['sof']['Format'])) {
109            $info['Format'] = $this->_info['sof']['Format'] . " JPEG";
110        } else {
111            $info['Format'] = $this->_info['sof']['Format'] . " JPEG";
112        }
113
114        if (@isset($this->_info['sof']['ColorChannels'])) {
115            $info['ColorMode'] = ($this->_info['sof']['ColorChannels'] > 1) ? "Color" : "B&W";
116        }
117
118        $info['Width'] = $this->getWidth();
119        $info['Height'] = $this->getHeight();
120        $info['DimStr'] = $this->getDimStr();
121
122        $dates = $this->getDates();
123
124        $info['DateTime'] = $dates['EarliestTime'];
125        $info['DateTimeStr'] = $dates['EarliestTimeStr'];
126
127        $info['HasThumbnail'] = $this->hasThumbnail();
128
129        return $info;
130    }
131
132
133    /**
134     * Convinience function to access nearly all available Data
135     * through one function
136     *
137     * @author Andreas Gohr <andi@splitbrain.org>
138     *
139     * @param array|string $fields field name or array with field names
140     * @return bool|string
141     */
142    function getField($fields) {
143        if(!is_array($fields)) $fields = array($fields);
144        $info = false;
145        foreach($fields as $field){
146            if(strtolower(substr($field,0,5)) == 'iptc.'){
147                $info = $this->getIPTCField(substr($field,5));
148            }elseif(strtolower(substr($field,0,5)) == 'exif.'){
149                $info = $this->getExifField(substr($field,5));
150            }elseif(strtolower(substr($field,0,4)) == 'xmp.'){
151                $info = $this->getXmpField(substr($field,4));
152            }elseif(strtolower(substr($field,0,5)) == 'file.'){
153                $info = $this->getFileField(substr($field,5));
154            }elseif(strtolower(substr($field,0,5)) == 'date.'){
155                $info = $this->getDateField(substr($field,5));
156            }elseif(strtolower($field) == 'simple.camera'){
157                $info = $this->getCamera();
158            }elseif(strtolower($field) == 'simple.raw'){
159                return $this->getRawInfo();
160            }elseif(strtolower($field) == 'simple.title'){
161                $info = $this->getTitle();
162            }elseif(strtolower($field) == 'simple.shutterspeed'){
163                $info = $this->getShutterSpeed();
164            }else{
165                $info = $this->getExifField($field);
166            }
167            if($info != false) break;
168        }
169
170        if($info === false)  $info = '';
171        if(is_array($info)){
172            if(isset($info['val'])){
173                $info = $info['val'];
174            }else{
175                $info = join(', ',$info);
176            }
177        }
178        return trim($info);
179    }
180
181    /**
182     * Convinience function to set nearly all available Data
183     * through one function
184     *
185     * @author Andreas Gohr <andi@splitbrain.org>
186     *
187     * @param string $field field name
188     * @param string $value
189     * @return bool success or fail
190     */
191    function setField($field, $value) {
192        if(strtolower(substr($field,0,5)) == 'iptc.'){
193            return $this->setIPTCField(substr($field,5),$value);
194        }elseif(strtolower(substr($field,0,5)) == 'exif.'){
195            return $this->setExifField(substr($field,5),$value);
196        }else{
197            return $this->setExifField($field,$value);
198        }
199    }
200
201    /**
202     * Convinience function to delete nearly all available Data
203     * through one function
204     *
205     * @author Andreas Gohr <andi@splitbrain.org>
206     *
207     * @param string $field field name
208     * @return bool
209     */
210    function deleteField($field) {
211        if(strtolower(substr($field,0,5)) == 'iptc.'){
212            return $this->deleteIPTCField(substr($field,5));
213        }elseif(strtolower(substr($field,0,5)) == 'exif.'){
214            return $this->deleteExifField(substr($field,5));
215        }else{
216            return $this->deleteExifField($field);
217        }
218    }
219
220    /**
221     * Return a date field
222     *
223     * @author Andreas Gohr <andi@splitbrain.org>
224     *
225     * @param string $field
226     * @return false|string
227     */
228    function getDateField($field) {
229        if (!isset($this->_info['dates'])) {
230            $this->_info['dates'] = $this->getDates();
231        }
232
233        if (isset($this->_info['dates'][$field])) {
234            return $this->_info['dates'][$field];
235        }
236
237        return false;
238    }
239
240    /**
241     * Return a file info field
242     *
243     * @author Andreas Gohr <andi@splitbrain.org>
244     *
245     * @param string $field field name
246     * @return false|string
247     */
248    function getFileField($field) {
249        if (!isset($this->_info['file'])) {
250            $this->_parseFileInfo();
251        }
252
253        if (isset($this->_info['file'][$field])) {
254            return $this->_info['file'][$field];
255        }
256
257        return false;
258    }
259
260    /**
261     * Return the camera info (Maker and Model)
262     *
263     * @author Andreas Gohr <andi@splitbrain.org>
264     * @todo   handle makernotes
265     *
266     * @return false|string
267     */
268    function getCamera(){
269        $make  = $this->getField(array('Exif.Make','Exif.TIFFMake'));
270        $model = $this->getField(array('Exif.Model','Exif.TIFFModel'));
271        $cam = trim("$make $model");
272        if(empty($cam)) return false;
273        return $cam;
274    }
275
276    /**
277     * Return shutter speed as a ratio
278     *
279     * @author Joe Lapp <joe.lapp@pobox.com>
280     *
281     * @return string
282     */
283    function getShutterSpeed() {
284        if (!isset($this->_info['exif'])) {
285            $this->_parseMarkerExif();
286        }
287        if(!isset($this->_info['exif']['ExposureTime'])){
288            return '';
289        }
290
291        $field = $this->_info['exif']['ExposureTime'];
292        if($field['den'] == 1) return $field['num'];
293        return $field['num'].'/'.$field['den'];
294    }
295
296    /**
297     * Return an EXIF field
298     *
299     * @author Sebastian Delmont <sdelmont@zonageek.com>
300     *
301     * @param string $field field name
302     * @return false|string
303     */
304    function getExifField($field) {
305        if (!isset($this->_info['exif'])) {
306            $this->_parseMarkerExif();
307        }
308
309        if ($this->_markers == null) {
310            return false;
311        }
312
313        if (isset($this->_info['exif'][$field])) {
314            return $this->_info['exif'][$field];
315        }
316
317        return false;
318    }
319
320    /**
321     * Return an XMP field
322     *
323     * @author Hakan Sandell <hakan.sandell@mydata.se>
324     *
325     * @param string $field field name
326     * @return false|string
327     */
328    function getXmpField($field) {
329        if (!isset($this->_info['xmp'])) {
330            $this->_parseMarkerXmp();
331        }
332
333        if ($this->_markers == null) {
334            return false;
335        }
336
337        if (isset($this->_info['xmp'][$field])) {
338            return $this->_info['xmp'][$field];
339        }
340
341        return false;
342    }
343
344    /**
345     * Return an Adobe Field
346     *
347     * @author Sebastian Delmont <sdelmont@zonageek.com>
348     *
349     * @param string $field field name
350     * @return false|string
351     */
352    function getAdobeField($field) {
353        if (!isset($this->_info['adobe'])) {
354            $this->_parseMarkerAdobe();
355        }
356
357        if ($this->_markers == null) {
358            return false;
359        }
360
361        if (isset($this->_info['adobe'][$field])) {
362            return $this->_info['adobe'][$field];
363        }
364
365        return false;
366    }
367
368    /**
369     * Return an IPTC field
370     *
371     * @author Sebastian Delmont <sdelmont@zonageek.com>
372     *
373     * @param string $field field name
374     * @return false|string
375     */
376    function getIPTCField($field) {
377        if (!isset($this->_info['iptc'])) {
378            $this->_parseMarkerAdobe();
379        }
380
381        if ($this->_markers == null) {
382            return false;
383        }
384
385        if (isset($this->_info['iptc'][$field])) {
386            return $this->_info['iptc'][$field];
387        }
388
389        return false;
390    }
391
392    /**
393     * Set an EXIF field
394     *
395     * @author Sebastian Delmont <sdelmont@zonageek.com>
396     * @author Joe Lapp <joe.lapp@pobox.com>
397     *
398     * @param string $field field name
399     * @param string $value
400     * @return bool
401     */
402    function setExifField($field, $value) {
403        if (!isset($this->_info['exif'])) {
404            $this->_parseMarkerExif();
405        }
406
407        if ($this->_markers == null) {
408            return false;
409        }
410
411        if ($this->_info['exif'] == false) {
412            $this->_info['exif'] = array();
413        }
414
415        // make sure datetimes are in correct format
416        if(strlen($field) >= 8 && strtolower(substr($field, 0, 8)) == 'datetime') {
417            if(strlen($value) < 8 || $value{4} != ':' || $value{7} != ':') {
418                $value = date('Y:m:d H:i:s', strtotime($value));
419            }
420        }
421
422        $this->_info['exif'][$field] = $value;
423
424        return true;
425    }
426
427    /**
428     * Set an Adobe Field
429     *
430     * @author Sebastian Delmont <sdelmont@zonageek.com>
431     *
432     * @param string $field field name
433     * @param string $value
434     * @return bool
435     */
436    function setAdobeField($field, $value) {
437        if (!isset($this->_info['adobe'])) {
438            $this->_parseMarkerAdobe();
439        }
440
441        if ($this->_markers == null) {
442            return false;
443        }
444
445        if ($this->_info['adobe'] == false) {
446            $this->_info['adobe'] = array();
447        }
448
449        $this->_info['adobe'][$field] = $value;
450
451        return true;
452    }
453
454    /**
455     * Calculates the multiplier needed to resize the image to the given
456     * dimensions
457     *
458     * @author Andreas Gohr <andi@splitbrain.org>
459     *
460     * @param int $maxwidth
461     * @param int $maxheight
462     * @return float|int
463     */
464    function getResizeRatio($maxwidth,$maxheight=0){
465        if(!$maxheight) $maxheight = $maxwidth;
466
467        $w = $this->getField('File.Width');
468        $h = $this->getField('File.Height');
469
470        $ratio = 1;
471        if($w >= $h){
472            if($w >= $maxwidth){
473                $ratio = $maxwidth/$w;
474            }elseif($h > $maxheight){
475                $ratio = $maxheight/$h;
476            }
477        }else{
478            if($h >= $maxheight){
479                $ratio = $maxheight/$h;
480            }elseif($w > $maxwidth){
481                $ratio = $maxwidth/$w;
482            }
483        }
484        return $ratio;
485    }
486
487
488    /**
489     * Set an IPTC field
490     *
491     * @author Sebastian Delmont <sdelmont@zonageek.com>
492     *
493     * @param string $field field name
494     * @param string $value
495     * @return bool
496     */
497    function setIPTCField($field, $value) {
498        if (!isset($this->_info['iptc'])) {
499            $this->_parseMarkerAdobe();
500        }
501
502        if ($this->_markers == null) {
503            return false;
504        }
505
506        if ($this->_info['iptc'] == false) {
507            $this->_info['iptc'] = array();
508        }
509
510        $this->_info['iptc'][$field] = $value;
511
512        return true;
513    }
514
515    /**
516     * Delete an EXIF field
517     *
518     * @author Sebastian Delmont <sdelmont@zonageek.com>
519     *
520     * @param string $field field name
521     * @return bool
522     */
523    function deleteExifField($field) {
524        if (!isset($this->_info['exif'])) {
525            $this->_parseMarkerAdobe();
526        }
527
528        if ($this->_markers == null) {
529            return false;
530        }
531
532        if ($this->_info['exif'] != false) {
533            unset($this->_info['exif'][$field]);
534        }
535
536        return true;
537    }
538
539    /**
540     * Delete an Adobe field
541     *
542     * @author Sebastian Delmont <sdelmont@zonageek.com>
543     *
544     * @param string $field field name
545     * @return bool
546     */
547    function deleteAdobeField($field) {
548        if (!isset($this->_info['adobe'])) {
549            $this->_parseMarkerAdobe();
550        }
551
552        if ($this->_markers == null) {
553            return false;
554        }
555
556        if ($this->_info['adobe'] != false) {
557            unset($this->_info['adobe'][$field]);
558        }
559
560        return true;
561    }
562
563    /**
564     * Delete an IPTC field
565     *
566     * @author Sebastian Delmont <sdelmont@zonageek.com>
567     *
568     * @param string $field field name
569     * @return bool
570     */
571    function deleteIPTCField($field) {
572        if (!isset($this->_info['iptc'])) {
573            $this->_parseMarkerAdobe();
574        }
575
576        if ($this->_markers == null) {
577            return false;
578        }
579
580        if ($this->_info['iptc'] != false) {
581            unset($this->_info['iptc'][$field]);
582        }
583
584        return true;
585    }
586
587    /**
588     * Get the image's title, tries various fields
589     *
590     * @param int $max maximum number chars (keeps words)
591     * @return false|string
592     *
593     * @author Andreas Gohr <andi@splitbrain.org>
594     */
595    function getTitle($max=80){
596        // try various fields
597        $cap = $this->getField(array('Iptc.Headline',
598                    'Iptc.Caption',
599                    'Xmp.dc:title',
600                    'Exif.UserComment',
601                    'Exif.TIFFUserComment',
602                    'Exif.TIFFImageDescription',
603                    'File.Name'));
604        if (empty($cap)) return false;
605
606        if(!$max) return $cap;
607        // Shorten to 80 chars (keeping words)
608        $new = preg_replace('/\n.+$/','',wordwrap($cap, $max));
609        if($new != $cap) $new .= '...';
610
611        return $new;
612    }
613
614    /**
615     * Gather various date fields
616     *
617     * @author Sebastian Delmont <sdelmont@zonageek.com>
618     *
619     * @return array|bool
620     */
621    function getDates() {
622        $this->_parseAll();
623        if ($this->_markers == null) {
624            if (@isset($this->_info['file']['UnixTime'])) {
625                $dates = array();
626                $dates['FileModified'] = $this->_info['file']['UnixTime'];
627                $dates['Time'] = $this->_info['file']['UnixTime'];
628                $dates['TimeSource'] = 'FileModified';
629                $dates['TimeStr'] = date("Y-m-d H:i:s", $this->_info['file']['UnixTime']);
630                $dates['EarliestTime'] = $this->_info['file']['UnixTime'];
631                $dates['EarliestTimeSource'] = 'FileModified';
632                $dates['EarliestTimeStr'] = date("Y-m-d H:i:s", $this->_info['file']['UnixTime']);
633                $dates['LatestTime'] = $this->_info['file']['UnixTime'];
634                $dates['LatestTimeSource'] = 'FileModified';
635                $dates['LatestTimeStr'] = date("Y-m-d H:i:s", $this->_info['file']['UnixTime']);
636                return $dates;
637            }
638            return false;
639        }
640
641        $dates = array();
642
643        $latestTime = 0;
644        $latestTimeSource = "";
645        $earliestTime = time();
646        $earliestTimeSource = "";
647
648        if (@isset($this->_info['exif']['DateTime'])) {
649            $dates['ExifDateTime'] = $this->_info['exif']['DateTime'];
650
651            $aux = $this->_info['exif']['DateTime'];
652            $aux{4} = "-";
653            $aux{7} = "-";
654            $t = strtotime($aux);
655
656            if ($t && $t > $latestTime) {
657                $latestTime = $t;
658                $latestTimeSource = "ExifDateTime";
659            }
660
661            if ($t && $t < $earliestTime) {
662                $earliestTime = $t;
663                $earliestTimeSource = "ExifDateTime";
664            }
665        }
666
667        if (@isset($this->_info['exif']['DateTimeOriginal'])) {
668            $dates['ExifDateTimeOriginal'] = $this->_info['exif']['DateTime'];
669
670            $aux = $this->_info['exif']['DateTimeOriginal'];
671            $aux{4} = "-";
672            $aux{7} = "-";
673            $t = strtotime($aux);
674
675            if ($t && $t > $latestTime) {
676                $latestTime = $t;
677                $latestTimeSource = "ExifDateTimeOriginal";
678            }
679
680            if ($t && $t < $earliestTime) {
681                $earliestTime = $t;
682                $earliestTimeSource = "ExifDateTimeOriginal";
683            }
684        }
685
686        if (@isset($this->_info['exif']['DateTimeDigitized'])) {
687            $dates['ExifDateTimeDigitized'] = $this->_info['exif']['DateTime'];
688
689            $aux = $this->_info['exif']['DateTimeDigitized'];
690            $aux{4} = "-";
691            $aux{7} = "-";
692            $t = strtotime($aux);
693
694            if ($t && $t > $latestTime) {
695                $latestTime = $t;
696                $latestTimeSource = "ExifDateTimeDigitized";
697            }
698
699            if ($t && $t < $earliestTime) {
700                $earliestTime = $t;
701                $earliestTimeSource = "ExifDateTimeDigitized";
702            }
703        }
704
705        if (@isset($this->_info['iptc']['DateCreated'])) {
706            $dates['IPTCDateCreated'] = $this->_info['iptc']['DateCreated'];
707
708            $aux = $this->_info['iptc']['DateCreated'];
709            $aux = substr($aux, 0, 4) . "-" . substr($aux, 4, 2) . "-" . substr($aux, 6, 2);
710            $t = strtotime($aux);
711
712            if ($t && $t > $latestTime) {
713                $latestTime = $t;
714                $latestTimeSource = "IPTCDateCreated";
715            }
716
717            if ($t && $t < $earliestTime) {
718                $earliestTime = $t;
719                $earliestTimeSource = "IPTCDateCreated";
720            }
721        }
722
723        if (@isset($this->_info['file']['UnixTime'])) {
724            $dates['FileModified'] = $this->_info['file']['UnixTime'];
725
726            $t = $this->_info['file']['UnixTime'];
727
728            if ($t && $t > $latestTime) {
729                $latestTime = $t;
730                $latestTimeSource = "FileModified";
731            }
732
733            if ($t && $t < $earliestTime) {
734                $earliestTime = $t;
735                $earliestTimeSource = "FileModified";
736            }
737        }
738
739        $dates['Time'] = $earliestTime;
740        $dates['TimeSource'] = $earliestTimeSource;
741        $dates['TimeStr'] = date("Y-m-d H:i:s", $earliestTime);
742        $dates['EarliestTime'] = $earliestTime;
743        $dates['EarliestTimeSource'] = $earliestTimeSource;
744        $dates['EarliestTimeStr'] = date("Y-m-d H:i:s", $earliestTime);
745        $dates['LatestTime'] = $latestTime;
746        $dates['LatestTimeSource'] = $latestTimeSource;
747        $dates['LatestTimeStr'] = date("Y-m-d H:i:s", $latestTime);
748
749        return $dates;
750    }
751
752    /**
753     * Get the image width, tries various fields
754     *
755     * @author Sebastian Delmont <sdelmont@zonageek.com>
756     *
757     * @return false|string
758     */
759    function getWidth() {
760        if (!isset($this->_info['sof'])) {
761            $this->_parseMarkerSOF();
762        }
763
764        if ($this->_markers == null) {
765            return false;
766        }
767
768        if (isset($this->_info['sof']['ImageWidth'])) {
769            return $this->_info['sof']['ImageWidth'];
770        }
771
772        if (!isset($this->_info['exif'])) {
773            $this->_parseMarkerExif();
774        }
775
776        if (isset($this->_info['exif']['PixelXDimension'])) {
777            return $this->_info['exif']['PixelXDimension'];
778        }
779
780        return false;
781    }
782
783    /**
784     * Get the image height, tries various fields
785     *
786     * @author Sebastian Delmont <sdelmont@zonageek.com>
787     *
788     * @return false|string
789     */
790    function getHeight() {
791        if (!isset($this->_info['sof'])) {
792            $this->_parseMarkerSOF();
793        }
794
795        if ($this->_markers == null) {
796            return false;
797        }
798
799        if (isset($this->_info['sof']['ImageHeight'])) {
800            return $this->_info['sof']['ImageHeight'];
801        }
802
803        if (!isset($this->_info['exif'])) {
804            $this->_parseMarkerExif();
805        }
806
807        if (isset($this->_info['exif']['PixelYDimension'])) {
808            return $this->_info['exif']['PixelYDimension'];
809        }
810
811        return false;
812    }
813
814    /**
815     * Get an dimension string for use in img tag
816     *
817     * @author Sebastian Delmont <sdelmont@zonageek.com>
818     *
819     * @return false|string
820     */
821    function getDimStr() {
822        if ($this->_markers == null) {
823            return false;
824        }
825
826        $w = $this->getWidth();
827        $h = $this->getHeight();
828
829        return "width='" . $w . "' height='" . $h . "'";
830    }
831
832    /**
833     * Checks for an embedded thumbnail
834     *
835     * @author Sebastian Delmont <sdelmont@zonageek.com>
836     *
837     * @param string $which possible values: 'any', 'exif' or 'adobe'
838     * @return false|string
839     */
840    function hasThumbnail($which = 'any') {
841        if (($which == 'any') || ($which == 'exif')) {
842            if (!isset($this->_info['exif'])) {
843                $this->_parseMarkerExif();
844            }
845
846            if ($this->_markers == null) {
847                return false;
848            }
849
850            if (isset($this->_info['exif']) && is_array($this->_info['exif'])) {
851                if (isset($this->_info['exif']['JFIFThumbnail'])) {
852                    return 'exif';
853                }
854            }
855        }
856
857        if ($which == 'adobe') {
858            if (!isset($this->_info['adobe'])) {
859                $this->_parseMarkerAdobe();
860            }
861
862            if ($this->_markers == null) {
863                return false;
864            }
865
866            if (isset($this->_info['adobe']) && is_array($this->_info['adobe'])) {
867                if (isset($this->_info['adobe']['ThumbnailData'])) {
868                    return 'exif';
869                }
870            }
871        }
872
873        return false;
874    }
875
876    /**
877     * Send embedded thumbnail to browser
878     *
879     * @author Sebastian Delmont <sdelmont@zonageek.com>
880     *
881     * @param string $which possible values: 'any', 'exif' or 'adobe'
882     * @return bool
883     */
884    function sendThumbnail($which = 'any') {
885        $data = null;
886
887        if (($which == 'any') || ($which == 'exif')) {
888            if (!isset($this->_info['exif'])) {
889                $this->_parseMarkerExif();
890            }
891
892            if ($this->_markers == null) {
893                return false;
894            }
895
896            if (isset($this->_info['exif']) && is_array($this->_info['exif'])) {
897                if (isset($this->_info['exif']['JFIFThumbnail'])) {
898                    $data =& $this->_info['exif']['JFIFThumbnail'];
899                }
900            }
901        }
902
903        if (($which == 'adobe') || ($data == null)){
904            if (!isset($this->_info['adobe'])) {
905                $this->_parseMarkerAdobe();
906            }
907
908            if ($this->_markers == null) {
909                return false;
910            }
911
912            if (isset($this->_info['adobe']) && is_array($this->_info['adobe'])) {
913                if (isset($this->_info['adobe']['ThumbnailData'])) {
914                    $data =& $this->_info['adobe']['ThumbnailData'];
915                }
916            }
917        }
918
919        if ($data != null) {
920            header("Content-type: image/jpeg");
921            echo $data;
922            return true;
923        }
924
925        return false;
926    }
927
928    /**
929     * Save changed Metadata
930     *
931     * @author Sebastian Delmont <sdelmont@zonageek.com>
932     * @author Andreas Gohr <andi@splitbrain.org>
933     *
934     * @param string $fileName file name or empty string for a random name
935     * @return bool
936     */
937    function save($fileName = "") {
938        if ($fileName == "") {
939            $tmpName = tempnam(dirname($this->_fileName),'_metatemp_');
940            $this->_writeJPEG($tmpName);
941            if (file_exists($tmpName)) {
942                return io_rename($tmpName, $this->_fileName);
943            }
944        } else {
945            return $this->_writeJPEG($fileName);
946        }
947        return false;
948    }
949
950    /*************************************************************/
951    /* PRIVATE FUNCTIONS (Internal Use Only!)                    */
952    /*************************************************************/
953
954    /*************************************************************/
955    function _dispose($fileName = "") {
956        $this->_fileName = $fileName;
957
958        $this->_fp = null;
959        $this->_type = 'unknown';
960
961        unset($this->_markers);
962        unset($this->_info);
963    }
964
965    /*************************************************************/
966    function _readJPEG() {
967        unset($this->_markers);
968        //unset($this->_info);
969        $this->_markers = array();
970        //$this->_info = array();
971
972        $this->_fp = @fopen($this->_fileName, 'rb');
973        if ($this->_fp) {
974            if (file_exists($this->_fileName)) {
975                $this->_type = 'file';
976            }
977            else {
978                $this->_type = 'url';
979            }
980        } else {
981            $this->_fp = null;
982            return false;  // ERROR: Can't open file
983        }
984
985        // Check for the JPEG signature
986        $c1 = ord(fgetc($this->_fp));
987        $c2 = ord(fgetc($this->_fp));
988
989        if ($c1 != 0xFF || $c2 != 0xD8) {   // (0xFF + SOI)
990            $this->_markers = null;
991            return false;  // ERROR: File is not a JPEG
992        }
993
994        $count = 0;
995
996        $done = false;
997        $ok = true;
998
999        while (!$done) {
1000            $capture = false;
1001
1002            // First, skip any non 0xFF bytes
1003            $discarded = 0;
1004            $c = ord(fgetc($this->_fp));
1005            while (!feof($this->_fp) && ($c != 0xFF)) {
1006                $discarded++;
1007                $c = ord(fgetc($this->_fp));
1008            }
1009            // Then skip all 0xFF until the marker byte
1010            do {
1011                $marker = ord(fgetc($this->_fp));
1012            } while (!feof($this->_fp) && ($marker == 0xFF));
1013
1014            if (feof($this->_fp)) {
1015                return false; // ERROR: Unexpected EOF
1016            }
1017            if ($discarded != 0) {
1018                return false; // ERROR: Extraneous data
1019            }
1020
1021            $length = ord(fgetc($this->_fp)) * 256 + ord(fgetc($this->_fp));
1022            if (feof($this->_fp)) {
1023                return false; // ERROR: Unexpected EOF
1024            }
1025            if ($length < 2) {
1026                return false; // ERROR: Extraneous data
1027            }
1028            $length = $length - 2; // The length we got counts itself
1029
1030            switch ($marker) {
1031                case 0xC0:    // SOF0
1032                case 0xC1:    // SOF1
1033                case 0xC2:    // SOF2
1034                case 0xC9:    // SOF9
1035                case 0xE0:    // APP0: JFIF data
1036                case 0xE1:    // APP1: EXIF or XMP data
1037                case 0xED:    // APP13: IPTC / Photoshop data
1038                    $capture = true;
1039                    break;
1040                case 0xDA:    // SOS: Start of scan... the image itself and the last block on the file
1041                    $capture = false;
1042                    $length = -1;  // This field has no length... it includes all data until EOF
1043                    $done = true;
1044                    break;
1045                default:
1046                    $capture = true;//false;
1047                    break;
1048            }
1049
1050            $this->_markers[$count] = array();
1051            $this->_markers[$count]['marker'] = $marker;
1052            $this->_markers[$count]['length'] = $length;
1053
1054            if ($capture) {
1055                if ($length)
1056                    $this->_markers[$count]['data'] = fread($this->_fp, $length);
1057                else
1058                    $this->_markers[$count]['data'] = "";
1059            }
1060            elseif (!$done) {
1061                $result = @fseek($this->_fp, $length, SEEK_CUR);
1062                // fseek doesn't seem to like HTTP 'files', but fgetc has no problem
1063                if (!($result === 0)) {
1064                    for ($i = 0; $i < $length; $i++) {
1065                        fgetc($this->_fp);
1066                    }
1067                }
1068            }
1069            $count++;
1070        }
1071
1072        if ($this->_fp) {
1073            fclose($this->_fp);
1074            $this->_fp = null;
1075        }
1076
1077        return $ok;
1078    }
1079
1080    /*************************************************************/
1081    function _parseAll() {
1082        if (!isset($this->_info['file'])) {
1083            $this->_parseFileInfo();
1084        }
1085        if (!isset($this->_markers)) {
1086            $this->_readJPEG();
1087        }
1088
1089        if ($this->_markers == null) {
1090            return false;
1091        }
1092
1093        if (!isset($this->_info['jfif'])) {
1094            $this->_parseMarkerJFIF();
1095        }
1096        if (!isset($this->_info['jpeg'])) {
1097            $this->_parseMarkerSOF();
1098        }
1099        if (!isset($this->_info['exif'])) {
1100            $this->_parseMarkerExif();
1101        }
1102        if (!isset($this->_info['xmp'])) {
1103            $this->_parseMarkerXmp();
1104        }
1105        if (!isset($this->_info['adobe'])) {
1106            $this->_parseMarkerAdobe();
1107        }
1108    }
1109
1110    /*************************************************************/
1111
1112    /**
1113     * @param string $outputName
1114     *
1115     * @return bool
1116     */
1117    function _writeJPEG($outputName) {
1118        $this->_parseAll();
1119
1120        $wroteEXIF = false;
1121        $wroteAdobe = false;
1122
1123        $this->_fp = @fopen($this->_fileName, 'r');
1124        if ($this->_fp) {
1125            if (file_exists($this->_fileName)) {
1126                $this->_type = 'file';
1127            }
1128            else {
1129                $this->_type = 'url';
1130            }
1131        } else {
1132            $this->_fp = null;
1133            return false;  // ERROR: Can't open file
1134        }
1135
1136        $this->_fpout = fopen($outputName, 'wb');
1137        if (!$this->_fpout) {
1138            $this->_fpout = null;
1139            fclose($this->_fp);
1140            $this->_fp = null;
1141            return false;  // ERROR: Can't open output file
1142        }
1143
1144        // Check for the JPEG signature
1145        $c1 = ord(fgetc($this->_fp));
1146        $c2 = ord(fgetc($this->_fp));
1147
1148        if ($c1 != 0xFF || $c2 != 0xD8) {   // (0xFF + SOI)
1149            return false;  // ERROR: File is not a JPEG
1150        }
1151
1152        fputs($this->_fpout, chr(0xFF), 1);
1153        fputs($this->_fpout, chr(0xD8), 1); // (0xFF + SOI)
1154
1155        $count = 0;
1156
1157        $done = false;
1158        $ok = true;
1159
1160        while (!$done) {
1161            // First, skip any non 0xFF bytes
1162            $discarded = 0;
1163            $c = ord(fgetc($this->_fp));
1164            while (!feof($this->_fp) && ($c != 0xFF)) {
1165                $discarded++;
1166                $c = ord(fgetc($this->_fp));
1167            }
1168            // Then skip all 0xFF until the marker byte
1169            do {
1170                $marker = ord(fgetc($this->_fp));
1171            } while (!feof($this->_fp) && ($marker == 0xFF));
1172
1173            if (feof($this->_fp)) {
1174                $ok = false;
1175                break; // ERROR: Unexpected EOF
1176            }
1177            if ($discarded != 0) {
1178                $ok = false;
1179                break; // ERROR: Extraneous data
1180            }
1181
1182            $length = ord(fgetc($this->_fp)) * 256 + ord(fgetc($this->_fp));
1183            if (feof($this->_fp)) {
1184                $ok = false;
1185                break; // ERROR: Unexpected EOF
1186            }
1187            if ($length < 2) {
1188                $ok = false;
1189                break; // ERROR: Extraneous data
1190            }
1191            $length = $length - 2; // The length we got counts itself
1192
1193            unset($data);
1194            if ($marker == 0xE1) { // APP1: EXIF data
1195                $data =& $this->_createMarkerEXIF();
1196                $wroteEXIF = true;
1197            }
1198            elseif ($marker == 0xED) { // APP13: IPTC / Photoshop data
1199                $data =& $this->_createMarkerAdobe();
1200                $wroteAdobe = true;
1201            }
1202            elseif ($marker == 0xDA) { // SOS: Start of scan... the image itself and the last block on the file
1203                $done = true;
1204            }
1205
1206            if (!$wroteEXIF && (($marker < 0xE0) || ($marker > 0xEF))) {
1207                if (isset($this->_info['exif']) && is_array($this->_info['exif'])) {
1208                    $exif =& $this->_createMarkerEXIF();
1209                    $this->_writeJPEGMarker(0xE1, strlen($exif), $exif, 0);
1210                    unset($exif);
1211                }
1212                $wroteEXIF = true;
1213            }
1214
1215            if (!$wroteAdobe && (($marker < 0xE0) || ($marker > 0xEF))) {
1216                if ((isset($this->_info['adobe']) && is_array($this->_info['adobe']))
1217                        || (isset($this->_info['iptc']) && is_array($this->_info['iptc']))) {
1218                    $adobe =& $this->_createMarkerAdobe();
1219                    $this->_writeJPEGMarker(0xED, strlen($adobe), $adobe, 0);
1220                    unset($adobe);
1221                }
1222                $wroteAdobe = true;
1223            }
1224
1225            $origLength = $length;
1226            if (isset($data)) {
1227                $length = strlen($data);
1228            }
1229
1230            if ($marker != -1) {
1231                $this->_writeJPEGMarker($marker, $length, $data, $origLength);
1232            }
1233        }
1234
1235        if ($this->_fp) {
1236            fclose($this->_fp);
1237            $this->_fp = null;
1238        }
1239
1240        if ($this->_fpout) {
1241            fclose($this->_fpout);
1242            $this->_fpout = null;
1243        }
1244
1245        return $ok;
1246    }
1247
1248    /*************************************************************/
1249
1250    /**
1251     * @param integer $marker
1252     * @param integer $length
1253     * @param string $data
1254     * @param integer $origLength
1255     *
1256     * @return bool
1257     */
1258    function _writeJPEGMarker($marker, $length, &$data, $origLength) {
1259        if ($length <= 0) {
1260            return false;
1261        }
1262
1263        fputs($this->_fpout, chr(0xFF), 1);
1264        fputs($this->_fpout, chr($marker), 1);
1265        fputs($this->_fpout, chr((($length + 2) & 0x0000FF00) >> 8), 1);
1266        fputs($this->_fpout, chr((($length + 2) & 0x000000FF) >> 0), 1);
1267
1268        if (isset($data)) {
1269            // Copy the generated data
1270            fputs($this->_fpout, $data, $length);
1271
1272            if ($origLength > 0) {   // Skip the original data
1273                $result = @fseek($this->_fp, $origLength, SEEK_CUR);
1274                // fseek doesn't seem to like HTTP 'files', but fgetc has no problem
1275                if ($result != 0) {
1276                    for ($i = 0; $i < $origLength; $i++) {
1277                        fgetc($this->_fp);
1278                    }
1279                }
1280            }
1281        } else {
1282            if ($marker == 0xDA) {  // Copy until EOF
1283                while (!feof($this->_fp)) {
1284                    $data = fread($this->_fp, 1024 * 16);
1285                    fputs($this->_fpout, $data, strlen($data));
1286                }
1287            } else { // Copy only $length bytes
1288                $data = @fread($this->_fp, $length);
1289                fputs($this->_fpout, $data, $length);
1290            }
1291        }
1292
1293        return true;
1294    }
1295
1296    /**
1297     * Gets basic info from the file - should work with non-JPEGs
1298     *
1299     * @author  Sebastian Delmont <sdelmont@zonageek.com>
1300     * @author  Andreas Gohr <andi@splitbrain.org>
1301     */
1302    function _parseFileInfo() {
1303        if (file_exists($this->_fileName) && is_file($this->_fileName)) {
1304            $this->_info['file'] = array();
1305            $this->_info['file']['Name'] = utf8_decodeFN(utf8_basename($this->_fileName));
1306            $this->_info['file']['Path'] = fullpath($this->_fileName);
1307            $this->_info['file']['Size'] = filesize($this->_fileName);
1308            if ($this->_info['file']['Size'] < 1024) {
1309                $this->_info['file']['NiceSize'] = $this->_info['file']['Size'] . 'B';
1310            } elseif ($this->_info['file']['Size'] < (1024 * 1024)) {
1311                $this->_info['file']['NiceSize'] = round($this->_info['file']['Size'] / 1024) . 'KB';
1312            } elseif ($this->_info['file']['Size'] < (1024 * 1024 * 1024)) {
1313                $this->_info['file']['NiceSize'] = round($this->_info['file']['Size'] / (1024*1024)) . 'MB';
1314            } else {
1315                $this->_info['file']['NiceSize'] = $this->_info['file']['Size'] . 'B';
1316            }
1317            $this->_info['file']['UnixTime'] = filemtime($this->_fileName);
1318
1319            // get image size directly from file
1320            $size = getimagesize($this->_fileName);
1321            $this->_info['file']['Width']  = $size[0];
1322            $this->_info['file']['Height'] = $size[1];
1323            // set mime types and formats
1324            // http://php.net/manual/en/function.getimagesize.php
1325            // http://php.net/manual/en/function.image-type-to-mime-type.php
1326            switch ($size[2]){
1327                case 1:
1328                    $this->_info['file']['Mime']   = 'image/gif';
1329                    $this->_info['file']['Format'] = 'GIF';
1330                    break;
1331                case 2:
1332                    $this->_info['file']['Mime']   = 'image/jpeg';
1333                    $this->_info['file']['Format'] = 'JPEG';
1334                    break;
1335                case 3:
1336                    $this->_info['file']['Mime']   = 'image/png';
1337                    $this->_info['file']['Format'] = 'PNG';
1338                    break;
1339                case 4:
1340                    $this->_info['file']['Mime']   = 'application/x-shockwave-flash';
1341                    $this->_info['file']['Format'] = 'SWF';
1342                    break;
1343                case 5:
1344                    $this->_info['file']['Mime']   = 'image/psd';
1345                    $this->_info['file']['Format'] = 'PSD';
1346                    break;
1347                case 6:
1348                    $this->_info['file']['Mime']   = 'image/bmp';
1349                    $this->_info['file']['Format'] = 'BMP';
1350                    break;
1351                case 7:
1352                    $this->_info['file']['Mime']   = 'image/tiff';
1353                    $this->_info['file']['Format'] = 'TIFF (Intel)';
1354                    break;
1355                case 8:
1356                    $this->_info['file']['Mime']   = 'image/tiff';
1357                    $this->_info['file']['Format'] = 'TIFF (Motorola)';
1358                    break;
1359                case 9:
1360                    $this->_info['file']['Mime']   = 'application/octet-stream';
1361                    $this->_info['file']['Format'] = 'JPC';
1362                    break;
1363                case 10:
1364                    $this->_info['file']['Mime']   = 'image/jp2';
1365                    $this->_info['file']['Format'] = 'JP2';
1366                    break;
1367                case 11:
1368                    $this->_info['file']['Mime']   = 'application/octet-stream';
1369                    $this->_info['file']['Format'] = 'JPX';
1370                    break;
1371                case 12:
1372                    $this->_info['file']['Mime']   = 'application/octet-stream';
1373                    $this->_info['file']['Format'] = 'JB2';
1374                    break;
1375                case 13:
1376                    $this->_info['file']['Mime']   = 'application/x-shockwave-flash';
1377                    $this->_info['file']['Format'] = 'SWC';
1378                    break;
1379                case 14:
1380                    $this->_info['file']['Mime']   = 'image/iff';
1381                    $this->_info['file']['Format'] = 'IFF';
1382                    break;
1383                case 15:
1384                    $this->_info['file']['Mime']   = 'image/vnd.wap.wbmp';
1385                    $this->_info['file']['Format'] = 'WBMP';
1386                    break;
1387                case 16:
1388                    $this->_info['file']['Mime']   = 'image/xbm';
1389                    $this->_info['file']['Format'] = 'XBM';
1390                    break;
1391                default:
1392                    $this->_info['file']['Mime']   = 'image/unknown';
1393            }
1394        } else {
1395            $this->_info['file'] = array();
1396            $this->_info['file']['Name'] = utf8_basename($this->_fileName);
1397            $this->_info['file']['Url'] = $this->_fileName;
1398        }
1399
1400        return true;
1401    }
1402
1403    /*************************************************************/
1404    function _parseMarkerJFIF() {
1405        if (!isset($this->_markers)) {
1406            $this->_readJPEG();
1407        }
1408
1409        if ($this->_markers == null) {
1410            return false;
1411        }
1412
1413        $data = null;
1414        $count = count($this->_markers);
1415        for ($i = 0; $i < $count; $i++) {
1416            if ($this->_markers[$i]['marker'] == 0xE0) {
1417                $signature = $this->_getFixedString($this->_markers[$i]['data'], 0, 4);
1418                if ($signature == 'JFIF') {
1419                    $data =& $this->_markers[$i]['data'];
1420                    break;
1421                }
1422            }
1423        }
1424
1425        if ($data == null) {
1426            $this->_info['jfif'] = false;
1427            return false;
1428        }
1429
1430        $this->_info['jfif'] = array();
1431
1432        $vmaj = $this->_getByte($data, 5);
1433        $vmin = $this->_getByte($data, 6);
1434
1435        $this->_info['jfif']['Version'] = sprintf('%d.%02d', $vmaj, $vmin);
1436
1437        $units = $this->_getByte($data, 7);
1438        switch ($units) {
1439            case 0:
1440                $this->_info['jfif']['Units'] = 'pixels';
1441                break;
1442            case 1:
1443                $this->_info['jfif']['Units'] = 'dpi';
1444                break;
1445            case 2:
1446                $this->_info['jfif']['Units'] = 'dpcm';
1447                break;
1448            default:
1449                $this->_info['jfif']['Units'] = 'unknown';
1450                break;
1451        }
1452
1453        $xdens = $this->_getShort($data, 8);
1454        $ydens = $this->_getShort($data, 10);
1455
1456        $this->_info['jfif']['XDensity'] = $xdens;
1457        $this->_info['jfif']['YDensity'] = $ydens;
1458
1459        $thumbx = $this->_getByte($data, 12);
1460        $thumby = $this->_getByte($data, 13);
1461
1462        $this->_info['jfif']['ThumbnailWidth'] = $thumbx;
1463        $this->_info['jfif']['ThumbnailHeight'] = $thumby;
1464
1465        return true;
1466    }
1467
1468    /*************************************************************/
1469    function _parseMarkerSOF() {
1470        if (!isset($this->_markers)) {
1471            $this->_readJPEG();
1472        }
1473
1474        if ($this->_markers == null) {
1475            return false;
1476        }
1477
1478        $data = null;
1479        $count = count($this->_markers);
1480        for ($i = 0; $i < $count; $i++) {
1481            switch ($this->_markers[$i]['marker']) {
1482                case 0xC0: // SOF0
1483                case 0xC1: // SOF1
1484                case 0xC2: // SOF2
1485                case 0xC9: // SOF9
1486                    $data =& $this->_markers[$i]['data'];
1487                    $marker = $this->_markers[$i]['marker'];
1488                    break;
1489            }
1490        }
1491
1492        if ($data == null) {
1493            $this->_info['sof'] = false;
1494            return false;
1495        }
1496
1497        $pos = 0;
1498        $this->_info['sof'] = array();
1499
1500        switch ($marker) {
1501            case 0xC0: // SOF0
1502                $format = 'Baseline';
1503                break;
1504            case 0xC1: // SOF1
1505                $format = 'Progessive';
1506                break;
1507            case 0xC2: // SOF2
1508                $format = 'Non-baseline';
1509                break;
1510            case 0xC9: // SOF9
1511                $format = 'Arithmetic';
1512                break;
1513            default:
1514                return false;
1515        }
1516
1517        $this->_info['sof']['Format']          = $format;
1518        $this->_info['sof']['SamplePrecision'] = $this->_getByte($data, $pos + 0);
1519        $this->_info['sof']['ImageHeight']     = $this->_getShort($data, $pos + 1);
1520        $this->_info['sof']['ImageWidth']      = $this->_getShort($data, $pos + 3);
1521        $this->_info['sof']['ColorChannels']   = $this->_getByte($data, $pos + 5);
1522
1523        return true;
1524    }
1525
1526    /**
1527     * Parses the XMP data
1528     *
1529     * @author  Hakan Sandell <hakan.sandell@mydata.se>
1530     */
1531    function _parseMarkerXmp() {
1532        if (!isset($this->_markers)) {
1533            $this->_readJPEG();
1534        }
1535
1536        if ($this->_markers == null) {
1537            return false;
1538        }
1539
1540        $data = null;
1541        $count = count($this->_markers);
1542        for ($i = 0; $i < $count; $i++) {
1543            if ($this->_markers[$i]['marker'] == 0xE1) {
1544                $signature = $this->_getFixedString($this->_markers[$i]['data'], 0, 29);
1545                if ($signature == "http://ns.adobe.com/xap/1.0/\0") {
1546                    $data = substr($this->_markers[$i]['data'], 29);
1547                    break;
1548                }
1549            }
1550        }
1551
1552        if ($data == null) {
1553            $this->_info['xmp'] = false;
1554            return false;
1555        }
1556
1557        $parser = xml_parser_create();
1558        xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
1559        xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
1560        $result = xml_parse_into_struct($parser, $data, $values, $tags);
1561        xml_parser_free($parser);
1562
1563        if ($result == 0) {
1564            $this->_info['xmp'] = false;
1565            return false;
1566        }
1567
1568        $this->_info['xmp'] = array();
1569        $count = count($values);
1570        for ($i = 0; $i < $count; $i++) {
1571            if ($values[$i]['tag'] == 'rdf:Description' && $values[$i]['type'] == 'open') {
1572
1573                while ((++$i < $count) && ($values[$i]['tag'] != 'rdf:Description')) {
1574                    $this->_parseXmpNode($values, $i, $this->_info['xmp'][$values[$i]['tag']], $count);
1575                }
1576            }
1577        }
1578        return true;
1579    }
1580
1581    /**
1582     * Parses XMP nodes by recursion
1583     *
1584     * @author  Hakan Sandell <hakan.sandell@mydata.se>
1585     *
1586     * @param array $values
1587     * @param int $i
1588     * @param mixed $meta
1589     * @param integer $count
1590     */
1591    function _parseXmpNode($values, &$i, &$meta, $count) {
1592        if ($values[$i]['type'] == 'close') return;
1593
1594        if ($values[$i]['type'] == 'complete') {
1595            // Simple Type property
1596            $meta = $values[$i]['value'];
1597            return;
1598        }
1599
1600        $i++;
1601        if ($i >= $count) return;
1602
1603        if ($values[$i]['tag'] == 'rdf:Bag' || $values[$i]['tag'] == 'rdf:Seq') {
1604            // Array property
1605            $meta = array();
1606            while ($values[++$i]['tag'] == 'rdf:li') {
1607                $this->_parseXmpNode($values, $i, $meta[], $count);
1608            }
1609            $i++; // skip closing Bag/Seq tag
1610
1611        } elseif ($values[$i]['tag'] == 'rdf:Alt') {
1612            // Language Alternative property, only the first (default) value is used
1613            if ($values[$i]['type'] == 'open') {
1614                $i++;
1615                $this->_parseXmpNode($values, $i, $meta, $count);
1616                while ((++$i < $count) && ($values[$i]['tag'] != 'rdf:Alt'));
1617                $i++; // skip closing Alt tag
1618            }
1619
1620        } else {
1621            // Structure property
1622            $meta = array();
1623            $startTag = $values[$i-1]['tag'];
1624            do {
1625                $this->_parseXmpNode($values, $i, $meta[$values[$i]['tag']], $count);
1626            } while ((++$i < $count) && ($values[$i]['tag'] != $startTag));
1627        }
1628    }
1629
1630    /*************************************************************/
1631    function _parseMarkerExif() {
1632        if (!isset($this->_markers)) {
1633            $this->_readJPEG();
1634        }
1635
1636        if ($this->_markers == null) {
1637            return false;
1638        }
1639
1640        $data = null;
1641        $count = count($this->_markers);
1642        for ($i = 0; $i < $count; $i++) {
1643            if ($this->_markers[$i]['marker'] == 0xE1) {
1644                $signature = $this->_getFixedString($this->_markers[$i]['data'], 0, 6);
1645                if ($signature == "Exif\0\0") {
1646                    $data =& $this->_markers[$i]['data'];
1647                    break;
1648                }
1649            }
1650        }
1651
1652        if ($data == null) {
1653            $this->_info['exif'] = false;
1654            return false;
1655        }
1656        $pos = 6;
1657        $this->_info['exif'] = array();
1658
1659        // We don't increment $pos after this because Exif uses offsets relative to this point
1660
1661        $byteAlign = $this->_getShort($data, $pos + 0);
1662
1663        if ($byteAlign == 0x4949) { // "II"
1664            $isBigEndian = false;
1665        } elseif ($byteAlign == 0x4D4D) { // "MM"
1666            $isBigEndian = true;
1667        } else {
1668            return false; // Unexpected data
1669        }
1670
1671        $alignCheck = $this->_getShort($data, $pos + 2, $isBigEndian);
1672        if ($alignCheck != 0x002A) // That's the expected value
1673            return false; // Unexpected data
1674
1675        if ($isBigEndian) {
1676            $this->_info['exif']['ByteAlign'] = "Big Endian";
1677        } else {
1678            $this->_info['exif']['ByteAlign'] = "Little Endian";
1679        }
1680
1681        $offsetIFD0 = $this->_getLong($data, $pos + 4, $isBigEndian);
1682        if ($offsetIFD0 < 8)
1683            return false; // Unexpected data
1684
1685        $offsetIFD1 = $this->_readIFD($data, $pos, $offsetIFD0, $isBigEndian, 'ifd0');
1686        if ($offsetIFD1 != 0)
1687            $this->_readIFD($data, $pos, $offsetIFD1, $isBigEndian, 'ifd1');
1688
1689        return true;
1690    }
1691
1692    /*************************************************************/
1693
1694    /**
1695     * @param mixed $data
1696     * @param integer $base
1697     * @param integer $offset
1698     * @param boolean $isBigEndian
1699     * @param string $mode
1700     *
1701     * @return int
1702     */
1703    function _readIFD($data, $base, $offset, $isBigEndian, $mode) {
1704        $EXIFTags = $this->_exifTagNames($mode);
1705
1706        $numEntries = $this->_getShort($data, $base + $offset, $isBigEndian);
1707        $offset += 2;
1708
1709        $exifTIFFOffset = 0;
1710        $exifTIFFLength = 0;
1711        $exifThumbnailOffset = 0;
1712        $exifThumbnailLength = 0;
1713
1714        for ($i = 0; $i < $numEntries; $i++) {
1715            $tag = $this->_getShort($data, $base + $offset, $isBigEndian);
1716            $offset += 2;
1717            $type = $this->_getShort($data, $base + $offset, $isBigEndian);
1718            $offset += 2;
1719            $count = $this->_getLong($data, $base + $offset, $isBigEndian);
1720            $offset += 4;
1721
1722            if (($type < 1) || ($type > 12))
1723                return false; // Unexpected Type
1724
1725            $typeLengths = array( -1, 1, 1, 2, 4, 8, 1, 1, 2, 4, 8, 4, 8 );
1726
1727            $dataLength = $typeLengths[$type] * $count;
1728            if ($dataLength > 4) {
1729                $dataOffset = $this->_getLong($data, $base + $offset, $isBigEndian);
1730                $rawValue = $this->_getFixedString($data, $base + $dataOffset, $dataLength);
1731            } else {
1732                $rawValue = $this->_getFixedString($data, $base + $offset, $dataLength);
1733            }
1734            $offset += 4;
1735
1736            switch ($type) {
1737                case 1:    // UBYTE
1738                    if ($count == 1) {
1739                        $value = $this->_getByte($rawValue, 0);
1740                    } else {
1741                        $value = array();
1742                        for ($j = 0; $j < $count; $j++)
1743                            $value[$j] = $this->_getByte($rawValue, $j);
1744                    }
1745                    break;
1746                case 2:    // ASCII
1747                    $value = $rawValue;
1748                    break;
1749                case 3:    // USHORT
1750                    if ($count == 1) {
1751                        $value = $this->_getShort($rawValue, 0, $isBigEndian);
1752                    } else {
1753                        $value = array();
1754                        for ($j = 0; $j < $count; $j++)
1755                            $value[$j] = $this->_getShort($rawValue, $j * 2, $isBigEndian);
1756                    }
1757                    break;
1758                case 4:    // ULONG
1759                    if ($count == 1) {
1760                        $value = $this->_getLong($rawValue, 0, $isBigEndian);
1761                    } else {
1762                        $value = array();
1763                        for ($j = 0; $j < $count; $j++)
1764                            $value[$j] = $this->_getLong($rawValue, $j * 4, $isBigEndian);
1765                    }
1766                    break;
1767                case 5:    // URATIONAL
1768                    if ($count == 1) {
1769                        $a = $this->_getLong($rawValue, 0, $isBigEndian);
1770                        $b = $this->_getLong($rawValue, 4, $isBigEndian);
1771                        $value = array();
1772                        $value['val'] = 0;
1773                        $value['num'] = $a;
1774                        $value['den'] = $b;
1775                        if (($a != 0) && ($b != 0)) {
1776                            $value['val'] = $a / $b;
1777                        }
1778                    } else {
1779                        $value = array();
1780                        for ($j = 0; $j < $count; $j++) {
1781                            $a = $this->_getLong($rawValue, $j * 8, $isBigEndian);
1782                            $b = $this->_getLong($rawValue, ($j * 8) + 4, $isBigEndian);
1783                            $value = array();
1784                            $value[$j]['val'] = 0;
1785                            $value[$j]['num'] = $a;
1786                            $value[$j]['den'] = $b;
1787                            if (($a != 0) && ($b != 0))
1788                                $value[$j]['val'] = $a / $b;
1789                        }
1790                    }
1791                    break;
1792                case 6:    // SBYTE
1793                    if ($count == 1) {
1794                        $value = $this->_getByte($rawValue, 0);
1795                    } else {
1796                        $value = array();
1797                        for ($j = 0; $j < $count; $j++)
1798                            $value[$j] = $this->_getByte($rawValue, $j);
1799                    }
1800                    break;
1801                case 7:    // UNDEFINED
1802                    $value = $rawValue;
1803                    break;
1804                case 8:    // SSHORT
1805                    if ($count == 1) {
1806                        $value = $this->_getShort($rawValue, 0, $isBigEndian);
1807                    } else {
1808                        $value = array();
1809                        for ($j = 0; $j < $count; $j++)
1810                            $value[$j] = $this->_getShort($rawValue, $j * 2, $isBigEndian);
1811                    }
1812                    break;
1813                case 9:    // SLONG
1814                    if ($count == 1) {
1815                        $value = $this->_getLong($rawValue, 0, $isBigEndian);
1816                    } else {
1817                        $value = array();
1818                        for ($j = 0; $j < $count; $j++)
1819                            $value[$j] = $this->_getLong($rawValue, $j * 4, $isBigEndian);
1820                    }
1821                    break;
1822                case 10:   // SRATIONAL
1823                    if ($count == 1) {
1824                        $a = $this->_getLong($rawValue, 0, $isBigEndian);
1825                        $b = $this->_getLong($rawValue, 4, $isBigEndian);
1826                        $value = array();
1827                        $value['val'] = 0;
1828                        $value['num'] = $a;
1829                        $value['den'] = $b;
1830                        if (($a != 0) && ($b != 0))
1831                            $value['val'] = $a / $b;
1832                    } else {
1833                        $value = array();
1834                        for ($j = 0; $j < $count; $j++) {
1835                            $a = $this->_getLong($rawValue, $j * 8, $isBigEndian);
1836                            $b = $this->_getLong($rawValue, ($j * 8) + 4, $isBigEndian);
1837                            $value = array();
1838                            $value[$j]['val'] = 0;
1839                            $value[$j]['num'] = $a;
1840                            $value[$j]['den'] = $b;
1841                            if (($a != 0) && ($b != 0))
1842                                $value[$j]['val'] = $a / $b;
1843                        }
1844                    }
1845                    break;
1846                case 11:   // FLOAT
1847                    $value = $rawValue;
1848                    break;
1849
1850                case 12:   // DFLOAT
1851                    $value = $rawValue;
1852                    break;
1853                default:
1854                    return false; // Unexpected Type
1855            }
1856
1857            $tagName = '';
1858            if (($mode == 'ifd0') && ($tag == 0x8769)) {  // ExifIFDOffset
1859                $this->_readIFD($data, $base, $value, $isBigEndian, 'exif');
1860            } elseif (($mode == 'ifd0') && ($tag == 0x8825)) {  // GPSIFDOffset
1861                $this->_readIFD($data, $base, $value, $isBigEndian, 'gps');
1862            } elseif (($mode == 'ifd1') && ($tag == 0x0111)) {  // TIFFStripOffsets
1863                $exifTIFFOffset = $value;
1864            } elseif (($mode == 'ifd1') && ($tag == 0x0117)) {  // TIFFStripByteCounts
1865                $exifTIFFLength = $value;
1866            } elseif (($mode == 'ifd1') && ($tag == 0x0201)) {  // TIFFJFIFOffset
1867                $exifThumbnailOffset = $value;
1868            } elseif (($mode == 'ifd1') && ($tag == 0x0202)) {  // TIFFJFIFLength
1869                $exifThumbnailLength = $value;
1870            } elseif (($mode == 'exif') && ($tag == 0xA005)) {  // InteropIFDOffset
1871                $this->_readIFD($data, $base, $value, $isBigEndian, 'interop');
1872            }
1873            // elseif (($mode == 'exif') && ($tag == 0x927C)) {  // MakerNote
1874            // }
1875            else {
1876                if (isset($EXIFTags[$tag])) {
1877                    $tagName = $EXIFTags[$tag];
1878                    if (isset($this->_info['exif'][$tagName])) {
1879                        if (!is_array($this->_info['exif'][$tagName])) {
1880                            $aux = array();
1881                            $aux[0] = $this->_info['exif'][$tagName];
1882                            $this->_info['exif'][$tagName] = $aux;
1883                        }
1884
1885                        $this->_info['exif'][$tagName][count($this->_info['exif'][$tagName])] = $value;
1886                    } else {
1887                        $this->_info['exif'][$tagName] = $value;
1888                    }
1889                }
1890                /*
1891                 else {
1892                    echo sprintf("<h1>Unknown tag %02x (t: %d l: %d) %s in %s</h1>", $tag, $type, $count, $mode, $this->_fileName);
1893                    // Unknown Tags will be ignored!!!
1894                    // That's because the tag might be a pointer (like the Exif tag)
1895                    // and saving it without saving the data it points to might
1896                    // create an invalid file.
1897                }
1898                */
1899            }
1900        }
1901
1902        if (($exifThumbnailOffset > 0) && ($exifThumbnailLength > 0)) {
1903            $this->_info['exif']['JFIFThumbnail'] = $this->_getFixedString($data, $base + $exifThumbnailOffset, $exifThumbnailLength);
1904        }
1905
1906        if (($exifTIFFOffset > 0) && ($exifTIFFLength > 0)) {
1907            $this->_info['exif']['TIFFStrips'] = $this->_getFixedString($data, $base + $exifTIFFOffset, $exifTIFFLength);
1908        }
1909
1910        $nextOffset = $this->_getLong($data, $base + $offset, $isBigEndian);
1911        return $nextOffset;
1912    }
1913
1914    /*************************************************************/
1915    function & _createMarkerExif() {
1916        $data = null;
1917        $count = count($this->_markers);
1918        for ($i = 0; $i < $count; $i++) {
1919            if ($this->_markers[$i]['marker'] == 0xE1) {
1920                $signature = $this->_getFixedString($this->_markers[$i]['data'], 0, 6);
1921                if ($signature == "Exif\0\0") {
1922                    $data =& $this->_markers[$i]['data'];
1923                    break;
1924                }
1925            }
1926        }
1927
1928        if (!isset($this->_info['exif'])) {
1929            return false;
1930        }
1931
1932        $data = "Exif\0\0";
1933        $pos = 6;
1934        $offsetBase = 6;
1935
1936        if (isset($this->_info['exif']['ByteAlign']) && ($this->_info['exif']['ByteAlign'] == "Big Endian")) {
1937            $isBigEndian = true;
1938            $aux = "MM";
1939            $pos = $this->_putString($data, $pos, $aux);
1940        } else {
1941            $isBigEndian = false;
1942            $aux = "II";
1943            $pos = $this->_putString($data, $pos, $aux);
1944        }
1945        $pos = $this->_putShort($data, $pos, 0x002A, $isBigEndian);
1946        $pos = $this->_putLong($data, $pos, 0x00000008, $isBigEndian); // IFD0 Offset is always 8
1947
1948        $ifd0 =& $this->_getIFDEntries($isBigEndian, 'ifd0');
1949        $ifd1 =& $this->_getIFDEntries($isBigEndian, 'ifd1');
1950
1951        $pos = $this->_writeIFD($data, $pos, $offsetBase, $ifd0, $isBigEndian, true);
1952        $pos = $this->_writeIFD($data, $pos, $offsetBase, $ifd1, $isBigEndian, false);
1953
1954        return $data;
1955    }
1956
1957    /*************************************************************/
1958
1959    /**
1960     * @param mixed $data
1961     * @param integer $pos
1962     * @param integer $offsetBase
1963     * @param array $entries
1964     * @param boolean $isBigEndian
1965     * @param boolean $hasNext
1966     *
1967     * @return mixed
1968     */
1969    function _writeIFD(&$data, $pos, $offsetBase, &$entries, $isBigEndian, $hasNext) {
1970        $tiffData = null;
1971        $tiffDataOffsetPos = -1;
1972
1973        $entryCount = count($entries);
1974
1975        $dataPos = $pos + 2 + ($entryCount * 12) + 4;
1976        $pos = $this->_putShort($data, $pos, $entryCount, $isBigEndian);
1977
1978        for ($i = 0; $i < $entryCount; $i++) {
1979            $tag = $entries[$i]['tag'];
1980            $type = $entries[$i]['type'];
1981
1982            if ($type == -99) { // SubIFD
1983                $pos = $this->_putShort($data, $pos, $tag, $isBigEndian);
1984                $pos = $this->_putShort($data, $pos, 0x04, $isBigEndian); // LONG
1985                $pos = $this->_putLong($data, $pos, 0x01, $isBigEndian); // Count = 1
1986                $pos = $this->_putLong($data, $pos, $dataPos - $offsetBase, $isBigEndian);
1987
1988                $dataPos = $this->_writeIFD($data, $dataPos, $offsetBase, $entries[$i]['value'], $isBigEndian, false);
1989            } elseif ($type == -98) { // TIFF Data
1990                $pos = $this->_putShort($data, $pos, $tag, $isBigEndian);
1991                $pos = $this->_putShort($data, $pos, 0x04, $isBigEndian); // LONG
1992                $pos = $this->_putLong($data, $pos, 0x01, $isBigEndian); // Count = 1
1993                $tiffDataOffsetPos = $pos;
1994                $pos = $this->_putLong($data, $pos, 0x00, $isBigEndian); // For Now
1995                $tiffData =& $entries[$i]['value'] ;
1996            } else { // Regular Entry
1997                $pos = $this->_putShort($data, $pos, $tag, $isBigEndian);
1998                $pos = $this->_putShort($data, $pos, $type, $isBigEndian);
1999                $pos = $this->_putLong($data, $pos, $entries[$i]['count'], $isBigEndian);
2000                if (strlen($entries[$i]['value']) > 4) {
2001                    $pos = $this->_putLong($data, $pos, $dataPos - $offsetBase, $isBigEndian);
2002                    $dataPos = $this->_putString($data, $dataPos, $entries[$i]['value']);
2003                } else {
2004                    $val = str_pad($entries[$i]['value'], 4, "\0");
2005                    $pos = $this->_putString($data, $pos, $val);
2006                }
2007            }
2008        }
2009
2010        if ($tiffData != null) {
2011            $this->_putLong($data, $tiffDataOffsetPos, $dataPos - $offsetBase, $isBigEndian);
2012            $dataPos = $this->_putString($data, $dataPos, $tiffData);
2013        }
2014
2015        if ($hasNext) {
2016            $pos = $this->_putLong($data, $pos, $dataPos - $offsetBase, $isBigEndian);
2017        } else {
2018            $pos = $this->_putLong($data, $pos, 0, $isBigEndian);
2019        }
2020
2021        return $dataPos;
2022    }
2023
2024    /*************************************************************/
2025
2026    /**
2027     * @param boolean $isBigEndian
2028     * @param string $mode
2029     *
2030     * @return array
2031     */
2032    function & _getIFDEntries($isBigEndian, $mode) {
2033        $EXIFNames = $this->_exifTagNames($mode);
2034        $EXIFTags = $this->_exifNameTags($mode);
2035        $EXIFTypeInfo = $this->_exifTagTypes($mode);
2036
2037        $ifdEntries = array();
2038        $entryCount = 0;
2039
2040        reset($EXIFNames);
2041        while (list($tag, $name) = each($EXIFNames)) {
2042            $type = $EXIFTypeInfo[$tag][0];
2043            $count = $EXIFTypeInfo[$tag][1];
2044            $value = null;
2045
2046            if (($mode == 'ifd0') && ($tag == 0x8769)) {  // ExifIFDOffset
2047                if (isset($this->_info['exif']['EXIFVersion'])) {
2048                    $value =& $this->_getIFDEntries($isBigEndian, "exif");
2049                    $type = -99;
2050                }
2051                else {
2052                    $value = null;
2053                }
2054            } elseif (($mode == 'ifd0') && ($tag == 0x8825)) {  // GPSIFDOffset
2055                if (isset($this->_info['exif']['GPSVersionID'])) {
2056                    $value =& $this->_getIFDEntries($isBigEndian, "gps");
2057                    $type = -99;
2058                } else {
2059                    $value = null;
2060                }
2061            } elseif (($mode == 'ifd1') && ($tag == 0x0111)) {  // TIFFStripOffsets
2062                if (isset($this->_info['exif']['TIFFStrips'])) {
2063                    $value =& $this->_info['exif']['TIFFStrips'];
2064                    $type = -98;
2065                } else {
2066                    $value = null;
2067                }
2068            } elseif (($mode == 'ifd1') && ($tag == 0x0117)) {  // TIFFStripByteCounts
2069                if (isset($this->_info['exif']['TIFFStrips'])) {
2070                    $value = strlen($this->_info['exif']['TIFFStrips']);
2071                } else {
2072                    $value = null;
2073                }
2074            } elseif (($mode == 'ifd1') && ($tag == 0x0201)) {  // TIFFJFIFOffset
2075                if (isset($this->_info['exif']['JFIFThumbnail'])) {
2076                    $value =& $this->_info['exif']['JFIFThumbnail'];
2077                    $type = -98;
2078                } else {
2079                    $value = null;
2080                }
2081            } elseif (($mode == 'ifd1') && ($tag == 0x0202)) {  // TIFFJFIFLength
2082                if (isset($this->_info['exif']['JFIFThumbnail'])) {
2083                    $value = strlen($this->_info['exif']['JFIFThumbnail']);
2084                } else {
2085                    $value = null;
2086                }
2087            } elseif (($mode == 'exif') && ($tag == 0xA005)) {  // InteropIFDOffset
2088                if (isset($this->_info['exif']['InteroperabilityIndex'])) {
2089                    $value =& $this->_getIFDEntries($isBigEndian, "interop");
2090                    $type = -99;
2091                } else {
2092                    $value = null;
2093                }
2094            } elseif (isset($this->_info['exif'][$name])) {
2095                $origValue =& $this->_info['exif'][$name];
2096
2097                // This makes it easier to process variable size elements
2098                if (!is_array($origValue) || isset($origValue['val'])) {
2099                    unset($origValue); // Break the reference
2100                    $origValue = array($this->_info['exif'][$name]);
2101                }
2102                $origCount = count($origValue);
2103
2104                if ($origCount == 0 ) {
2105                    $type = -1;  // To ignore this field
2106                }
2107
2108                $value = " ";
2109
2110                switch ($type) {
2111                    case 1:    // UBYTE
2112                        if ($count == 0) {
2113                            $count = $origCount;
2114                        }
2115
2116                        $j = 0;
2117                        while (($j < $count) && ($j < $origCount)) {
2118
2119                            $this->_putByte($value, $j, $origValue[$j]);
2120                            $j++;
2121                        }
2122
2123                        while ($j < $count) {
2124                            $this->_putByte($value, $j, 0);
2125                            $j++;
2126                        }
2127                        break;
2128                    case 2:    // ASCII
2129                        $v = strval($origValue[0]);
2130                        if (($count != 0) && (strlen($v) > $count)) {
2131                            $v = substr($v, 0, $count);
2132                        }
2133                        elseif (($count > 0) && (strlen($v) < $count)) {
2134                            $v = str_pad($v, $count, "\0");
2135                        }
2136
2137                        $count = strlen($v);
2138
2139                        $this->_putString($value, 0, $v);
2140                        break;
2141                    case 3:    // USHORT
2142                        if ($count == 0) {
2143                            $count = $origCount;
2144                        }
2145
2146                        $j = 0;
2147                        while (($j < $count) && ($j < $origCount)) {
2148                            $this->_putShort($value, $j * 2, $origValue[$j], $isBigEndian);
2149                            $j++;
2150                        }
2151
2152                        while ($j < $count) {
2153                            $this->_putShort($value, $j * 2, 0, $isBigEndian);
2154                            $j++;
2155                        }
2156                        break;
2157                    case 4:    // ULONG
2158                        if ($count == 0) {
2159                            $count = $origCount;
2160                        }
2161
2162                        $j = 0;
2163                        while (($j < $count) && ($j < $origCount)) {
2164                            $this->_putLong($value, $j * 4, $origValue[$j], $isBigEndian);
2165                            $j++;
2166                        }
2167
2168                        while ($j < $count) {
2169                            $this->_putLong($value, $j * 4, 0, $isBigEndian);
2170                            $j++;
2171                        }
2172                        break;
2173                    case 5:    // URATIONAL
2174                        if ($count == 0) {
2175                            $count = $origCount;
2176                        }
2177
2178                        $j = 0;
2179                        while (($j < $count) && ($j < $origCount)) {
2180                            $v = $origValue[$j];
2181                            if (is_array($v)) {
2182                                $a = $v['num'];
2183                                $b = $v['den'];
2184                            }
2185                            else {
2186                                $a = 0;
2187                                $b = 0;
2188                                // TODO: Allow other types and convert them
2189                            }
2190                            $this->_putLong($value, $j * 8, $a, $isBigEndian);
2191                            $this->_putLong($value, ($j * 8) + 4, $b, $isBigEndian);
2192                            $j++;
2193                        }
2194
2195                        while ($j < $count) {
2196                            $this->_putLong($value, $j * 8, 0, $isBigEndian);
2197                            $this->_putLong($value, ($j * 8) + 4, 0, $isBigEndian);
2198                            $j++;
2199                        }
2200                        break;
2201                    case 6:    // SBYTE
2202                        if ($count == 0) {
2203                            $count = $origCount;
2204                        }
2205
2206                        $j = 0;
2207                        while (($j < $count) && ($j < $origCount)) {
2208                            $this->_putByte($value, $j, $origValue[$j]);
2209                            $j++;
2210                        }
2211
2212                        while ($j < $count) {
2213                            $this->_putByte($value, $j, 0);
2214                            $j++;
2215                        }
2216                        break;
2217                    case 7:    // UNDEFINED
2218                        $v = strval($origValue[0]);
2219                        if (($count != 0) && (strlen($v) > $count)) {
2220                            $v = substr($v, 0, $count);
2221                        }
2222                        elseif (($count > 0) && (strlen($v) < $count)) {
2223                            $v = str_pad($v, $count, "\0");
2224                        }
2225
2226                        $count = strlen($v);
2227
2228                        $this->_putString($value, 0, $v);
2229                        break;
2230                    case 8:    // SSHORT
2231                        if ($count == 0) {
2232                            $count = $origCount;
2233                        }
2234
2235                        $j = 0;
2236                        while (($j < $count) && ($j < $origCount)) {
2237                            $this->_putShort($value, $j * 2, $origValue[$j], $isBigEndian);
2238                            $j++;
2239                        }
2240
2241                        while ($j < $count) {
2242                            $this->_putShort($value, $j * 2, 0, $isBigEndian);
2243                            $j++;
2244                        }
2245                        break;
2246                    case 9:    // SLONG
2247                        if ($count == 0) {
2248                            $count = $origCount;
2249                        }
2250
2251                        $j = 0;
2252                        while (($j < $count) && ($j < $origCount)) {
2253                            $this->_putLong($value, $j * 4, $origValue[$j], $isBigEndian);
2254                            $j++;
2255                        }
2256
2257                        while ($j < $count) {
2258                            $this->_putLong($value, $j * 4, 0, $isBigEndian);
2259                            $j++;
2260                        }
2261                        break;
2262                    case 10:   // SRATIONAL
2263                        if ($count == 0) {
2264                            $count = $origCount;
2265                        }
2266
2267                        $j = 0;
2268                        while (($j < $count) && ($j < $origCount)) {
2269                            $v = $origValue[$j];
2270                            if (is_array($v)) {
2271                                $a = $v['num'];
2272                                $b = $v['den'];
2273                            }
2274                            else {
2275                                $a = 0;
2276                                $b = 0;
2277                                // TODO: Allow other types and convert them
2278                            }
2279
2280                            $this->_putLong($value, $j * 8, $a, $isBigEndian);
2281                            $this->_putLong($value, ($j * 8) + 4, $b, $isBigEndian);
2282                            $j++;
2283                        }
2284
2285                        while ($j < $count) {
2286                            $this->_putLong($value, $j * 8, 0, $isBigEndian);
2287                            $this->_putLong($value, ($j * 8) + 4, 0, $isBigEndian);
2288                            $j++;
2289                        }
2290                        break;
2291                    case 11:   // FLOAT
2292                        if ($count == 0) {
2293                            $count = $origCount;
2294                        }
2295
2296                        $j = 0;
2297                        while (($j < $count) && ($j < $origCount)) {
2298                            $v = strval($origValue[$j]);
2299                            if (strlen($v) > 4) {
2300                                $v = substr($v, 0, 4);
2301                            }
2302                            elseif (strlen($v) < 4) {
2303                                $v = str_pad($v, 4, "\0");
2304                            }
2305                            $this->_putString($value, $j * 4, $v);
2306                            $j++;
2307                        }
2308
2309                        while ($j < $count) {
2310                            $v = "\0\0\0\0";
2311                            $this->_putString($value, $j * 4, $v);
2312                            $j++;
2313                        }
2314                        break;
2315                    case 12:   // DFLOAT
2316                        if ($count == 0) {
2317                            $count = $origCount;
2318                        }
2319
2320                        $j = 0;
2321                        while (($j < $count) && ($j < $origCount)) {
2322                            $v = strval($origValue[$j]);
2323                            if (strlen($v) > 8) {
2324                                $v = substr($v, 0, 8);
2325                            }
2326                            elseif (strlen($v) < 8) {
2327                                $v = str_pad($v, 8, "\0");
2328                            }
2329                            $this->_putString($value, $j * 8, $v);
2330                            $j++;
2331                        }
2332
2333                        while ($j < $count) {
2334                            $v = "\0\0\0\0\0\0\0\0";
2335                            $this->_putString($value, $j * 8, $v);
2336                            $j++;
2337                        }
2338                        break;
2339                    default:
2340                        $value = null;
2341                        break;
2342                }
2343            }
2344
2345            if ($value != null) {
2346                $ifdEntries[$entryCount] = array();
2347                $ifdEntries[$entryCount]['tag'] = $tag;
2348                $ifdEntries[$entryCount]['type'] = $type;
2349                $ifdEntries[$entryCount]['count'] = $count;
2350                $ifdEntries[$entryCount]['value'] = $value;
2351
2352                $entryCount++;
2353            }
2354        }
2355
2356        return $ifdEntries;
2357    }
2358
2359    /*************************************************************/
2360    function _parseMarkerAdobe() {
2361        if (!isset($this->_markers)) {
2362            $this->_readJPEG();
2363        }
2364
2365        if ($this->_markers == null) {
2366            return false;
2367        }
2368
2369        $data = null;
2370        $count = count($this->_markers);
2371        for ($i = 0; $i < $count; $i++) {
2372            if ($this->_markers[$i]['marker'] == 0xED) {
2373                $signature = $this->_getFixedString($this->_markers[$i]['data'], 0, 14);
2374                if ($signature == "Photoshop 3.0\0") {
2375                    $data =& $this->_markers[$i]['data'];
2376                    break;
2377                }
2378            }
2379        }
2380
2381        if ($data == null) {
2382            $this->_info['adobe'] = false;
2383            $this->_info['iptc'] = false;
2384            return false;
2385        }
2386        $pos = 14;
2387        $this->_info['adobe'] = array();
2388        $this->_info['adobe']['raw'] = array();
2389        $this->_info['iptc'] = array();
2390
2391        $datasize = strlen($data);
2392
2393        while ($pos < $datasize) {
2394            $signature = $this->_getFixedString($data, $pos, 4);
2395            if ($signature != '8BIM')
2396                return false;
2397            $pos += 4;
2398
2399            $type = $this->_getShort($data, $pos);
2400            $pos += 2;
2401
2402            $strlen = $this->_getByte($data, $pos);
2403            $pos += 1;
2404            $header = '';
2405            for ($i = 0; $i < $strlen; $i++) {
2406                $header .= $data{$pos + $i};
2407            }
2408            $pos += $strlen + 1 - ($strlen % 2);  // The string is padded to even length, counting the length byte itself
2409
2410            $length = $this->_getLong($data, $pos);
2411            $pos += 4;
2412
2413            $basePos = $pos;
2414
2415            switch ($type) {
2416                case 0x0404: // Caption (IPTC Data)
2417                    $pos = $this->_readIPTC($data, $pos);
2418                    if ($pos == false)
2419                        return false;
2420                    break;
2421                case 0x040A: // CopyrightFlag
2422                    $this->_info['adobe']['CopyrightFlag'] = $this->_getByte($data, $pos);
2423                    $pos += $length;
2424                    break;
2425                case 0x040B: // ImageURL
2426                    $this->_info['adobe']['ImageURL'] = $this->_getFixedString($data, $pos, $length);
2427                    $pos += $length;
2428                    break;
2429                case 0x040C: // Thumbnail
2430                    $aux = $this->_getLong($data, $pos);
2431                    $pos += 4;
2432                    if ($aux == 1) {
2433                        $this->_info['adobe']['ThumbnailWidth'] = $this->_getLong($data, $pos);
2434                        $pos += 4;
2435                        $this->_info['adobe']['ThumbnailHeight'] = $this->_getLong($data, $pos);
2436                        $pos += 4;
2437
2438                        $pos += 16; // Skip some data
2439
2440                        $this->_info['adobe']['ThumbnailData'] = $this->_getFixedString($data, $pos, $length - 28);
2441                        $pos += $length - 28;
2442                    }
2443                    break;
2444                default:
2445                    break;
2446            }
2447
2448            // We save all blocks, even those we recognized
2449            $label = sprintf('8BIM_0x%04x', $type);
2450            $this->_info['adobe']['raw'][$label] = array();
2451            $this->_info['adobe']['raw'][$label]['type'] = $type;
2452            $this->_info['adobe']['raw'][$label]['header'] = $header;
2453            $this->_info['adobe']['raw'][$label]['data'] =& $this->_getFixedString($data, $basePos, $length);
2454
2455            $pos = $basePos + $length + ($length % 2); // Even padding
2456        }
2457
2458    }
2459
2460    /*************************************************************/
2461    function _readIPTC(&$data, $pos = 0) {
2462        $totalLength = strlen($data);
2463
2464        $IPTCTags = $this->_iptcTagNames();
2465
2466        while ($pos < ($totalLength - 5)) {
2467            $signature = $this->_getShort($data, $pos);
2468            if ($signature != 0x1C02)
2469                return $pos;
2470            $pos += 2;
2471
2472            $type = $this->_getByte($data, $pos);
2473            $pos += 1;
2474            $length = $this->_getShort($data, $pos);
2475            $pos += 2;
2476
2477            $basePos = $pos;
2478            $label = '';
2479
2480            if (isset($IPTCTags[$type])) {
2481                $label = $IPTCTags[$type];
2482            } else {
2483                $label = sprintf('IPTC_0x%02x', $type);
2484            }
2485
2486            if ($label != '') {
2487                if (isset($this->_info['iptc'][$label])) {
2488                    if (!is_array($this->_info['iptc'][$label])) {
2489                        $aux = array();
2490                        $aux[0] = $this->_info['iptc'][$label];
2491                        $this->_info['iptc'][$label] = $aux;
2492                    }
2493                    $this->_info['iptc'][$label][ count($this->_info['iptc'][$label]) ] = $this->_getFixedString($data, $pos, $length);
2494                } else {
2495                    $this->_info['iptc'][$label] = $this->_getFixedString($data, $pos, $length);
2496                }
2497            }
2498
2499            $pos = $basePos + $length; // No padding
2500        }
2501        return $pos;
2502    }
2503
2504    /*************************************************************/
2505    function & _createMarkerAdobe() {
2506        if (isset($this->_info['iptc'])) {
2507            if (!isset($this->_info['adobe'])) {
2508                $this->_info['adobe'] = array();
2509            }
2510            if (!isset($this->_info['adobe']['raw'])) {
2511                $this->_info['adobe']['raw'] = array();
2512            }
2513            if (!isset($this->_info['adobe']['raw']['8BIM_0x0404'])) {
2514                $this->_info['adobe']['raw']['8BIM_0x0404'] = array();
2515            }
2516            $this->_info['adobe']['raw']['8BIM_0x0404']['type'] = 0x0404;
2517            $this->_info['adobe']['raw']['8BIM_0x0404']['header'] = "Caption";
2518            $this->_info['adobe']['raw']['8BIM_0x0404']['data'] =& $this->_writeIPTC();
2519        }
2520
2521        if (isset($this->_info['adobe']['raw']) && (count($this->_info['adobe']['raw']) > 0)) {
2522            $data = "Photoshop 3.0\0";
2523            $pos = 14;
2524
2525            reset($this->_info['adobe']['raw']);
2526            while (list($key) = each($this->_info['adobe']['raw'])) {
2527                $pos = $this->_write8BIM(
2528                        $data,
2529                        $pos,
2530                        $this->_info['adobe']['raw'][$key]['type'],
2531                        $this->_info['adobe']['raw'][$key]['header'],
2532                        $this->_info['adobe']['raw'][$key]['data'] );
2533            }
2534        }
2535
2536        return $data;
2537    }
2538
2539    /*************************************************************/
2540
2541    /**
2542     * @param mixed $data
2543     * @param integer $pos
2544     *
2545     * @param string $type
2546     * @param string $header
2547     * @param mixed $value
2548     *
2549     * @return int|mixed
2550     */
2551    function _write8BIM(&$data, $pos, $type, $header, &$value) {
2552        $signature = "8BIM";
2553
2554        $pos = $this->_putString($data, $pos, $signature);
2555        $pos = $this->_putShort($data, $pos, $type);
2556
2557        $len = strlen($header);
2558
2559        $pos = $this->_putByte($data, $pos, $len);
2560        $pos = $this->_putString($data, $pos, $header);
2561        if (($len % 2) == 0) {  // Even padding, including the length byte
2562            $pos = $this->_putByte($data, $pos, 0);
2563        }
2564
2565        $len = strlen($value);
2566        $pos = $this->_putLong($data, $pos, $len);
2567        $pos = $this->_putString($data, $pos, $value);
2568        if (($len % 2) != 0) {  // Even padding
2569            $pos = $this->_putByte($data, $pos, 0);
2570        }
2571        return $pos;
2572    }
2573
2574    /*************************************************************/
2575    function & _writeIPTC() {
2576        $data = " ";
2577        $pos = 0;
2578
2579        $IPTCNames =& $this->_iptcNameTags();
2580
2581        reset($this->_info['iptc']);
2582
2583        while (list($label) = each($this->_info['iptc'])) {
2584            $value =& $this->_info['iptc'][$label];
2585            $type = -1;
2586
2587            if (isset($IPTCNames[$label])) {
2588                $type = $IPTCNames[$label];
2589            }
2590            elseif (substr($label, 0, 7) == "IPTC_0x") {
2591                $type = hexdec(substr($label, 7, 2));
2592            }
2593
2594            if ($type != -1) {
2595                if (is_array($value)) {
2596                    $vcnt = count($value);
2597                    for ($i = 0; $i < $vcnt; $i++) {
2598                        $pos = $this->_writeIPTCEntry($data, $pos, $type, $value[$i]);
2599                    }
2600                }
2601                else {
2602                    $pos = $this->_writeIPTCEntry($data, $pos, $type, $value);
2603                }
2604            }
2605        }
2606
2607        return $data;
2608    }
2609
2610    /*************************************************************/
2611
2612    /**
2613     * @param mixed $data
2614     * @param integer $pos
2615     *
2616     * @param string $type
2617     * @param mixed $value
2618     *
2619     * @return int|mixed
2620     */
2621    function _writeIPTCEntry(&$data, $pos, $type, &$value) {
2622        $pos = $this->_putShort($data, $pos, 0x1C02);
2623        $pos = $this->_putByte($data, $pos, $type);
2624        $pos = $this->_putShort($data, $pos, strlen($value));
2625        $pos = $this->_putString($data, $pos, $value);
2626
2627        return $pos;
2628    }
2629
2630    /*************************************************************/
2631    function _exifTagNames($mode) {
2632        $tags = array();
2633
2634        if ($mode == 'ifd0') {
2635            $tags[0x010E] = 'ImageDescription';
2636            $tags[0x010F] = 'Make';
2637            $tags[0x0110] = 'Model';
2638            $tags[0x0112] = 'Orientation';
2639            $tags[0x011A] = 'XResolution';
2640            $tags[0x011B] = 'YResolution';
2641            $tags[0x0128] = 'ResolutionUnit';
2642            $tags[0x0131] = 'Software';
2643            $tags[0x0132] = 'DateTime';
2644            $tags[0x013B] = 'Artist';
2645            $tags[0x013E] = 'WhitePoint';
2646            $tags[0x013F] = 'PrimaryChromaticities';
2647            $tags[0x0211] = 'YCbCrCoefficients';
2648            $tags[0x0212] = 'YCbCrSubSampling';
2649            $tags[0x0213] = 'YCbCrPositioning';
2650            $tags[0x0214] = 'ReferenceBlackWhite';
2651            $tags[0x8298] = 'Copyright';
2652            $tags[0x8769] = 'ExifIFDOffset';
2653            $tags[0x8825] = 'GPSIFDOffset';
2654        }
2655        if ($mode == 'ifd1') {
2656            $tags[0x00FE] = 'TIFFNewSubfileType';
2657            $tags[0x00FF] = 'TIFFSubfileType';
2658            $tags[0x0100] = 'TIFFImageWidth';
2659            $tags[0x0101] = 'TIFFImageHeight';
2660            $tags[0x0102] = 'TIFFBitsPerSample';
2661            $tags[0x0103] = 'TIFFCompression';
2662            $tags[0x0106] = 'TIFFPhotometricInterpretation';
2663            $tags[0x0107] = 'TIFFThreshholding';
2664            $tags[0x0108] = 'TIFFCellWidth';
2665            $tags[0x0109] = 'TIFFCellLength';
2666            $tags[0x010A] = 'TIFFFillOrder';
2667            $tags[0x010E] = 'TIFFImageDescription';
2668            $tags[0x010F] = 'TIFFMake';
2669            $tags[0x0110] = 'TIFFModel';
2670            $tags[0x0111] = 'TIFFStripOffsets';
2671            $tags[0x0112] = 'TIFFOrientation';
2672            $tags[0x0115] = 'TIFFSamplesPerPixel';
2673            $tags[0x0116] = 'TIFFRowsPerStrip';
2674            $tags[0x0117] = 'TIFFStripByteCounts';
2675            $tags[0x0118] = 'TIFFMinSampleValue';
2676            $tags[0x0119] = 'TIFFMaxSampleValue';
2677            $tags[0x011A] = 'TIFFXResolution';
2678            $tags[0x011B] = 'TIFFYResolution';
2679            $tags[0x011C] = 'TIFFPlanarConfiguration';
2680            $tags[0x0122] = 'TIFFGrayResponseUnit';
2681            $tags[0x0123] = 'TIFFGrayResponseCurve';
2682            $tags[0x0128] = 'TIFFResolutionUnit';
2683            $tags[0x0131] = 'TIFFSoftware';
2684            $tags[0x0132] = 'TIFFDateTime';
2685            $tags[0x013B] = 'TIFFArtist';
2686            $tags[0x013C] = 'TIFFHostComputer';
2687            $tags[0x0140] = 'TIFFColorMap';
2688            $tags[0x0152] = 'TIFFExtraSamples';
2689            $tags[0x0201] = 'TIFFJFIFOffset';
2690            $tags[0x0202] = 'TIFFJFIFLength';
2691            $tags[0x0211] = 'TIFFYCbCrCoefficients';
2692            $tags[0x0212] = 'TIFFYCbCrSubSampling';
2693            $tags[0x0213] = 'TIFFYCbCrPositioning';
2694            $tags[0x0214] = 'TIFFReferenceBlackWhite';
2695            $tags[0x8298] = 'TIFFCopyright';
2696            $tags[0x9286] = 'TIFFUserComment';
2697        } elseif ($mode == 'exif') {
2698            $tags[0x829A] = 'ExposureTime';
2699            $tags[0x829D] = 'FNumber';
2700            $tags[0x8822] = 'ExposureProgram';
2701            $tags[0x8824] = 'SpectralSensitivity';
2702            $tags[0x8827] = 'ISOSpeedRatings';
2703            $tags[0x8828] = 'OECF';
2704            $tags[0x9000] = 'EXIFVersion';
2705            $tags[0x9003] = 'DatetimeOriginal';
2706            $tags[0x9004] = 'DatetimeDigitized';
2707            $tags[0x9101] = 'ComponentsConfiguration';
2708            $tags[0x9102] = 'CompressedBitsPerPixel';
2709            $tags[0x9201] = 'ShutterSpeedValue';
2710            $tags[0x9202] = 'ApertureValue';
2711            $tags[0x9203] = 'BrightnessValue';
2712            $tags[0x9204] = 'ExposureBiasValue';
2713            $tags[0x9205] = 'MaxApertureValue';
2714            $tags[0x9206] = 'SubjectDistance';
2715            $tags[0x9207] = 'MeteringMode';
2716            $tags[0x9208] = 'LightSource';
2717            $tags[0x9209] = 'Flash';
2718            $tags[0x920A] = 'FocalLength';
2719            $tags[0x927C] = 'MakerNote';
2720            $tags[0x9286] = 'UserComment';
2721            $tags[0x9290] = 'SubSecTime';
2722            $tags[0x9291] = 'SubSecTimeOriginal';
2723            $tags[0x9292] = 'SubSecTimeDigitized';
2724            $tags[0xA000] = 'FlashPixVersion';
2725            $tags[0xA001] = 'ColorSpace';
2726            $tags[0xA002] = 'PixelXDimension';
2727            $tags[0xA003] = 'PixelYDimension';
2728            $tags[0xA004] = 'RelatedSoundFile';
2729            $tags[0xA005] = 'InteropIFDOffset';
2730            $tags[0xA20B] = 'FlashEnergy';
2731            $tags[0xA20C] = 'SpatialFrequencyResponse';
2732            $tags[0xA20E] = 'FocalPlaneXResolution';
2733            $tags[0xA20F] = 'FocalPlaneYResolution';
2734            $tags[0xA210] = 'FocalPlaneResolutionUnit';
2735            $tags[0xA214] = 'SubjectLocation';
2736            $tags[0xA215] = 'ExposureIndex';
2737            $tags[0xA217] = 'SensingMethod';
2738            $tags[0xA300] = 'FileSource';
2739            $tags[0xA301] = 'SceneType';
2740            $tags[0xA302] = 'CFAPattern';
2741        } elseif ($mode == 'interop') {
2742            $tags[0x0001] = 'InteroperabilityIndex';
2743            $tags[0x0002] = 'InteroperabilityVersion';
2744            $tags[0x1000] = 'RelatedImageFileFormat';
2745            $tags[0x1001] = 'RelatedImageWidth';
2746            $tags[0x1002] = 'RelatedImageLength';
2747        } elseif ($mode == 'gps') {
2748            $tags[0x0000] = 'GPSVersionID';
2749            $tags[0x0001] = 'GPSLatitudeRef';
2750            $tags[0x0002] = 'GPSLatitude';
2751            $tags[0x0003] = 'GPSLongitudeRef';
2752            $tags[0x0004] = 'GPSLongitude';
2753            $tags[0x0005] = 'GPSAltitudeRef';
2754            $tags[0x0006] = 'GPSAltitude';
2755            $tags[0x0007] = 'GPSTimeStamp';
2756            $tags[0x0008] = 'GPSSatellites';
2757            $tags[0x0009] = 'GPSStatus';
2758            $tags[0x000A] = 'GPSMeasureMode';
2759            $tags[0x000B] = 'GPSDOP';
2760            $tags[0x000C] = 'GPSSpeedRef';
2761            $tags[0x000D] = 'GPSSpeed';
2762            $tags[0x000E] = 'GPSTrackRef';
2763            $tags[0x000F] = 'GPSTrack';
2764            $tags[0x0010] = 'GPSImgDirectionRef';
2765            $tags[0x0011] = 'GPSImgDirection';
2766            $tags[0x0012] = 'GPSMapDatum';
2767            $tags[0x0013] = 'GPSDestLatitudeRef';
2768            $tags[0x0014] = 'GPSDestLatitude';
2769            $tags[0x0015] = 'GPSDestLongitudeRef';
2770            $tags[0x0016] = 'GPSDestLongitude';
2771            $tags[0x0017] = 'GPSDestBearingRef';
2772            $tags[0x0018] = 'GPSDestBearing';
2773            $tags[0x0019] = 'GPSDestDistanceRef';
2774            $tags[0x001A] = 'GPSDestDistance';
2775        }
2776
2777        return $tags;
2778    }
2779
2780    /*************************************************************/
2781    function _exifTagTypes($mode) {
2782        $tags = array();
2783
2784        if ($mode == 'ifd0') {
2785            $tags[0x010E] = array(2, 0); // ImageDescription -> ASCII, Any
2786            $tags[0x010F] = array(2, 0); // Make -> ASCII, Any
2787            $tags[0x0110] = array(2, 0); // Model -> ASCII, Any
2788            $tags[0x0112] = array(3, 1); // Orientation -> SHORT, 1
2789            $tags[0x011A] = array(5, 1); // XResolution -> RATIONAL, 1
2790            $tags[0x011B] = array(5, 1); // YResolution -> RATIONAL, 1
2791            $tags[0x0128] = array(3, 1); // ResolutionUnit -> SHORT
2792            $tags[0x0131] = array(2, 0); // Software -> ASCII, Any
2793            $tags[0x0132] = array(2, 20); // DateTime -> ASCII, 20
2794            $tags[0x013B] = array(2, 0); // Artist -> ASCII, Any
2795            $tags[0x013E] = array(5, 2); // WhitePoint -> RATIONAL, 2
2796            $tags[0x013F] = array(5, 6); // PrimaryChromaticities -> RATIONAL, 6
2797            $tags[0x0211] = array(5, 3); // YCbCrCoefficients -> RATIONAL, 3
2798            $tags[0x0212] = array(3, 2); // YCbCrSubSampling -> SHORT, 2
2799            $tags[0x0213] = array(3, 1); // YCbCrPositioning -> SHORT, 1
2800            $tags[0x0214] = array(5, 6); // ReferenceBlackWhite -> RATIONAL, 6
2801            $tags[0x8298] = array(2, 0); // Copyright -> ASCII, Any
2802            $tags[0x8769] = array(4, 1); // ExifIFDOffset -> LONG, 1
2803            $tags[0x8825] = array(4, 1); // GPSIFDOffset -> LONG, 1
2804        }
2805        if ($mode == 'ifd1') {
2806            $tags[0x00FE] = array(4, 1); // TIFFNewSubfileType -> LONG, 1
2807            $tags[0x00FF] = array(3, 1); // TIFFSubfileType -> SHORT, 1
2808            $tags[0x0100] = array(4, 1); // TIFFImageWidth -> LONG (or SHORT), 1
2809            $tags[0x0101] = array(4, 1); // TIFFImageHeight -> LONG (or SHORT), 1
2810            $tags[0x0102] = array(3, 3); // TIFFBitsPerSample -> SHORT, 3
2811            $tags[0x0103] = array(3, 1); // TIFFCompression -> SHORT, 1
2812            $tags[0x0106] = array(3, 1); // TIFFPhotometricInterpretation -> SHORT, 1
2813            $tags[0x0107] = array(3, 1); // TIFFThreshholding -> SHORT, 1
2814            $tags[0x0108] = array(3, 1); // TIFFCellWidth -> SHORT, 1
2815            $tags[0x0109] = array(3, 1); // TIFFCellLength -> SHORT, 1
2816            $tags[0x010A] = array(3, 1); // TIFFFillOrder -> SHORT, 1
2817            $tags[0x010E] = array(2, 0); // TIFFImageDescription -> ASCII, Any
2818            $tags[0x010F] = array(2, 0); // TIFFMake -> ASCII, Any
2819            $tags[0x0110] = array(2, 0); // TIFFModel -> ASCII, Any
2820            $tags[0x0111] = array(4, 0); // TIFFStripOffsets -> LONG (or SHORT), Any (one per strip)
2821            $tags[0x0112] = array(3, 1); // TIFFOrientation -> SHORT, 1
2822            $tags[0x0115] = array(3, 1); // TIFFSamplesPerPixel -> SHORT, 1
2823            $tags[0x0116] = array(4, 1); // TIFFRowsPerStrip -> LONG (or SHORT), 1
2824            $tags[0x0117] = array(4, 0); // TIFFStripByteCounts -> LONG (or SHORT), Any (one per strip)
2825            $tags[0x0118] = array(3, 0); // TIFFMinSampleValue -> SHORT, Any (SamplesPerPixel)
2826            $tags[0x0119] = array(3, 0); // TIFFMaxSampleValue -> SHORT, Any (SamplesPerPixel)
2827            $tags[0x011A] = array(5, 1); // TIFFXResolution -> RATIONAL, 1
2828            $tags[0x011B] = array(5, 1); // TIFFYResolution -> RATIONAL, 1
2829            $tags[0x011C] = array(3, 1); // TIFFPlanarConfiguration -> SHORT, 1
2830            $tags[0x0122] = array(3, 1); // TIFFGrayResponseUnit -> SHORT, 1
2831            $tags[0x0123] = array(3, 0); // TIFFGrayResponseCurve -> SHORT, Any (2^BitsPerSample)
2832            $tags[0x0128] = array(3, 1); // TIFFResolutionUnit -> SHORT, 1
2833            $tags[0x0131] = array(2, 0); // TIFFSoftware -> ASCII, Any
2834            $tags[0x0132] = array(2, 20); // TIFFDateTime -> ASCII, 20
2835            $tags[0x013B] = array(2, 0); // TIFFArtist -> ASCII, Any
2836            $tags[0x013C] = array(2, 0); // TIFFHostComputer -> ASCII, Any
2837            $tags[0x0140] = array(3, 0); // TIFFColorMap -> SHORT, Any (3 * 2^BitsPerSample)
2838            $tags[0x0152] = array(3, 0); // TIFFExtraSamples -> SHORT, Any (SamplesPerPixel - 3)
2839            $tags[0x0201] = array(4, 1); // TIFFJFIFOffset -> LONG, 1
2840            $tags[0x0202] = array(4, 1); // TIFFJFIFLength -> LONG, 1
2841            $tags[0x0211] = array(5, 3); // TIFFYCbCrCoefficients -> RATIONAL, 3
2842            $tags[0x0212] = array(3, 2); // TIFFYCbCrSubSampling -> SHORT, 2
2843            $tags[0x0213] = array(3, 1); // TIFFYCbCrPositioning -> SHORT, 1
2844            $tags[0x0214] = array(5, 6); // TIFFReferenceBlackWhite -> RATIONAL, 6
2845            $tags[0x8298] = array(2, 0); // TIFFCopyright -> ASCII, Any
2846            $tags[0x9286] = array(2, 0); // TIFFUserComment -> ASCII, Any
2847        } elseif ($mode == 'exif') {
2848            $tags[0x829A] = array(5, 1); // ExposureTime -> RATIONAL, 1
2849            $tags[0x829D] = array(5, 1); // FNumber -> RATIONAL, 1
2850            $tags[0x8822] = array(3, 1); // ExposureProgram -> SHORT, 1
2851            $tags[0x8824] = array(2, 0); // SpectralSensitivity -> ASCII, Any
2852            $tags[0x8827] = array(3, 0); // ISOSpeedRatings -> SHORT, Any
2853            $tags[0x8828] = array(7, 0); // OECF -> UNDEFINED, Any
2854            $tags[0x9000] = array(7, 4); // EXIFVersion -> UNDEFINED, 4
2855            $tags[0x9003] = array(2, 20); // DatetimeOriginal -> ASCII, 20
2856            $tags[0x9004] = array(2, 20); // DatetimeDigitized -> ASCII, 20
2857            $tags[0x9101] = array(7, 4); // ComponentsConfiguration -> UNDEFINED, 4
2858            $tags[0x9102] = array(5, 1); // CompressedBitsPerPixel -> RATIONAL, 1
2859            $tags[0x9201] = array(10, 1); // ShutterSpeedValue -> SRATIONAL, 1
2860            $tags[0x9202] = array(5, 1); // ApertureValue -> RATIONAL, 1
2861            $tags[0x9203] = array(10, 1); // BrightnessValue -> SRATIONAL, 1
2862            $tags[0x9204] = array(10, 1); // ExposureBiasValue -> SRATIONAL, 1
2863            $tags[0x9205] = array(5, 1); // MaxApertureValue -> RATIONAL, 1
2864            $tags[0x9206] = array(5, 1); // SubjectDistance -> RATIONAL, 1
2865            $tags[0x9207] = array(3, 1); // MeteringMode -> SHORT, 1
2866            $tags[0x9208] = array(3, 1); // LightSource -> SHORT, 1
2867            $tags[0x9209] = array(3, 1); // Flash -> SHORT, 1
2868            $tags[0x920A] = array(5, 1); // FocalLength -> RATIONAL, 1
2869            $tags[0x927C] = array(7, 0); // MakerNote -> UNDEFINED, Any
2870            $tags[0x9286] = array(7, 0); // UserComment -> UNDEFINED, Any
2871            $tags[0x9290] = array(2, 0); // SubSecTime -> ASCII, Any
2872            $tags[0x9291] = array(2, 0); // SubSecTimeOriginal -> ASCII, Any
2873            $tags[0x9292] = array(2, 0); // SubSecTimeDigitized -> ASCII, Any
2874            $tags[0xA000] = array(7, 4); // FlashPixVersion -> UNDEFINED, 4
2875            $tags[0xA001] = array(3, 1); // ColorSpace -> SHORT, 1
2876            $tags[0xA002] = array(4, 1); // PixelXDimension -> LONG (or SHORT), 1
2877            $tags[0xA003] = array(4, 1); // PixelYDimension -> LONG (or SHORT), 1
2878            $tags[0xA004] = array(2, 13); // RelatedSoundFile -> ASCII, 13
2879            $tags[0xA005] = array(4, 1); // InteropIFDOffset -> LONG, 1
2880            $tags[0xA20B] = array(5, 1); // FlashEnergy -> RATIONAL, 1
2881            $tags[0xA20C] = array(7, 0); // SpatialFrequencyResponse -> UNDEFINED, Any
2882            $tags[0xA20E] = array(5, 1); // FocalPlaneXResolution -> RATIONAL, 1
2883            $tags[0xA20F] = array(5, 1); // FocalPlaneYResolution -> RATIONAL, 1
2884            $tags[0xA210] = array(3, 1); // FocalPlaneResolutionUnit -> SHORT, 1
2885            $tags[0xA214] = array(3, 2); // SubjectLocation -> SHORT, 2
2886            $tags[0xA215] = array(5, 1); // ExposureIndex -> RATIONAL, 1
2887            $tags[0xA217] = array(3, 1); // SensingMethod -> SHORT, 1
2888            $tags[0xA300] = array(7, 1); // FileSource -> UNDEFINED, 1
2889            $tags[0xA301] = array(7, 1); // SceneType -> UNDEFINED, 1
2890            $tags[0xA302] = array(7, 0); // CFAPattern -> UNDEFINED, Any
2891        } elseif ($mode == 'interop') {
2892            $tags[0x0001] = array(2, 0); // InteroperabilityIndex -> ASCII, Any
2893            $tags[0x0002] = array(7, 4); // InteroperabilityVersion -> UNKNOWN, 4
2894            $tags[0x1000] = array(2, 0); // RelatedImageFileFormat -> ASCII, Any
2895            $tags[0x1001] = array(4, 1); // RelatedImageWidth -> LONG (or SHORT), 1
2896            $tags[0x1002] = array(4, 1); // RelatedImageLength -> LONG (or SHORT), 1
2897        } elseif ($mode == 'gps') {
2898            $tags[0x0000] = array(1, 4); // GPSVersionID -> BYTE, 4
2899            $tags[0x0001] = array(2, 2); // GPSLatitudeRef -> ASCII, 2
2900            $tags[0x0002] = array(5, 3); // GPSLatitude -> RATIONAL, 3
2901            $tags[0x0003] = array(2, 2); // GPSLongitudeRef -> ASCII, 2
2902            $tags[0x0004] = array(5, 3); // GPSLongitude -> RATIONAL, 3
2903            $tags[0x0005] = array(2, 2); // GPSAltitudeRef -> ASCII, 2
2904            $tags[0x0006] = array(5, 1); // GPSAltitude -> RATIONAL, 1
2905            $tags[0x0007] = array(5, 3); // GPSTimeStamp -> RATIONAL, 3
2906            $tags[0x0008] = array(2, 0); // GPSSatellites -> ASCII, Any
2907            $tags[0x0009] = array(2, 2); // GPSStatus -> ASCII, 2
2908            $tags[0x000A] = array(2, 2); // GPSMeasureMode -> ASCII, 2
2909            $tags[0x000B] = array(5, 1); // GPSDOP -> RATIONAL, 1
2910            $tags[0x000C] = array(2, 2); // GPSSpeedRef -> ASCII, 2
2911            $tags[0x000D] = array(5, 1); // GPSSpeed -> RATIONAL, 1
2912            $tags[0x000E] = array(2, 2); // GPSTrackRef -> ASCII, 2
2913            $tags[0x000F] = array(5, 1); // GPSTrack -> RATIONAL, 1
2914            $tags[0x0010] = array(2, 2); // GPSImgDirectionRef -> ASCII, 2
2915            $tags[0x0011] = array(5, 1); // GPSImgDirection -> RATIONAL, 1
2916            $tags[0x0012] = array(2, 0); // GPSMapDatum -> ASCII, Any
2917            $tags[0x0013] = array(2, 2); // GPSDestLatitudeRef -> ASCII, 2
2918            $tags[0x0014] = array(5, 3); // GPSDestLatitude -> RATIONAL, 3
2919            $tags[0x0015] = array(2, 2); // GPSDestLongitudeRef -> ASCII, 2
2920            $tags[0x0016] = array(5, 3); // GPSDestLongitude -> RATIONAL, 3
2921            $tags[0x0017] = array(2, 2); // GPSDestBearingRef -> ASCII, 2
2922            $tags[0x0018] = array(5, 1); // GPSDestBearing -> RATIONAL, 1
2923            $tags[0x0019] = array(2, 2); // GPSDestDistanceRef -> ASCII, 2
2924            $tags[0x001A] = array(5, 1); // GPSDestDistance -> RATIONAL, 1
2925        }
2926
2927        return $tags;
2928    }
2929
2930    /*************************************************************/
2931    function _exifNameTags($mode) {
2932        $tags = $this->_exifTagNames($mode);
2933        return $this->_names2Tags($tags);
2934    }
2935
2936    /*************************************************************/
2937    function _iptcTagNames() {
2938        $tags = array();
2939        $tags[0x14] = 'SuplementalCategories';
2940        $tags[0x19] = 'Keywords';
2941        $tags[0x78] = 'Caption';
2942        $tags[0x7A] = 'CaptionWriter';
2943        $tags[0x69] = 'Headline';
2944        $tags[0x28] = 'SpecialInstructions';
2945        $tags[0x0F] = 'Category';
2946        $tags[0x50] = 'Byline';
2947        $tags[0x55] = 'BylineTitle';
2948        $tags[0x6E] = 'Credit';
2949        $tags[0x73] = 'Source';
2950        $tags[0x74] = 'CopyrightNotice';
2951        $tags[0x05] = 'ObjectName';
2952        $tags[0x5A] = 'City';
2953        $tags[0x5C] = 'Sublocation';
2954        $tags[0x5F] = 'ProvinceState';
2955        $tags[0x65] = 'CountryName';
2956        $tags[0x67] = 'OriginalTransmissionReference';
2957        $tags[0x37] = 'DateCreated';
2958        $tags[0x0A] = 'CopyrightFlag';
2959
2960        return $tags;
2961    }
2962
2963    /*************************************************************/
2964    function & _iptcNameTags() {
2965        $tags = $this->_iptcTagNames();
2966        return $this->_names2Tags($tags);
2967    }
2968
2969    /*************************************************************/
2970    function _names2Tags($tags2Names) {
2971        $names2Tags = array();
2972        reset($tags2Names);
2973        while (list($tag, $name) = each($tags2Names)) {
2974            $names2Tags[$name] = $tag;
2975        }
2976
2977        return $names2Tags;
2978    }
2979
2980    /*************************************************************/
2981
2982    /**
2983     * @param $data
2984     * @param integer $pos
2985     *
2986     * @return int
2987     */
2988    function _getByte(&$data, $pos) {
2989        return ord($data{$pos});
2990    }
2991
2992    /*************************************************************/
2993
2994    /**
2995     * @param mixed $data
2996     * @param integer $pos
2997     *
2998     * @param mixed $val
2999     *
3000     * @return int
3001     */
3002    function _putByte(&$data, $pos, $val) {
3003        $val = intval($val);
3004
3005        $data{$pos} = chr($val);
3006
3007        return $pos + 1;
3008    }
3009
3010    /*************************************************************/
3011    function _getShort(&$data, $pos, $bigEndian = true) {
3012        if ($bigEndian) {
3013            return (ord($data{$pos}) << 8)
3014                + ord($data{$pos + 1});
3015        } else {
3016            return ord($data{$pos})
3017                + (ord($data{$pos + 1}) << 8);
3018        }
3019    }
3020
3021    /*************************************************************/
3022    function _putShort(&$data, $pos = 0, $val = 0, $bigEndian = true) {
3023        $val = intval($val);
3024
3025        if ($bigEndian) {
3026            $data{$pos + 0} = chr(($val & 0x0000FF00) >> 8);
3027            $data{$pos + 1} = chr(($val & 0x000000FF) >> 0);
3028        } else {
3029            $data{$pos + 0} = chr(($val & 0x00FF) >> 0);
3030            $data{$pos + 1} = chr(($val & 0xFF00) >> 8);
3031        }
3032
3033        return $pos + 2;
3034    }
3035
3036    /*************************************************************/
3037
3038    /**
3039     * @param mixed $data
3040     * @param integer $pos
3041     *
3042     * @param bool $bigEndian
3043     *
3044     * @return int
3045     */
3046    function _getLong(&$data, $pos, $bigEndian = true) {
3047        if ($bigEndian) {
3048            return (ord($data{$pos}) << 24)
3049                + (ord($data{$pos + 1}) << 16)
3050                + (ord($data{$pos + 2}) << 8)
3051                + ord($data{$pos + 3});
3052        } else {
3053            return ord($data{$pos})
3054                + (ord($data{$pos + 1}) << 8)
3055                + (ord($data{$pos + 2}) << 16)
3056                + (ord($data{$pos + 3}) << 24);
3057        }
3058    }
3059
3060    /*************************************************************/
3061
3062    /**
3063     * @param mixed $data
3064     * @param integer $pos
3065     *
3066     * @param mixed $val
3067     * @param bool $bigEndian
3068     *
3069     * @return int
3070     */
3071    function _putLong(&$data, $pos, $val, $bigEndian = true) {
3072        $val = intval($val);
3073
3074        if ($bigEndian) {
3075            $data{$pos + 0} = chr(($val & 0xFF000000) >> 24);
3076            $data{$pos + 1} = chr(($val & 0x00FF0000) >> 16);
3077            $data{$pos + 2} = chr(($val & 0x0000FF00) >> 8);
3078            $data{$pos + 3} = chr(($val & 0x000000FF) >> 0);
3079        } else {
3080            $data{$pos + 0} = chr(($val & 0x000000FF) >> 0);
3081            $data{$pos + 1} = chr(($val & 0x0000FF00) >> 8);
3082            $data{$pos + 2} = chr(($val & 0x00FF0000) >> 16);
3083            $data{$pos + 3} = chr(($val & 0xFF000000) >> 24);
3084        }
3085
3086        return $pos + 4;
3087    }
3088
3089    /*************************************************************/
3090    function & _getNullString(&$data, $pos) {
3091        $str = '';
3092        $max = strlen($data);
3093
3094        while ($pos < $max) {
3095            if (ord($data{$pos}) == 0) {
3096                return $str;
3097            } else {
3098                $str .= $data{$pos};
3099            }
3100            $pos++;
3101        }
3102
3103        return $str;
3104    }
3105
3106    /*************************************************************/
3107    function & _getFixedString(&$data, $pos, $length = -1) {
3108        if ($length == -1) {
3109            $length = strlen($data) - $pos;
3110        }
3111
3112        $rv = substr($data, $pos, $length);
3113        return $rv;
3114    }
3115
3116    /*************************************************************/
3117    function _putString(&$data, $pos, &$str) {
3118        $len = strlen($str);
3119        for ($i = 0; $i < $len; $i++) {
3120            $data{$pos + $i} = $str{$i};
3121        }
3122
3123        return $pos + $len;
3124    }
3125
3126    /*************************************************************/
3127    function _hexDump(&$data, $start = 0, $length = -1) {
3128        if (($length == -1) || (($length + $start) > strlen($data))) {
3129            $end = strlen($data);
3130        } else {
3131            $end = $start + $length;
3132        }
3133
3134        $ascii = '';
3135        $count = 0;
3136
3137        echo "<tt>\n";
3138
3139        while ($start < $end) {
3140            if (($count % 16) == 0) {
3141                echo sprintf('%04d', $count) . ': ';
3142            }
3143
3144            $c = ord($data{$start});
3145            $count++;
3146            $start++;
3147
3148            $aux = dechex($c);
3149            if (strlen($aux) == 1)
3150                echo '0';
3151            echo $aux . ' ';
3152
3153            if ($c == 60)
3154                $ascii .= '&lt;';
3155            elseif ($c == 62)
3156                $ascii .= '&gt;';
3157            elseif ($c == 32)
3158                $ascii .= '&#160;';
3159            elseif ($c > 32)
3160                $ascii .= chr($c);
3161            else
3162                $ascii .= '.';
3163
3164            if (($count % 4) == 0) {
3165                echo ' - ';
3166            }
3167
3168            if (($count % 16) == 0) {
3169                echo ': ' . $ascii . "<br>\n";
3170                $ascii = '';
3171            }
3172        }
3173
3174        if ($ascii != '') {
3175            while (($count % 16) != 0) {
3176                echo '-- ';
3177                $count++;
3178                if (($count % 4) == 0) {
3179                    echo ' - ';
3180                }
3181            }
3182            echo ': ' . $ascii . "<br>\n";
3183        }
3184
3185        echo "</tt>\n";
3186    }
3187
3188    /*****************************************************************/
3189}
3190
3191/* vim: set expandtab tabstop=4 shiftwidth=4: */
3192