xref: /dokuwiki/inc/JpegMeta.php (revision 45717242991e0a74f15ead94903991d465a79501)
155efc227SAndreas Gohr<?php
255efc227SAndreas Gohr/**
355efc227SAndreas Gohr * JPEG metadata reader/writer
455efc227SAndreas Gohr *
52bd74074SAndreas Gohr * @license    BSD <http://www.opensource.org/licenses/bsd-license.php>
62bd74074SAndreas Gohr * @link       http://github.com/sd/jpeg-php
755efc227SAndreas Gohr * @author     Sebastian Delmont <sdelmont@zonageek.com>
855efc227SAndreas Gohr * @author     Andreas Gohr <andi@splitbrain.org>
9431c7fc8Shakan.sandell * @author     Hakan Sandell <hakan.sandell@mydata.se>
1036df6fa3SAndreas Gohr * @todo       Add support for Maker Notes, Extend for GIF and PNG metadata
1155efc227SAndreas Gohr */
1255efc227SAndreas Gohr
132bd74074SAndreas Gohr// Original copyright notice:
1455efc227SAndreas Gohr//
152bd74074SAndreas Gohr// Copyright (c) 2003 Sebastian Delmont <sdelmont@zonageek.com>
162bd74074SAndreas Gohr// All rights reserved.
172bd74074SAndreas Gohr//
182bd74074SAndreas Gohr// Redistribution and use in source and binary forms, with or without
192bd74074SAndreas Gohr// modification, are permitted provided that the following conditions
202bd74074SAndreas Gohr// are met:
212bd74074SAndreas Gohr// 1. Redistributions of source code must retain the above copyright
222bd74074SAndreas Gohr//    notice, this list of conditions and the following disclaimer.
232bd74074SAndreas Gohr// 2. Redistributions in binary form must reproduce the above copyright
242bd74074SAndreas Gohr//    notice, this list of conditions and the following disclaimer in the
252bd74074SAndreas Gohr//    documentation and/or other materials provided with the distribution.
262bd74074SAndreas Gohr// 3. Neither the name of the author nor the names of its contributors
272bd74074SAndreas Gohr//    may be used to endorse or promote products derived from this software
282bd74074SAndreas Gohr//    without specific prior written permission.
292bd74074SAndreas Gohr//
302bd74074SAndreas Gohr// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
312bd74074SAndreas Gohr// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
322bd74074SAndreas Gohr// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
332bd74074SAndreas Gohr// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
342bd74074SAndreas Gohr// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
352bd74074SAndreas Gohr// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
362bd74074SAndreas Gohr// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
372bd74074SAndreas Gohr// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
382bd74074SAndreas Gohr// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
392bd74074SAndreas Gohr// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
402bd74074SAndreas Gohr// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
4155efc227SAndreas Gohr
420b17fdc6SAndreas Gohrclass JpegMeta {
4355efc227SAndreas Gohr    var $_fileName;
4455efc227SAndreas Gohr    var $_fp = null;
4559bc3b48SGerrit Uitslag    var $_fpout = null;
4655efc227SAndreas Gohr    var $_type = 'unknown';
4755efc227SAndreas Gohr
4855efc227SAndreas Gohr    var $_markers;
4955efc227SAndreas Gohr    var $_info;
5055efc227SAndreas Gohr
5155efc227SAndreas Gohr
5255efc227SAndreas Gohr    /**
5355efc227SAndreas Gohr     * Constructor
5455efc227SAndreas Gohr     *
5555efc227SAndreas Gohr     * @author Sebastian Delmont <sdelmont@zonageek.com>
56f50a239bSTakamura     *
57f50a239bSTakamura     * @param $fileName
5855efc227SAndreas Gohr     */
595c3b310dSChristopher Smith    function __construct($fileName) {
6055efc227SAndreas Gohr
6155efc227SAndreas Gohr        $this->_fileName = $fileName;
6255efc227SAndreas Gohr
6355efc227SAndreas Gohr        $this->_fp = null;
6455efc227SAndreas Gohr        $this->_type = 'unknown';
6555efc227SAndreas Gohr
6655efc227SAndreas Gohr        unset($this->_info);
6755efc227SAndreas Gohr        unset($this->_markers);
6855efc227SAndreas Gohr    }
6955efc227SAndreas Gohr
7055efc227SAndreas Gohr    /**
7155efc227SAndreas Gohr     * Returns all gathered info as multidim array
7255efc227SAndreas Gohr     *
7355efc227SAndreas Gohr     * @author Sebastian Delmont <sdelmont@zonageek.com>
7455efc227SAndreas Gohr     */
750b17fdc6SAndreas Gohr    function & getRawInfo() {
7655efc227SAndreas Gohr        $this->_parseAll();
7755efc227SAndreas Gohr
7855efc227SAndreas Gohr        if ($this->_markers == null) {
7955efc227SAndreas Gohr            return false;
8055efc227SAndreas Gohr        }
8155efc227SAndreas Gohr
8255efc227SAndreas Gohr        return $this->_info;
8355efc227SAndreas Gohr    }
8455efc227SAndreas Gohr
8555efc227SAndreas Gohr    /**
8655efc227SAndreas Gohr     * Returns basic image info
8755efc227SAndreas Gohr     *
8855efc227SAndreas Gohr     * @author Sebastian Delmont <sdelmont@zonageek.com>
8955efc227SAndreas Gohr     */
900b17fdc6SAndreas Gohr    function & getBasicInfo() {
9155efc227SAndreas Gohr        $this->_parseAll();
9255efc227SAndreas Gohr
9355efc227SAndreas Gohr        $info = array();
9455efc227SAndreas Gohr
9555efc227SAndreas Gohr        if ($this->_markers == null) {
9655efc227SAndreas Gohr            return false;
9755efc227SAndreas Gohr        }
9855efc227SAndreas Gohr
9955efc227SAndreas Gohr        $info['Name'] = $this->_info['file']['Name'];
10055efc227SAndreas Gohr        if (isset($this->_info['file']['Url'])) {
10155efc227SAndreas Gohr            $info['Url'] = $this->_info['file']['Url'];
10255efc227SAndreas Gohr            $info['NiceSize'] = "???KB";
1030b17fdc6SAndreas Gohr        } else {
10455efc227SAndreas Gohr            $info['Size'] = $this->_info['file']['Size'];
10555efc227SAndreas Gohr            $info['NiceSize'] = $this->_info['file']['NiceSize'];
10655efc227SAndreas Gohr        }
10755efc227SAndreas Gohr
10855efc227SAndreas Gohr        if (@isset($this->_info['sof']['Format'])) {
10955efc227SAndreas Gohr            $info['Format'] = $this->_info['sof']['Format'] . " JPEG";
1100b17fdc6SAndreas Gohr        } else {
11155efc227SAndreas Gohr            $info['Format'] = $this->_info['sof']['Format'] . " JPEG";
11255efc227SAndreas Gohr        }
11355efc227SAndreas Gohr
11455efc227SAndreas Gohr        if (@isset($this->_info['sof']['ColorChannels'])) {
11555efc227SAndreas Gohr            $info['ColorMode'] = ($this->_info['sof']['ColorChannels'] > 1) ? "Color" : "B&W";
11655efc227SAndreas Gohr        }
11755efc227SAndreas Gohr
11855efc227SAndreas Gohr        $info['Width'] = $this->getWidth();
11955efc227SAndreas Gohr        $info['Height'] = $this->getHeight();
12055efc227SAndreas Gohr        $info['DimStr'] = $this->getDimStr();
12155efc227SAndreas Gohr
12255efc227SAndreas Gohr        $dates = $this->getDates();
12355efc227SAndreas Gohr
12455efc227SAndreas Gohr        $info['DateTime'] = $dates['EarliestTime'];
12555efc227SAndreas Gohr        $info['DateTimeStr'] = $dates['EarliestTimeStr'];
12655efc227SAndreas Gohr
12755efc227SAndreas Gohr        $info['HasThumbnail'] = $this->hasThumbnail();
12855efc227SAndreas Gohr
12955efc227SAndreas Gohr        return $info;
13055efc227SAndreas Gohr    }
13155efc227SAndreas Gohr
13255efc227SAndreas Gohr
13355efc227SAndreas Gohr    /**
13455efc227SAndreas Gohr     * Convinience function to access nearly all available Data
13555efc227SAndreas Gohr     * through one function
13655efc227SAndreas Gohr     *
13755efc227SAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
13842ea7f44SGerrit Uitslag     *
13942ea7f44SGerrit Uitslag     * @param array|string $fields field name or array with field names
14042ea7f44SGerrit Uitslag     * @return bool|string
14155efc227SAndreas Gohr     */
1420b17fdc6SAndreas Gohr    function getField($fields) {
14355efc227SAndreas Gohr        if(!is_array($fields)) $fields = array($fields);
14455efc227SAndreas Gohr        $info = false;
14555efc227SAndreas Gohr        foreach($fields as $field){
1466c16a3a9Sfiwswe            $lower_field = strtolower($field);
1476c16a3a9Sfiwswe            if(str_starts_with($lower_field, 'iptc.')){
14855efc227SAndreas Gohr                $info = $this->getIPTCField(substr($field,5));
1496c16a3a9Sfiwswe            }elseif(str_starts_with($lower_field, 'exif.')){
15055efc227SAndreas Gohr                $info = $this->getExifField(substr($field,5));
1516c16a3a9Sfiwswe            }elseif(str_starts_with($lower_field, 'xmp.')){
152431c7fc8Shakan.sandell                $info = $this->getXmpField(substr($field,4));
1536c16a3a9Sfiwswe            }elseif(str_starts_with($lower_field, 'file.')){
15455efc227SAndreas Gohr                $info = $this->getFileField(substr($field,5));
1556c16a3a9Sfiwswe            }elseif(str_starts_with($lower_field, 'date.')){
15655efc227SAndreas Gohr                $info = $this->getDateField(substr($field,5));
1576c16a3a9Sfiwswe            }elseif($lower_field == 'simple.camera'){
15855efc227SAndreas Gohr                $info = $this->getCamera();
1596c16a3a9Sfiwswe            }elseif($lower_field == 'simple.raw'){
16055efc227SAndreas Gohr                return $this->getRawInfo();
1616c16a3a9Sfiwswe            }elseif($lower_field == 'simple.title'){
16255efc227SAndreas Gohr                $info = $this->getTitle();
1636c16a3a9Sfiwswe            }elseif($lower_field == 'simple.shutterspeed'){
1646db72d46SJoe Lapp                $info = $this->getShutterSpeed();
16555efc227SAndreas Gohr            }else{
16655efc227SAndreas Gohr                $info = $this->getExifField($field);
16755efc227SAndreas Gohr            }
16855efc227SAndreas Gohr            if($info != false) break;
16955efc227SAndreas Gohr        }
17055efc227SAndreas Gohr
1715aaca723SGerrit Uitslag        if($info === false)  $info = '';
17255efc227SAndreas Gohr        if(is_array($info)){
17355efc227SAndreas Gohr            if(isset($info['val'])){
17455efc227SAndreas Gohr                $info = $info['val'];
17555efc227SAndreas Gohr            }else{
176e3b89425Sasivery                $arr = array();
177e3b89425Sasivery                foreach($info as $part){
178e3b89425Sasivery                    if(is_array($part)){
179e3b89425Sasivery                        if(isset($part['val'])){
180e3b89425Sasivery                            $arr[] = $part['val'];
181e3b89425Sasivery                        }else{
182e3b89425Sasivery                            $arr[] = join(', ',$part);
183e3b89425Sasivery                        }
184e3b89425Sasivery                    }else{
185e3b89425Sasivery                        $arr[] = $part;
186e3b89425Sasivery                    }
187e3b89425Sasivery                }
188e3b89425Sasivery                $info = join(', ',$arr);
18955efc227SAndreas Gohr            }
19055efc227SAndreas Gohr        }
19155efc227SAndreas Gohr        return trim($info);
19255efc227SAndreas Gohr    }
19355efc227SAndreas Gohr
19455efc227SAndreas Gohr    /**
19536df6fa3SAndreas Gohr     * Convinience function to set nearly all available Data
19636df6fa3SAndreas Gohr     * through one function
19736df6fa3SAndreas Gohr     *
19836df6fa3SAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
19942ea7f44SGerrit Uitslag     *
20042ea7f44SGerrit Uitslag     * @param string $field field name
20142ea7f44SGerrit Uitslag     * @param string $value
20242ea7f44SGerrit Uitslag     * @return bool success or fail
20336df6fa3SAndreas Gohr     */
2040b17fdc6SAndreas Gohr    function setField($field, $value) {
2056c16a3a9Sfiwswe        $lower_field = strtolower($field);
2066c16a3a9Sfiwswe        if(str_starts_with($lower_field, 'iptc.')){
20736df6fa3SAndreas Gohr            return $this->setIPTCField(substr($field,5),$value);
2086c16a3a9Sfiwswe        }elseif(str_starts_with($lower_field, 'exif.')){
20936df6fa3SAndreas Gohr            return $this->setExifField(substr($field,5),$value);
21036df6fa3SAndreas Gohr        }else{
21136df6fa3SAndreas Gohr            return $this->setExifField($field,$value);
21236df6fa3SAndreas Gohr        }
21336df6fa3SAndreas Gohr    }
21436df6fa3SAndreas Gohr
21536df6fa3SAndreas Gohr    /**
21636df6fa3SAndreas Gohr     * Convinience function to delete nearly all available Data
21736df6fa3SAndreas Gohr     * through one function
21836df6fa3SAndreas Gohr     *
21936df6fa3SAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
22042ea7f44SGerrit Uitslag     *
22142ea7f44SGerrit Uitslag     * @param string $field field name
22242ea7f44SGerrit Uitslag     * @return bool
22336df6fa3SAndreas Gohr     */
2240b17fdc6SAndreas Gohr    function deleteField($field) {
2256c16a3a9Sfiwswe        $lower_field = strtolower($field);
2266c16a3a9Sfiwswe        if(str_starts_with($lower_field, 'iptc.')){
22736df6fa3SAndreas Gohr            return $this->deleteIPTCField(substr($field,5));
2286c16a3a9Sfiwswe        }elseif(str_starts_with($lower_field, 'exif.')){
22936df6fa3SAndreas Gohr            return $this->deleteExifField(substr($field,5));
23036df6fa3SAndreas Gohr        }else{
23136df6fa3SAndreas Gohr            return $this->deleteExifField($field);
23236df6fa3SAndreas Gohr        }
23336df6fa3SAndreas Gohr    }
23436df6fa3SAndreas Gohr
23536df6fa3SAndreas Gohr    /**
23655efc227SAndreas Gohr     * Return a date field
23755efc227SAndreas Gohr     *
23855efc227SAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
23942ea7f44SGerrit Uitslag     *
24042ea7f44SGerrit Uitslag     * @param string $field
24142ea7f44SGerrit Uitslag     * @return false|string
24255efc227SAndreas Gohr     */
2430b17fdc6SAndreas Gohr    function getDateField($field) {
24455efc227SAndreas Gohr        if (!isset($this->_info['dates'])) {
24555efc227SAndreas Gohr            $this->_info['dates'] = $this->getDates();
24655efc227SAndreas Gohr        }
24755efc227SAndreas Gohr
24855efc227SAndreas Gohr        if (isset($this->_info['dates'][$field])) {
24955efc227SAndreas Gohr            return $this->_info['dates'][$field];
25055efc227SAndreas Gohr        }
25155efc227SAndreas Gohr
25255efc227SAndreas Gohr        return false;
25355efc227SAndreas Gohr    }
25455efc227SAndreas Gohr
25555efc227SAndreas Gohr    /**
25655efc227SAndreas Gohr     * Return a file info field
25755efc227SAndreas Gohr     *
25855efc227SAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
25942ea7f44SGerrit Uitslag     *
26042ea7f44SGerrit Uitslag     * @param string $field field name
26142ea7f44SGerrit Uitslag     * @return false|string
26255efc227SAndreas Gohr     */
2630b17fdc6SAndreas Gohr    function getFileField($field) {
26455efc227SAndreas Gohr        if (!isset($this->_info['file'])) {
26555efc227SAndreas Gohr            $this->_parseFileInfo();
26655efc227SAndreas Gohr        }
26755efc227SAndreas Gohr
26855efc227SAndreas Gohr        if (isset($this->_info['file'][$field])) {
26955efc227SAndreas Gohr            return $this->_info['file'][$field];
27055efc227SAndreas Gohr        }
27155efc227SAndreas Gohr
27255efc227SAndreas Gohr        return false;
27355efc227SAndreas Gohr    }
27455efc227SAndreas Gohr
27555efc227SAndreas Gohr    /**
27655efc227SAndreas Gohr     * Return the camera info (Maker and Model)
27755efc227SAndreas Gohr     *
27855efc227SAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
27955efc227SAndreas Gohr     * @todo   handle makernotes
28042ea7f44SGerrit Uitslag     *
28142ea7f44SGerrit Uitslag     * @return false|string
28255efc227SAndreas Gohr     */
28355efc227SAndreas Gohr    function getCamera(){
28455efc227SAndreas Gohr        $make  = $this->getField(array('Exif.Make','Exif.TIFFMake'));
28555efc227SAndreas Gohr        $model = $this->getField(array('Exif.Model','Exif.TIFFModel'));
28655efc227SAndreas Gohr        $cam = trim("$make $model");
28755efc227SAndreas Gohr        if(empty($cam)) return false;
28855efc227SAndreas Gohr        return $cam;
28955efc227SAndreas Gohr    }
29055efc227SAndreas Gohr
29155efc227SAndreas Gohr    /**
2926db72d46SJoe Lapp     * Return shutter speed as a ratio
2936db72d46SJoe Lapp     *
2946db72d46SJoe Lapp     * @author Joe Lapp <joe.lapp@pobox.com>
29542ea7f44SGerrit Uitslag     *
29642ea7f44SGerrit Uitslag     * @return string
2976db72d46SJoe Lapp     */
2980b17fdc6SAndreas Gohr    function getShutterSpeed() {
2996db72d46SJoe Lapp        if (!isset($this->_info['exif'])) {
3006db72d46SJoe Lapp            $this->_parseMarkerExif();
3016db72d46SJoe Lapp        }
3026db72d46SJoe Lapp        if(!isset($this->_info['exif']['ExposureTime'])){
3036db72d46SJoe Lapp            return '';
3046db72d46SJoe Lapp        }
3056db72d46SJoe Lapp
3066db72d46SJoe Lapp        $field = $this->_info['exif']['ExposureTime'];
3076db72d46SJoe Lapp        if($field['den'] == 1) return $field['num'];
3086db72d46SJoe Lapp        return $field['num'].'/'.$field['den'];
3096db72d46SJoe Lapp    }
3106db72d46SJoe Lapp
3116db72d46SJoe Lapp    /**
31255efc227SAndreas Gohr     * Return an EXIF field
31355efc227SAndreas Gohr     *
31455efc227SAndreas Gohr     * @author Sebastian Delmont <sdelmont@zonageek.com>
31542ea7f44SGerrit Uitslag     *
31642ea7f44SGerrit Uitslag     * @param string $field field name
31742ea7f44SGerrit Uitslag     * @return false|string
31855efc227SAndreas Gohr     */
3190b17fdc6SAndreas Gohr    function getExifField($field) {
32055efc227SAndreas Gohr        if (!isset($this->_info['exif'])) {
32155efc227SAndreas Gohr            $this->_parseMarkerExif();
32255efc227SAndreas Gohr        }
32355efc227SAndreas Gohr
32455efc227SAndreas Gohr        if ($this->_markers == null) {
32555efc227SAndreas Gohr            return false;
32655efc227SAndreas Gohr        }
32755efc227SAndreas Gohr
32855efc227SAndreas Gohr        if (isset($this->_info['exif'][$field])) {
32955efc227SAndreas Gohr            return $this->_info['exif'][$field];
33055efc227SAndreas Gohr        }
33155efc227SAndreas Gohr
33255efc227SAndreas Gohr        return false;
33355efc227SAndreas Gohr    }
33455efc227SAndreas Gohr
33555efc227SAndreas Gohr    /**
336431c7fc8Shakan.sandell     * Return an XMP field
337431c7fc8Shakan.sandell     *
338431c7fc8Shakan.sandell     * @author Hakan Sandell <hakan.sandell@mydata.se>
33942ea7f44SGerrit Uitslag     *
34042ea7f44SGerrit Uitslag     * @param string $field field name
34142ea7f44SGerrit Uitslag     * @return false|string
342431c7fc8Shakan.sandell     */
3430b17fdc6SAndreas Gohr    function getXmpField($field) {
344431c7fc8Shakan.sandell        if (!isset($this->_info['xmp'])) {
345431c7fc8Shakan.sandell            $this->_parseMarkerXmp();
346431c7fc8Shakan.sandell        }
347431c7fc8Shakan.sandell
348431c7fc8Shakan.sandell        if ($this->_markers == null) {
349431c7fc8Shakan.sandell            return false;
350431c7fc8Shakan.sandell        }
351431c7fc8Shakan.sandell
352431c7fc8Shakan.sandell        if (isset($this->_info['xmp'][$field])) {
353431c7fc8Shakan.sandell            return $this->_info['xmp'][$field];
354431c7fc8Shakan.sandell        }
355431c7fc8Shakan.sandell
356431c7fc8Shakan.sandell        return false;
357431c7fc8Shakan.sandell    }
358431c7fc8Shakan.sandell
359431c7fc8Shakan.sandell    /**
36055efc227SAndreas Gohr     * Return an Adobe Field
36155efc227SAndreas Gohr     *
36255efc227SAndreas Gohr     * @author Sebastian Delmont <sdelmont@zonageek.com>
36342ea7f44SGerrit Uitslag     *
36442ea7f44SGerrit Uitslag     * @param string $field field name
36542ea7f44SGerrit Uitslag     * @return false|string
36655efc227SAndreas Gohr     */
3670b17fdc6SAndreas Gohr    function getAdobeField($field) {
36855efc227SAndreas Gohr        if (!isset($this->_info['adobe'])) {
36955efc227SAndreas Gohr            $this->_parseMarkerAdobe();
37055efc227SAndreas Gohr        }
37155efc227SAndreas Gohr
37255efc227SAndreas Gohr        if ($this->_markers == null) {
37355efc227SAndreas Gohr            return false;
37455efc227SAndreas Gohr        }
37555efc227SAndreas Gohr
37655efc227SAndreas Gohr        if (isset($this->_info['adobe'][$field])) {
37755efc227SAndreas Gohr            return $this->_info['adobe'][$field];
37855efc227SAndreas Gohr        }
37955efc227SAndreas Gohr
38055efc227SAndreas Gohr        return false;
38155efc227SAndreas Gohr    }
38255efc227SAndreas Gohr
38355efc227SAndreas Gohr    /**
38455efc227SAndreas Gohr     * Return an IPTC field
38555efc227SAndreas Gohr     *
38655efc227SAndreas Gohr     * @author Sebastian Delmont <sdelmont@zonageek.com>
38742ea7f44SGerrit Uitslag     *
38842ea7f44SGerrit Uitslag     * @param string $field field name
38942ea7f44SGerrit Uitslag     * @return false|string
39055efc227SAndreas Gohr     */
3910b17fdc6SAndreas Gohr    function getIPTCField($field) {
39255efc227SAndreas Gohr        if (!isset($this->_info['iptc'])) {
39355efc227SAndreas Gohr            $this->_parseMarkerAdobe();
39455efc227SAndreas Gohr        }
39555efc227SAndreas Gohr
39655efc227SAndreas Gohr        if ($this->_markers == null) {
39755efc227SAndreas Gohr            return false;
39855efc227SAndreas Gohr        }
39955efc227SAndreas Gohr
40055efc227SAndreas Gohr        if (isset($this->_info['iptc'][$field])) {
40155efc227SAndreas Gohr            return $this->_info['iptc'][$field];
40255efc227SAndreas Gohr        }
40355efc227SAndreas Gohr
40455efc227SAndreas Gohr        return false;
40555efc227SAndreas Gohr    }
40655efc227SAndreas Gohr
40755efc227SAndreas Gohr    /**
40855efc227SAndreas Gohr     * Set an EXIF field
40955efc227SAndreas Gohr     *
41055efc227SAndreas Gohr     * @author Sebastian Delmont <sdelmont@zonageek.com>
411b5a81756SJoe Lapp     * @author Joe Lapp <joe.lapp@pobox.com>
41242ea7f44SGerrit Uitslag     *
41342ea7f44SGerrit Uitslag     * @param string $field field name
41442ea7f44SGerrit Uitslag     * @param string $value
41542ea7f44SGerrit Uitslag     * @return bool
41655efc227SAndreas Gohr     */
4170b17fdc6SAndreas Gohr    function setExifField($field, $value) {
41855efc227SAndreas Gohr        if (!isset($this->_info['exif'])) {
41955efc227SAndreas Gohr            $this->_parseMarkerExif();
42055efc227SAndreas Gohr        }
42155efc227SAndreas Gohr
42255efc227SAndreas Gohr        if ($this->_markers == null) {
42355efc227SAndreas Gohr            return false;
42455efc227SAndreas Gohr        }
42555efc227SAndreas Gohr
42655efc227SAndreas Gohr        if ($this->_info['exif'] == false) {
42755efc227SAndreas Gohr            $this->_info['exif'] = array();
42855efc227SAndreas Gohr        }
42955efc227SAndreas Gohr
430b5a81756SJoe Lapp        // make sure datetimes are in correct format
4316c16a3a9Sfiwswe        if(strlen($field) >= 8 && str_starts_with(strtolower($field), 'datetime')) {
4322401f18dSSyntaxseed            if(strlen($value) < 8 || $value[4] != ':' || $value[7] != ':') {
433b5a81756SJoe Lapp                $value = date('Y:m:d H:i:s', strtotime($value));
434b5a81756SJoe Lapp            }
435b5a81756SJoe Lapp        }
436b5a81756SJoe Lapp
43755efc227SAndreas Gohr        $this->_info['exif'][$field] = $value;
43855efc227SAndreas Gohr
43955efc227SAndreas Gohr        return true;
44055efc227SAndreas Gohr    }
44155efc227SAndreas Gohr
44255efc227SAndreas Gohr    /**
44355efc227SAndreas Gohr     * Set an Adobe Field
44455efc227SAndreas Gohr     *
44555efc227SAndreas Gohr     * @author Sebastian Delmont <sdelmont@zonageek.com>
44642ea7f44SGerrit Uitslag     *
44742ea7f44SGerrit Uitslag     * @param string $field field name
44842ea7f44SGerrit Uitslag     * @param string $value
44942ea7f44SGerrit Uitslag     * @return bool
45055efc227SAndreas Gohr     */
4510b17fdc6SAndreas Gohr    function setAdobeField($field, $value) {
45255efc227SAndreas Gohr        if (!isset($this->_info['adobe'])) {
45355efc227SAndreas Gohr            $this->_parseMarkerAdobe();
45455efc227SAndreas Gohr        }
45555efc227SAndreas Gohr
45655efc227SAndreas Gohr        if ($this->_markers == null) {
45755efc227SAndreas Gohr            return false;
45855efc227SAndreas Gohr        }
45955efc227SAndreas Gohr
46055efc227SAndreas Gohr        if ($this->_info['adobe'] == false) {
46155efc227SAndreas Gohr            $this->_info['adobe'] = array();
46255efc227SAndreas Gohr        }
46355efc227SAndreas Gohr
46455efc227SAndreas Gohr        $this->_info['adobe'][$field] = $value;
46555efc227SAndreas Gohr
46655efc227SAndreas Gohr        return true;
46755efc227SAndreas Gohr    }
46855efc227SAndreas Gohr
46955efc227SAndreas Gohr    /**
47023a34783SAndreas Gohr     * Calculates the multiplier needed to resize the image to the given
47123a34783SAndreas Gohr     * dimensions
47223a34783SAndreas Gohr     *
47323a34783SAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
47442ea7f44SGerrit Uitslag     *
47542ea7f44SGerrit Uitslag     * @param int $maxwidth
47642ea7f44SGerrit Uitslag     * @param int $maxheight
47742ea7f44SGerrit Uitslag     * @return float|int
47823a34783SAndreas Gohr     */
47923a34783SAndreas Gohr    function getResizeRatio($maxwidth,$maxheight=0){
48023a34783SAndreas Gohr        if(!$maxheight) $maxheight = $maxwidth;
48123a34783SAndreas Gohr
48223a34783SAndreas Gohr        $w = $this->getField('File.Width');
48323a34783SAndreas Gohr        $h = $this->getField('File.Height');
48423a34783SAndreas Gohr
48523a34783SAndreas Gohr        $ratio = 1;
48623a34783SAndreas Gohr        if($w >= $h){
48723a34783SAndreas Gohr            if($w >= $maxwidth){
48823a34783SAndreas Gohr                $ratio = $maxwidth/$w;
48923a34783SAndreas Gohr            }elseif($h > $maxheight){
49023a34783SAndreas Gohr                $ratio = $maxheight/$h;
49123a34783SAndreas Gohr            }
49223a34783SAndreas Gohr        }else{
49323a34783SAndreas Gohr            if($h >= $maxheight){
49423a34783SAndreas Gohr                $ratio = $maxheight/$h;
49523a34783SAndreas Gohr            }elseif($w > $maxwidth){
49623a34783SAndreas Gohr                $ratio = $maxwidth/$w;
49723a34783SAndreas Gohr            }
49823a34783SAndreas Gohr        }
49923a34783SAndreas Gohr        return $ratio;
50023a34783SAndreas Gohr    }
50123a34783SAndreas Gohr
50223a34783SAndreas Gohr
50323a34783SAndreas Gohr    /**
50455efc227SAndreas Gohr     * Set an IPTC field
50555efc227SAndreas Gohr     *
50655efc227SAndreas Gohr     * @author Sebastian Delmont <sdelmont@zonageek.com>
50742ea7f44SGerrit Uitslag     *
50842ea7f44SGerrit Uitslag     * @param string $field field name
50942ea7f44SGerrit Uitslag     * @param string $value
51042ea7f44SGerrit Uitslag     * @return bool
51155efc227SAndreas Gohr     */
5120b17fdc6SAndreas Gohr    function setIPTCField($field, $value) {
51355efc227SAndreas Gohr        if (!isset($this->_info['iptc'])) {
51455efc227SAndreas Gohr            $this->_parseMarkerAdobe();
51555efc227SAndreas Gohr        }
51655efc227SAndreas Gohr
51755efc227SAndreas Gohr        if ($this->_markers == null) {
51855efc227SAndreas Gohr            return false;
51955efc227SAndreas Gohr        }
52055efc227SAndreas Gohr
52155efc227SAndreas Gohr        if ($this->_info['iptc'] == false) {
52255efc227SAndreas Gohr            $this->_info['iptc'] = array();
52355efc227SAndreas Gohr        }
52455efc227SAndreas Gohr
52555efc227SAndreas Gohr        $this->_info['iptc'][$field] = $value;
52655efc227SAndreas Gohr
52755efc227SAndreas Gohr        return true;
52855efc227SAndreas Gohr    }
52955efc227SAndreas Gohr
53055efc227SAndreas Gohr    /**
53155efc227SAndreas Gohr     * Delete an EXIF field
53255efc227SAndreas Gohr     *
53355efc227SAndreas Gohr     * @author Sebastian Delmont <sdelmont@zonageek.com>
53442ea7f44SGerrit Uitslag     *
53542ea7f44SGerrit Uitslag     * @param string $field field name
53642ea7f44SGerrit Uitslag     * @return bool
53755efc227SAndreas Gohr     */
5380b17fdc6SAndreas Gohr    function deleteExifField($field) {
53955efc227SAndreas Gohr        if (!isset($this->_info['exif'])) {
54055efc227SAndreas Gohr            $this->_parseMarkerAdobe();
54155efc227SAndreas Gohr        }
54255efc227SAndreas Gohr
54355efc227SAndreas Gohr        if ($this->_markers == null) {
54455efc227SAndreas Gohr            return false;
54555efc227SAndreas Gohr        }
54655efc227SAndreas Gohr
54755efc227SAndreas Gohr        if ($this->_info['exif'] != false) {
54855efc227SAndreas Gohr            unset($this->_info['exif'][$field]);
54955efc227SAndreas Gohr        }
55055efc227SAndreas Gohr
55155efc227SAndreas Gohr        return true;
55255efc227SAndreas Gohr    }
55355efc227SAndreas Gohr
55455efc227SAndreas Gohr    /**
55555efc227SAndreas Gohr     * Delete an Adobe field
55655efc227SAndreas Gohr     *
55755efc227SAndreas Gohr     * @author Sebastian Delmont <sdelmont@zonageek.com>
55842ea7f44SGerrit Uitslag     *
55942ea7f44SGerrit Uitslag     * @param string $field field name
56042ea7f44SGerrit Uitslag     * @return bool
56155efc227SAndreas Gohr     */
5620b17fdc6SAndreas Gohr    function deleteAdobeField($field) {
56355efc227SAndreas Gohr        if (!isset($this->_info['adobe'])) {
56455efc227SAndreas Gohr            $this->_parseMarkerAdobe();
56555efc227SAndreas Gohr        }
56655efc227SAndreas Gohr
56755efc227SAndreas Gohr        if ($this->_markers == null) {
56855efc227SAndreas Gohr            return false;
56955efc227SAndreas Gohr        }
57055efc227SAndreas Gohr
57155efc227SAndreas Gohr        if ($this->_info['adobe'] != false) {
57255efc227SAndreas Gohr            unset($this->_info['adobe'][$field]);
57355efc227SAndreas Gohr        }
57455efc227SAndreas Gohr
57555efc227SAndreas Gohr        return true;
57655efc227SAndreas Gohr    }
57755efc227SAndreas Gohr
57855efc227SAndreas Gohr    /**
57955efc227SAndreas Gohr     * Delete an IPTC field
58055efc227SAndreas Gohr     *
58155efc227SAndreas Gohr     * @author Sebastian Delmont <sdelmont@zonageek.com>
58242ea7f44SGerrit Uitslag     *
58342ea7f44SGerrit Uitslag     * @param string $field field name
58442ea7f44SGerrit Uitslag     * @return bool
58555efc227SAndreas Gohr     */
5860b17fdc6SAndreas Gohr    function deleteIPTCField($field) {
58755efc227SAndreas Gohr        if (!isset($this->_info['iptc'])) {
58855efc227SAndreas Gohr            $this->_parseMarkerAdobe();
58955efc227SAndreas Gohr        }
59055efc227SAndreas Gohr
59155efc227SAndreas Gohr        if ($this->_markers == null) {
59255efc227SAndreas Gohr            return false;
59355efc227SAndreas Gohr        }
59455efc227SAndreas Gohr
59555efc227SAndreas Gohr        if ($this->_info['iptc'] != false) {
59655efc227SAndreas Gohr            unset($this->_info['iptc'][$field]);
59755efc227SAndreas Gohr        }
59855efc227SAndreas Gohr
59955efc227SAndreas Gohr        return true;
60055efc227SAndreas Gohr    }
60155efc227SAndreas Gohr
60255efc227SAndreas Gohr    /**
60355efc227SAndreas Gohr     * Get the image's title, tries various fields
60455efc227SAndreas Gohr     *
60555efc227SAndreas Gohr     * @param int $max maximum number chars (keeps words)
60642ea7f44SGerrit Uitslag     * @return false|string
60759bc3b48SGerrit Uitslag     *
60855efc227SAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
60955efc227SAndreas Gohr     */
61055efc227SAndreas Gohr    function getTitle($max=80){
61155efc227SAndreas Gohr        // try various fields
61255efc227SAndreas Gohr        $cap = $this->getField(array('Iptc.Headline',
61355efc227SAndreas Gohr                    'Iptc.Caption',
614431c7fc8Shakan.sandell                    'Xmp.dc:title',
61555efc227SAndreas Gohr                    'Exif.UserComment',
61655efc227SAndreas Gohr                    'Exif.TIFFUserComment',
6172684e50aSAndreas Gohr                    'Exif.TIFFImageDescription',
6182684e50aSAndreas Gohr                    'File.Name'));
61955efc227SAndreas Gohr        if (empty($cap)) return false;
62055efc227SAndreas Gohr
62155efc227SAndreas Gohr        if(!$max) return $cap;
62255efc227SAndreas Gohr        // Shorten to 80 chars (keeping words)
62355efc227SAndreas Gohr        $new = preg_replace('/\n.+$/','',wordwrap($cap, $max));
62455efc227SAndreas Gohr        if($new != $cap) $new .= '...';
62555efc227SAndreas Gohr
62655efc227SAndreas Gohr        return $new;
62755efc227SAndreas Gohr    }
62855efc227SAndreas Gohr
62955efc227SAndreas Gohr    /**
63055efc227SAndreas Gohr     * Gather various date fields
63155efc227SAndreas Gohr     *
63255efc227SAndreas Gohr     * @author Sebastian Delmont <sdelmont@zonageek.com>
63342ea7f44SGerrit Uitslag     *
63442ea7f44SGerrit Uitslag     * @return array|bool
63555efc227SAndreas Gohr     */
6360b17fdc6SAndreas Gohr    function getDates() {
63755efc227SAndreas Gohr        $this->_parseAll();
63855efc227SAndreas Gohr        if ($this->_markers == null) {
639a73b5b7eSAndreas Gohr            if (@isset($this->_info['file']['UnixTime'])) {
64059bc3b48SGerrit Uitslag                $dates = array();
641a73b5b7eSAndreas Gohr                $dates['FileModified'] = $this->_info['file']['UnixTime'];
642a73b5b7eSAndreas Gohr                $dates['Time'] = $this->_info['file']['UnixTime'];
643a73b5b7eSAndreas Gohr                $dates['TimeSource'] = 'FileModified';
644a73b5b7eSAndreas Gohr                $dates['TimeStr'] = date("Y-m-d H:i:s", $this->_info['file']['UnixTime']);
645a73b5b7eSAndreas Gohr                $dates['EarliestTime'] = $this->_info['file']['UnixTime'];
646a73b5b7eSAndreas Gohr                $dates['EarliestTimeSource'] = 'FileModified';
647a73b5b7eSAndreas Gohr                $dates['EarliestTimeStr'] = date("Y-m-d H:i:s", $this->_info['file']['UnixTime']);
648a73b5b7eSAndreas Gohr                $dates['LatestTime'] = $this->_info['file']['UnixTime'];
649a73b5b7eSAndreas Gohr                $dates['LatestTimeSource'] = 'FileModified';
650a73b5b7eSAndreas Gohr                $dates['LatestTimeStr'] = date("Y-m-d H:i:s", $this->_info['file']['UnixTime']);
651a73b5b7eSAndreas Gohr                return $dates;
652a73b5b7eSAndreas Gohr            }
65355efc227SAndreas Gohr            return false;
65455efc227SAndreas Gohr        }
65555efc227SAndreas Gohr
65655efc227SAndreas Gohr        $dates = array();
65755efc227SAndreas Gohr
65855efc227SAndreas Gohr        $latestTime = 0;
65955efc227SAndreas Gohr        $latestTimeSource = "";
66055efc227SAndreas Gohr        $earliestTime = time();
66155efc227SAndreas Gohr        $earliestTimeSource = "";
66255efc227SAndreas Gohr
663baea39e3SAndreas Gohr        $exifDateFields = array(
664baea39e3SAndreas Gohr            'DateTime' => 'ExifDateTime',
665baea39e3SAndreas Gohr            'DateTimeOriginal' => 'ExifDateTimeOriginal',
666baea39e3SAndreas Gohr            'DateTimeDigitized' => 'ExifDateTimeDigitized',
667baea39e3SAndreas Gohr        );
66855efc227SAndreas Gohr
669baea39e3SAndreas Gohr        foreach ($exifDateFields as $field => $source) {
670baea39e3SAndreas Gohr            if (!isset($this->_info['exif'][$field])) {
671baea39e3SAndreas Gohr                continue;
672baea39e3SAndreas Gohr            }
67355efc227SAndreas Gohr
674baea39e3SAndreas Gohr            $dates[$source] = $this->_info['exif'][$field];
675baea39e3SAndreas Gohr
676baea39e3SAndreas Gohr            foreach ($this->parseExifDateTime($this->_info['exif'][$field]) as $t) {
6772114dafdSAndreas Gohr                if ($t && $t > $latestTime) {
67855efc227SAndreas Gohr                    $latestTime = $t;
679baea39e3SAndreas Gohr                    $latestTimeSource = $source;
68055efc227SAndreas Gohr                }
68155efc227SAndreas Gohr
6822114dafdSAndreas Gohr                if ($t && $t < $earliestTime) {
68355efc227SAndreas Gohr                    $earliestTime = $t;
684baea39e3SAndreas Gohr                    $earliestTimeSource = $source;
68555efc227SAndreas Gohr                }
68655efc227SAndreas Gohr            }
68755efc227SAndreas Gohr        }
68855efc227SAndreas Gohr
68955efc227SAndreas Gohr        if (@isset($this->_info['iptc']['DateCreated'])) {
69055efc227SAndreas Gohr            $dates['IPTCDateCreated'] = $this->_info['iptc']['DateCreated'];
69155efc227SAndreas Gohr
69255efc227SAndreas Gohr            $aux = $this->_info['iptc']['DateCreated'];
69355efc227SAndreas Gohr            $aux = substr($aux, 0, 4) . "-" . substr($aux, 4, 2) . "-" . substr($aux, 6, 2);
69455efc227SAndreas Gohr            $t = strtotime($aux);
69555efc227SAndreas Gohr
6962114dafdSAndreas Gohr            if ($t && $t > $latestTime) {
69755efc227SAndreas Gohr                $latestTime = $t;
69855efc227SAndreas Gohr                $latestTimeSource = "IPTCDateCreated";
69955efc227SAndreas Gohr            }
70055efc227SAndreas Gohr
7012114dafdSAndreas Gohr            if ($t && $t < $earliestTime) {
70255efc227SAndreas Gohr                $earliestTime = $t;
70355efc227SAndreas Gohr                $earliestTimeSource = "IPTCDateCreated";
70455efc227SAndreas Gohr            }
70555efc227SAndreas Gohr        }
70655efc227SAndreas Gohr
70755efc227SAndreas Gohr        if (@isset($this->_info['file']['UnixTime'])) {
70855efc227SAndreas Gohr            $dates['FileModified'] = $this->_info['file']['UnixTime'];
70955efc227SAndreas Gohr
71055efc227SAndreas Gohr            $t = $this->_info['file']['UnixTime'];
71155efc227SAndreas Gohr
7122114dafdSAndreas Gohr            if ($t && $t > $latestTime) {
71355efc227SAndreas Gohr                $latestTime = $t;
71455efc227SAndreas Gohr                $latestTimeSource = "FileModified";
71555efc227SAndreas Gohr            }
71655efc227SAndreas Gohr
7172114dafdSAndreas Gohr            if ($t && $t < $earliestTime) {
71855efc227SAndreas Gohr                $earliestTime = $t;
71955efc227SAndreas Gohr                $earliestTimeSource = "FileModified";
72055efc227SAndreas Gohr            }
72155efc227SAndreas Gohr        }
72255efc227SAndreas Gohr
72355efc227SAndreas Gohr        $dates['Time'] = $earliestTime;
72455efc227SAndreas Gohr        $dates['TimeSource'] = $earliestTimeSource;
72555efc227SAndreas Gohr        $dates['TimeStr'] = date("Y-m-d H:i:s", $earliestTime);
72655efc227SAndreas Gohr        $dates['EarliestTime'] = $earliestTime;
72755efc227SAndreas Gohr        $dates['EarliestTimeSource'] = $earliestTimeSource;
72855efc227SAndreas Gohr        $dates['EarliestTimeStr'] = date("Y-m-d H:i:s", $earliestTime);
72955efc227SAndreas Gohr        $dates['LatestTime'] = $latestTime;
73055efc227SAndreas Gohr        $dates['LatestTimeSource'] = $latestTimeSource;
73155efc227SAndreas Gohr        $dates['LatestTimeStr'] = date("Y-m-d H:i:s", $latestTime);
73255efc227SAndreas Gohr
73355efc227SAndreas Gohr        return $dates;
73455efc227SAndreas Gohr    }
73555efc227SAndreas Gohr
73655efc227SAndreas Gohr    /**
737baea39e3SAndreas Gohr     * Parse one or more EXIF date strings to unix timestamps
738baea39e3SAndreas Gohr     *
739baea39e3SAndreas Gohr     * EXIF tags can occur multiple times in different IFDs. In that case the
740baea39e3SAndreas Gohr     * metadata parser stores the field as an array of values.
741baea39e3SAndreas Gohr     *
742baea39e3SAndreas Gohr     * @return int[]
743baea39e3SAndreas Gohr     */
744baea39e3SAndreas Gohr    protected function parseExifDateTime(string|array $value): array {
745baea39e3SAndreas Gohr        $timestamps = [];
746baea39e3SAndreas Gohr        foreach ((array) $value as $date) {
747baea39e3SAndreas Gohr            if (is_array($date) && isset($date['val'])) {
748baea39e3SAndreas Gohr                $date = $date['val'];
749baea39e3SAndreas Gohr            }
750baea39e3SAndreas Gohr
751baea39e3SAndreas Gohr            if (!is_string($date) || strlen($date) < 10) {
752baea39e3SAndreas Gohr                continue;
753baea39e3SAndreas Gohr            }
754baea39e3SAndreas Gohr
755baea39e3SAndreas Gohr            $date[4] = '-';
756baea39e3SAndreas Gohr            $date[7] = '-';
757baea39e3SAndreas Gohr            $timestamp = strtotime($date);
758baea39e3SAndreas Gohr            if ($timestamp) {
759baea39e3SAndreas Gohr                $timestamps[] = $timestamp;
760baea39e3SAndreas Gohr            }
761baea39e3SAndreas Gohr        }
762baea39e3SAndreas Gohr
763baea39e3SAndreas Gohr        return $timestamps;
764baea39e3SAndreas Gohr    }
765baea39e3SAndreas Gohr
766baea39e3SAndreas Gohr    /**
76755efc227SAndreas Gohr     * Get the image width, tries various fields
76855efc227SAndreas Gohr     *
76955efc227SAndreas Gohr     * @author Sebastian Delmont <sdelmont@zonageek.com>
77042ea7f44SGerrit Uitslag     *
77142ea7f44SGerrit Uitslag     * @return false|string
77255efc227SAndreas Gohr     */
7730b17fdc6SAndreas Gohr    function getWidth() {
77455efc227SAndreas Gohr        if (!isset($this->_info['sof'])) {
77555efc227SAndreas Gohr            $this->_parseMarkerSOF();
77655efc227SAndreas Gohr        }
77755efc227SAndreas Gohr
77855efc227SAndreas Gohr        if ($this->_markers == null) {
77955efc227SAndreas Gohr            return false;
78055efc227SAndreas Gohr        }
78155efc227SAndreas Gohr
78255efc227SAndreas Gohr        if (isset($this->_info['sof']['ImageWidth'])) {
78355efc227SAndreas Gohr            return $this->_info['sof']['ImageWidth'];
78455efc227SAndreas Gohr        }
78555efc227SAndreas Gohr
78655efc227SAndreas Gohr        if (!isset($this->_info['exif'])) {
78755efc227SAndreas Gohr            $this->_parseMarkerExif();
78855efc227SAndreas Gohr        }
78955efc227SAndreas Gohr
79055efc227SAndreas Gohr        if (isset($this->_info['exif']['PixelXDimension'])) {
79155efc227SAndreas Gohr            return $this->_info['exif']['PixelXDimension'];
79255efc227SAndreas Gohr        }
79355efc227SAndreas Gohr
79455efc227SAndreas Gohr        return false;
79555efc227SAndreas Gohr    }
79655efc227SAndreas Gohr
79755efc227SAndreas Gohr    /**
79855efc227SAndreas Gohr     * Get the image height, tries various fields
79955efc227SAndreas Gohr     *
80055efc227SAndreas Gohr     * @author Sebastian Delmont <sdelmont@zonageek.com>
80142ea7f44SGerrit Uitslag     *
80242ea7f44SGerrit Uitslag     * @return false|string
80355efc227SAndreas Gohr     */
8040b17fdc6SAndreas Gohr    function getHeight() {
80555efc227SAndreas Gohr        if (!isset($this->_info['sof'])) {
80655efc227SAndreas Gohr            $this->_parseMarkerSOF();
80755efc227SAndreas Gohr        }
80855efc227SAndreas Gohr
80955efc227SAndreas Gohr        if ($this->_markers == null) {
81055efc227SAndreas Gohr            return false;
81155efc227SAndreas Gohr        }
81255efc227SAndreas Gohr
81355efc227SAndreas Gohr        if (isset($this->_info['sof']['ImageHeight'])) {
81455efc227SAndreas Gohr            return $this->_info['sof']['ImageHeight'];
81555efc227SAndreas Gohr        }
81655efc227SAndreas Gohr
81755efc227SAndreas Gohr        if (!isset($this->_info['exif'])) {
81855efc227SAndreas Gohr            $this->_parseMarkerExif();
81955efc227SAndreas Gohr        }
82055efc227SAndreas Gohr
82155efc227SAndreas Gohr        if (isset($this->_info['exif']['PixelYDimension'])) {
82255efc227SAndreas Gohr            return $this->_info['exif']['PixelYDimension'];
82355efc227SAndreas Gohr        }
82455efc227SAndreas Gohr
82555efc227SAndreas Gohr        return false;
82655efc227SAndreas Gohr    }
82755efc227SAndreas Gohr
82855efc227SAndreas Gohr    /**
82955efc227SAndreas Gohr     * Get an dimension string for use in img tag
83055efc227SAndreas Gohr     *
83155efc227SAndreas Gohr     * @author Sebastian Delmont <sdelmont@zonageek.com>
83242ea7f44SGerrit Uitslag     *
83342ea7f44SGerrit Uitslag     * @return false|string
83455efc227SAndreas Gohr     */
8350b17fdc6SAndreas Gohr    function getDimStr() {
83655efc227SAndreas Gohr        if ($this->_markers == null) {
83755efc227SAndreas Gohr            return false;
83855efc227SAndreas Gohr        }
83955efc227SAndreas Gohr
84055efc227SAndreas Gohr        $w = $this->getWidth();
84155efc227SAndreas Gohr        $h = $this->getHeight();
84255efc227SAndreas Gohr
84355efc227SAndreas Gohr        return "width='" . $w . "' height='" . $h . "'";
84455efc227SAndreas Gohr    }
84555efc227SAndreas Gohr
84655efc227SAndreas Gohr    /**
84755efc227SAndreas Gohr     * Checks for an embedded thumbnail
84855efc227SAndreas Gohr     *
84955efc227SAndreas Gohr     * @author Sebastian Delmont <sdelmont@zonageek.com>
85042ea7f44SGerrit Uitslag     *
85142ea7f44SGerrit Uitslag     * @param string $which possible values: 'any', 'exif' or 'adobe'
85242ea7f44SGerrit Uitslag     * @return false|string
85355efc227SAndreas Gohr     */
8540b17fdc6SAndreas Gohr    function hasThumbnail($which = 'any') {
85555efc227SAndreas Gohr        if (($which == 'any') || ($which == 'exif')) {
85655efc227SAndreas Gohr            if (!isset($this->_info['exif'])) {
85755efc227SAndreas Gohr                $this->_parseMarkerExif();
85855efc227SAndreas Gohr            }
85955efc227SAndreas Gohr
86055efc227SAndreas Gohr            if ($this->_markers == null) {
86155efc227SAndreas Gohr                return false;
86255efc227SAndreas Gohr            }
86355efc227SAndreas Gohr
86455efc227SAndreas Gohr            if (isset($this->_info['exif']) && is_array($this->_info['exif'])) {
86555efc227SAndreas Gohr                if (isset($this->_info['exif']['JFIFThumbnail'])) {
86655efc227SAndreas Gohr                    return 'exif';
86755efc227SAndreas Gohr                }
86855efc227SAndreas Gohr            }
86955efc227SAndreas Gohr        }
87055efc227SAndreas Gohr
87155efc227SAndreas Gohr        if ($which == 'adobe') {
87255efc227SAndreas Gohr            if (!isset($this->_info['adobe'])) {
87355efc227SAndreas Gohr                $this->_parseMarkerAdobe();
87455efc227SAndreas Gohr            }
87555efc227SAndreas Gohr
87655efc227SAndreas Gohr            if ($this->_markers == null) {
87755efc227SAndreas Gohr                return false;
87855efc227SAndreas Gohr            }
87955efc227SAndreas Gohr
88055efc227SAndreas Gohr            if (isset($this->_info['adobe']) && is_array($this->_info['adobe'])) {
88155efc227SAndreas Gohr                if (isset($this->_info['adobe']['ThumbnailData'])) {
88255efc227SAndreas Gohr                    return 'exif';
88355efc227SAndreas Gohr                }
88455efc227SAndreas Gohr            }
88555efc227SAndreas Gohr        }
88655efc227SAndreas Gohr
88755efc227SAndreas Gohr        return false;
88855efc227SAndreas Gohr    }
88955efc227SAndreas Gohr
89055efc227SAndreas Gohr    /**
89155efc227SAndreas Gohr     * Send embedded thumbnail to browser
89255efc227SAndreas Gohr     *
89355efc227SAndreas Gohr     * @author Sebastian Delmont <sdelmont@zonageek.com>
89442ea7f44SGerrit Uitslag     *
89542ea7f44SGerrit Uitslag     * @param string $which possible values: 'any', 'exif' or 'adobe'
89642ea7f44SGerrit Uitslag     * @return bool
89755efc227SAndreas Gohr     */
8980b17fdc6SAndreas Gohr    function sendThumbnail($which = 'any') {
89955efc227SAndreas Gohr        $data = null;
90055efc227SAndreas Gohr
90155efc227SAndreas Gohr        if (($which == 'any') || ($which == 'exif')) {
90255efc227SAndreas Gohr            if (!isset($this->_info['exif'])) {
90355efc227SAndreas Gohr                $this->_parseMarkerExif();
90455efc227SAndreas Gohr            }
90555efc227SAndreas Gohr
90655efc227SAndreas Gohr            if ($this->_markers == null) {
90755efc227SAndreas Gohr                return false;
90855efc227SAndreas Gohr            }
90955efc227SAndreas Gohr
91055efc227SAndreas Gohr            if (isset($this->_info['exif']) && is_array($this->_info['exif'])) {
91155efc227SAndreas Gohr                if (isset($this->_info['exif']['JFIFThumbnail'])) {
91255efc227SAndreas Gohr                    $data =& $this->_info['exif']['JFIFThumbnail'];
91355efc227SAndreas Gohr                }
91455efc227SAndreas Gohr            }
91555efc227SAndreas Gohr        }
91655efc227SAndreas Gohr
91755efc227SAndreas Gohr        if (($which == 'adobe') || ($data == null)){
91855efc227SAndreas Gohr            if (!isset($this->_info['adobe'])) {
91955efc227SAndreas Gohr                $this->_parseMarkerAdobe();
92055efc227SAndreas Gohr            }
92155efc227SAndreas Gohr
92255efc227SAndreas Gohr            if ($this->_markers == null) {
92355efc227SAndreas Gohr                return false;
92455efc227SAndreas Gohr            }
92555efc227SAndreas Gohr
92655efc227SAndreas Gohr            if (isset($this->_info['adobe']) && is_array($this->_info['adobe'])) {
92755efc227SAndreas Gohr                if (isset($this->_info['adobe']['ThumbnailData'])) {
92855efc227SAndreas Gohr                    $data =& $this->_info['adobe']['ThumbnailData'];
92955efc227SAndreas Gohr                }
93055efc227SAndreas Gohr            }
93155efc227SAndreas Gohr        }
93255efc227SAndreas Gohr
93355efc227SAndreas Gohr        if ($data != null) {
93455efc227SAndreas Gohr            header("Content-type: image/jpeg");
93555efc227SAndreas Gohr            echo $data;
93655efc227SAndreas Gohr            return true;
93755efc227SAndreas Gohr        }
93855efc227SAndreas Gohr
93955efc227SAndreas Gohr        return false;
94055efc227SAndreas Gohr    }
94155efc227SAndreas Gohr
94255efc227SAndreas Gohr    /**
94355efc227SAndreas Gohr     * Save changed Metadata
94455efc227SAndreas Gohr     *
94555efc227SAndreas Gohr     * @author Sebastian Delmont <sdelmont@zonageek.com>
94636df6fa3SAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
94742ea7f44SGerrit Uitslag     *
94842ea7f44SGerrit Uitslag     * @param string $fileName file name or empty string for a random name
94942ea7f44SGerrit Uitslag     * @return bool
95055efc227SAndreas Gohr     */
95155efc227SAndreas Gohr    function save($fileName = "") {
95255efc227SAndreas Gohr        if ($fileName == "") {
95336df6fa3SAndreas Gohr            $tmpName = tempnam(dirname($this->_fileName),'_metatemp_');
95455efc227SAndreas Gohr            $this->_writeJPEG($tmpName);
95579e79377SAndreas Gohr            if (file_exists($tmpName)) {
956bf5e5a5bSAndreas Gohr                return io_rename($tmpName, $this->_fileName);
95755efc227SAndreas Gohr            }
95836df6fa3SAndreas Gohr        } else {
95936df6fa3SAndreas Gohr            return $this->_writeJPEG($fileName);
96055efc227SAndreas Gohr        }
96136df6fa3SAndreas Gohr        return false;
96255efc227SAndreas Gohr    }
96355efc227SAndreas Gohr
96455efc227SAndreas Gohr    /*************************************************************/
96555efc227SAndreas Gohr    /* PRIVATE FUNCTIONS (Internal Use Only!)                    */
96655efc227SAndreas Gohr    /*************************************************************/
96755efc227SAndreas Gohr
96855efc227SAndreas Gohr    /*************************************************************/
9695aaca723SGerrit Uitslag    function _dispose($fileName = "") {
97055efc227SAndreas Gohr        $this->_fileName = $fileName;
97155efc227SAndreas Gohr
97255efc227SAndreas Gohr        $this->_fp = null;
97355efc227SAndreas Gohr        $this->_type = 'unknown';
97455efc227SAndreas Gohr
97555efc227SAndreas Gohr        unset($this->_markers);
97655efc227SAndreas Gohr        unset($this->_info);
97755efc227SAndreas Gohr    }
97855efc227SAndreas Gohr
97955efc227SAndreas Gohr    /*************************************************************/
9800b17fdc6SAndreas Gohr    function _readJPEG() {
98155efc227SAndreas Gohr        unset($this->_markers);
982a73b5b7eSAndreas Gohr        //unset($this->_info);
98355efc227SAndreas Gohr        $this->_markers = array();
984a73b5b7eSAndreas Gohr        //$this->_info = array();
98555efc227SAndreas Gohr
98655efc227SAndreas Gohr        $this->_fp = @fopen($this->_fileName, 'rb');
98755efc227SAndreas Gohr        if ($this->_fp) {
98855efc227SAndreas Gohr            if (file_exists($this->_fileName)) {
98955efc227SAndreas Gohr                $this->_type = 'file';
99055efc227SAndreas Gohr            }
99155efc227SAndreas Gohr            else {
99255efc227SAndreas Gohr                $this->_type = 'url';
99355efc227SAndreas Gohr            }
9940b17fdc6SAndreas Gohr        } else {
99555efc227SAndreas Gohr            $this->_fp = null;
99655efc227SAndreas Gohr            return false;  // ERROR: Can't open file
99755efc227SAndreas Gohr        }
99855efc227SAndreas Gohr
99955efc227SAndreas Gohr        // Check for the JPEG signature
100055efc227SAndreas Gohr        $c1 = ord(fgetc($this->_fp));
100155efc227SAndreas Gohr        $c2 = ord(fgetc($this->_fp));
100255efc227SAndreas Gohr
100355efc227SAndreas Gohr        if ($c1 != 0xFF || $c2 != 0xD8) {   // (0xFF + SOI)
100455efc227SAndreas Gohr            $this->_markers = null;
100555efc227SAndreas Gohr            return false;  // ERROR: File is not a JPEG
100655efc227SAndreas Gohr        }
100755efc227SAndreas Gohr
100855efc227SAndreas Gohr        $count = 0;
100955efc227SAndreas Gohr
101055efc227SAndreas Gohr        $done = false;
101155efc227SAndreas Gohr        $ok = true;
101255efc227SAndreas Gohr
101355efc227SAndreas Gohr        while (!$done) {
101455efc227SAndreas Gohr            $capture = false;
101555efc227SAndreas Gohr
101655efc227SAndreas Gohr            // First, skip any non 0xFF bytes
101755efc227SAndreas Gohr            $discarded = 0;
101855efc227SAndreas Gohr            $c = ord(fgetc($this->_fp));
101955efc227SAndreas Gohr            while (!feof($this->_fp) && ($c != 0xFF)) {
102055efc227SAndreas Gohr                $discarded++;
102155efc227SAndreas Gohr                $c = ord(fgetc($this->_fp));
102255efc227SAndreas Gohr            }
102355efc227SAndreas Gohr            // Then skip all 0xFF until the marker byte
102455efc227SAndreas Gohr            do {
102555efc227SAndreas Gohr                $marker = ord(fgetc($this->_fp));
102655efc227SAndreas Gohr            } while (!feof($this->_fp) && ($marker == 0xFF));
102755efc227SAndreas Gohr
102855efc227SAndreas Gohr            if (feof($this->_fp)) {
102955efc227SAndreas Gohr                return false; // ERROR: Unexpected EOF
103055efc227SAndreas Gohr            }
103155efc227SAndreas Gohr            if ($discarded != 0) {
103255efc227SAndreas Gohr                return false; // ERROR: Extraneous data
103355efc227SAndreas Gohr            }
103455efc227SAndreas Gohr
103555efc227SAndreas Gohr            $length = ord(fgetc($this->_fp)) * 256 + ord(fgetc($this->_fp));
103655efc227SAndreas Gohr            if (feof($this->_fp)) {
103755efc227SAndreas Gohr                return false; // ERROR: Unexpected EOF
103855efc227SAndreas Gohr            }
103955efc227SAndreas Gohr            if ($length < 2) {
104055efc227SAndreas Gohr                return false; // ERROR: Extraneous data
104155efc227SAndreas Gohr            }
104255efc227SAndreas Gohr            $length = $length - 2; // The length we got counts itself
104355efc227SAndreas Gohr
104455efc227SAndreas Gohr            switch ($marker) {
104555efc227SAndreas Gohr                case 0xC0:    // SOF0
104655efc227SAndreas Gohr                case 0xC1:    // SOF1
104755efc227SAndreas Gohr                case 0xC2:    // SOF2
104855efc227SAndreas Gohr                case 0xC9:    // SOF9
104955efc227SAndreas Gohr                case 0xE0:    // APP0: JFIF data
1050431c7fc8Shakan.sandell                case 0xE1:    // APP1: EXIF or XMP data
105155efc227SAndreas Gohr                case 0xED:    // APP13: IPTC / Photoshop data
105255efc227SAndreas Gohr                    $capture = true;
105355efc227SAndreas Gohr                    break;
105455efc227SAndreas Gohr                case 0xDA:    // SOS: Start of scan... the image itself and the last block on the file
105555efc227SAndreas Gohr                    $capture = false;
105655efc227SAndreas Gohr                    $length = -1;  // This field has no length... it includes all data until EOF
105755efc227SAndreas Gohr                    $done = true;
105855efc227SAndreas Gohr                    break;
105955efc227SAndreas Gohr                default:
106055efc227SAndreas Gohr                    $capture = true;//false;
106155efc227SAndreas Gohr                    break;
106255efc227SAndreas Gohr            }
106355efc227SAndreas Gohr
106455efc227SAndreas Gohr            $this->_markers[$count] = array();
106555efc227SAndreas Gohr            $this->_markers[$count]['marker'] = $marker;
106655efc227SAndreas Gohr            $this->_markers[$count]['length'] = $length;
106755efc227SAndreas Gohr
106855efc227SAndreas Gohr            if ($capture) {
1069ed3655c4STom N Harris                if ($length)
10700ea5ced2SGerrit Uitslag                    $this->_markers[$count]['data'] = fread($this->_fp, $length);
1071ed3655c4STom N Harris                else
1072ed3655c4STom N Harris                    $this->_markers[$count]['data'] = "";
107355efc227SAndreas Gohr            }
107455efc227SAndreas Gohr            elseif (!$done) {
107555efc227SAndreas Gohr                $result = @fseek($this->_fp, $length, SEEK_CUR);
107655efc227SAndreas Gohr                // fseek doesn't seem to like HTTP 'files', but fgetc has no problem
107755efc227SAndreas Gohr                if (!($result === 0)) {
107855efc227SAndreas Gohr                    for ($i = 0; $i < $length; $i++) {
107955efc227SAndreas Gohr                        fgetc($this->_fp);
108055efc227SAndreas Gohr                    }
108155efc227SAndreas Gohr                }
108255efc227SAndreas Gohr            }
108355efc227SAndreas Gohr            $count++;
108455efc227SAndreas Gohr        }
108555efc227SAndreas Gohr
108655efc227SAndreas Gohr        if ($this->_fp) {
108755efc227SAndreas Gohr            fclose($this->_fp);
108855efc227SAndreas Gohr            $this->_fp = null;
108955efc227SAndreas Gohr        }
109055efc227SAndreas Gohr
109155efc227SAndreas Gohr        return $ok;
109255efc227SAndreas Gohr    }
109355efc227SAndreas Gohr
109455efc227SAndreas Gohr    /*************************************************************/
10950b17fdc6SAndreas Gohr    function _parseAll() {
10961017ae2eSAndreas Gohr        if (!isset($this->_info['file'])) {
10971017ae2eSAndreas Gohr            $this->_parseFileInfo();
10981017ae2eSAndreas Gohr        }
109955efc227SAndreas Gohr        if (!isset($this->_markers)) {
110055efc227SAndreas Gohr            $this->_readJPEG();
110155efc227SAndreas Gohr        }
110255efc227SAndreas Gohr
110355efc227SAndreas Gohr        if ($this->_markers == null) {
110455efc227SAndreas Gohr            return false;
110555efc227SAndreas Gohr        }
110655efc227SAndreas Gohr
110755efc227SAndreas Gohr        if (!isset($this->_info['jfif'])) {
110855efc227SAndreas Gohr            $this->_parseMarkerJFIF();
110955efc227SAndreas Gohr        }
111055efc227SAndreas Gohr        if (!isset($this->_info['jpeg'])) {
111155efc227SAndreas Gohr            $this->_parseMarkerSOF();
111255efc227SAndreas Gohr        }
111355efc227SAndreas Gohr        if (!isset($this->_info['exif'])) {
111455efc227SAndreas Gohr            $this->_parseMarkerExif();
111555efc227SAndreas Gohr        }
1116431c7fc8Shakan.sandell        if (!isset($this->_info['xmp'])) {
1117431c7fc8Shakan.sandell            $this->_parseMarkerXmp();
1118431c7fc8Shakan.sandell        }
111955efc227SAndreas Gohr        if (!isset($this->_info['adobe'])) {
112055efc227SAndreas Gohr            $this->_parseMarkerAdobe();
112155efc227SAndreas Gohr        }
112255efc227SAndreas Gohr    }
112355efc227SAndreas Gohr
112455efc227SAndreas Gohr    /*************************************************************/
1125276820f7SScrutinizer Auto-Fixer
1126276820f7SScrutinizer Auto-Fixer    /**
1127276820f7SScrutinizer Auto-Fixer     * @param string $outputName
1128f50a239bSTakamura     *
1129f50a239bSTakamura     * @return bool
1130276820f7SScrutinizer Auto-Fixer     */
11310b17fdc6SAndreas Gohr    function _writeJPEG($outputName) {
113255efc227SAndreas Gohr        $this->_parseAll();
113355efc227SAndreas Gohr
113455efc227SAndreas Gohr        $wroteEXIF = false;
113555efc227SAndreas Gohr        $wroteAdobe = false;
113655efc227SAndreas Gohr
113755efc227SAndreas Gohr        $this->_fp = @fopen($this->_fileName, 'r');
113855efc227SAndreas Gohr        if ($this->_fp) {
113955efc227SAndreas Gohr            if (file_exists($this->_fileName)) {
114055efc227SAndreas Gohr                $this->_type = 'file';
114155efc227SAndreas Gohr            }
114255efc227SAndreas Gohr            else {
114355efc227SAndreas Gohr                $this->_type = 'url';
114455efc227SAndreas Gohr            }
11450b17fdc6SAndreas Gohr        } else {
114655efc227SAndreas Gohr            $this->_fp = null;
114755efc227SAndreas Gohr            return false;  // ERROR: Can't open file
114855efc227SAndreas Gohr        }
114955efc227SAndreas Gohr
115055efc227SAndreas Gohr        $this->_fpout = fopen($outputName, 'wb');
11510b17fdc6SAndreas Gohr        if (!$this->_fpout) {
115255efc227SAndreas Gohr            $this->_fpout = null;
115355efc227SAndreas Gohr            fclose($this->_fp);
115455efc227SAndreas Gohr            $this->_fp = null;
115555efc227SAndreas Gohr            return false;  // ERROR: Can't open output file
115655efc227SAndreas Gohr        }
115755efc227SAndreas Gohr
115855efc227SAndreas Gohr        // Check for the JPEG signature
115955efc227SAndreas Gohr        $c1 = ord(fgetc($this->_fp));
116055efc227SAndreas Gohr        $c2 = ord(fgetc($this->_fp));
116155efc227SAndreas Gohr
116255efc227SAndreas Gohr        if ($c1 != 0xFF || $c2 != 0xD8) {   // (0xFF + SOI)
116355efc227SAndreas Gohr            return false;  // ERROR: File is not a JPEG
116455efc227SAndreas Gohr        }
116555efc227SAndreas Gohr
116655efc227SAndreas Gohr        fputs($this->_fpout, chr(0xFF), 1);
116755efc227SAndreas Gohr        fputs($this->_fpout, chr(0xD8), 1); // (0xFF + SOI)
116855efc227SAndreas Gohr
116955efc227SAndreas Gohr        $count = 0;
117055efc227SAndreas Gohr
117155efc227SAndreas Gohr        $done = false;
117255efc227SAndreas Gohr        $ok = true;
117355efc227SAndreas Gohr
117455efc227SAndreas Gohr        while (!$done) {
117555efc227SAndreas Gohr            // First, skip any non 0xFF bytes
117655efc227SAndreas Gohr            $discarded = 0;
117755efc227SAndreas Gohr            $c = ord(fgetc($this->_fp));
117855efc227SAndreas Gohr            while (!feof($this->_fp) && ($c != 0xFF)) {
117955efc227SAndreas Gohr                $discarded++;
118055efc227SAndreas Gohr                $c = ord(fgetc($this->_fp));
118155efc227SAndreas Gohr            }
118255efc227SAndreas Gohr            // Then skip all 0xFF until the marker byte
118355efc227SAndreas Gohr            do {
118455efc227SAndreas Gohr                $marker = ord(fgetc($this->_fp));
118555efc227SAndreas Gohr            } while (!feof($this->_fp) && ($marker == 0xFF));
118655efc227SAndreas Gohr
118755efc227SAndreas Gohr            if (feof($this->_fp)) {
118855efc227SAndreas Gohr                $ok = false;
118955efc227SAndreas Gohr                break; // ERROR: Unexpected EOF
119055efc227SAndreas Gohr            }
119155efc227SAndreas Gohr            if ($discarded != 0) {
119255efc227SAndreas Gohr                $ok = false;
119355efc227SAndreas Gohr                break; // ERROR: Extraneous data
119455efc227SAndreas Gohr            }
119555efc227SAndreas Gohr
119655efc227SAndreas Gohr            $length = ord(fgetc($this->_fp)) * 256 + ord(fgetc($this->_fp));
119755efc227SAndreas Gohr            if (feof($this->_fp)) {
119855efc227SAndreas Gohr                $ok = false;
119955efc227SAndreas Gohr                break; // ERROR: Unexpected EOF
120055efc227SAndreas Gohr            }
120155efc227SAndreas Gohr            if ($length < 2) {
120255efc227SAndreas Gohr                $ok = false;
120355efc227SAndreas Gohr                break; // ERROR: Extraneous data
120455efc227SAndreas Gohr            }
120555efc227SAndreas Gohr            $length = $length - 2; // The length we got counts itself
120655efc227SAndreas Gohr
120755efc227SAndreas Gohr            unset($data);
120855efc227SAndreas Gohr            if ($marker == 0xE1) { // APP1: EXIF data
120955efc227SAndreas Gohr                $data =& $this->_createMarkerEXIF();
121055efc227SAndreas Gohr                $wroteEXIF = true;
121155efc227SAndreas Gohr            }
121255efc227SAndreas Gohr            elseif ($marker == 0xED) { // APP13: IPTC / Photoshop data
121355efc227SAndreas Gohr                $data =& $this->_createMarkerAdobe();
121455efc227SAndreas Gohr                $wroteAdobe = true;
121555efc227SAndreas Gohr            }
121655efc227SAndreas Gohr            elseif ($marker == 0xDA) { // SOS: Start of scan... the image itself and the last block on the file
121755efc227SAndreas Gohr                $done = true;
121855efc227SAndreas Gohr            }
121955efc227SAndreas Gohr
122055efc227SAndreas Gohr            if (!$wroteEXIF && (($marker < 0xE0) || ($marker > 0xEF))) {
122155efc227SAndreas Gohr                if (isset($this->_info['exif']) && is_array($this->_info['exif'])) {
122255efc227SAndreas Gohr                    $exif =& $this->_createMarkerEXIF();
122355efc227SAndreas Gohr                    $this->_writeJPEGMarker(0xE1, strlen($exif), $exif, 0);
122455efc227SAndreas Gohr                    unset($exif);
122555efc227SAndreas Gohr                }
122655efc227SAndreas Gohr                $wroteEXIF = true;
122755efc227SAndreas Gohr            }
122855efc227SAndreas Gohr
122955efc227SAndreas Gohr            if (!$wroteAdobe && (($marker < 0xE0) || ($marker > 0xEF))) {
123055efc227SAndreas Gohr                if ((isset($this->_info['adobe']) && is_array($this->_info['adobe']))
123155efc227SAndreas Gohr                        || (isset($this->_info['iptc']) && is_array($this->_info['iptc']))) {
123255efc227SAndreas Gohr                    $adobe =& $this->_createMarkerAdobe();
123355efc227SAndreas Gohr                    $this->_writeJPEGMarker(0xED, strlen($adobe), $adobe, 0);
123455efc227SAndreas Gohr                    unset($adobe);
123555efc227SAndreas Gohr                }
123655efc227SAndreas Gohr                $wroteAdobe = true;
123755efc227SAndreas Gohr            }
123855efc227SAndreas Gohr
123955efc227SAndreas Gohr            $origLength = $length;
124055efc227SAndreas Gohr            if (isset($data)) {
124155efc227SAndreas Gohr                $length = strlen($data);
124255efc227SAndreas Gohr            }
124355efc227SAndreas Gohr
124455efc227SAndreas Gohr            if ($marker != -1) {
124555efc227SAndreas Gohr                $this->_writeJPEGMarker($marker, $length, $data, $origLength);
124655efc227SAndreas Gohr            }
124755efc227SAndreas Gohr        }
124855efc227SAndreas Gohr
124955efc227SAndreas Gohr        if ($this->_fp) {
125055efc227SAndreas Gohr            fclose($this->_fp);
125155efc227SAndreas Gohr            $this->_fp = null;
125255efc227SAndreas Gohr        }
125355efc227SAndreas Gohr
125455efc227SAndreas Gohr        if ($this->_fpout) {
125555efc227SAndreas Gohr            fclose($this->_fpout);
125655efc227SAndreas Gohr            $this->_fpout = null;
125755efc227SAndreas Gohr        }
125855efc227SAndreas Gohr
125955efc227SAndreas Gohr        return $ok;
126055efc227SAndreas Gohr    }
126155efc227SAndreas Gohr
126255efc227SAndreas Gohr    /*************************************************************/
1263276820f7SScrutinizer Auto-Fixer
1264276820f7SScrutinizer Auto-Fixer    /**
1265276820f7SScrutinizer Auto-Fixer     * @param integer $marker
1266276820f7SScrutinizer Auto-Fixer     * @param integer $length
1267f50a239bSTakamura     * @param string $data
1268276820f7SScrutinizer Auto-Fixer     * @param integer $origLength
1269f50a239bSTakamura     *
1270f50a239bSTakamura     * @return bool
1271276820f7SScrutinizer Auto-Fixer     */
12720b17fdc6SAndreas Gohr    function _writeJPEGMarker($marker, $length, &$data, $origLength) {
127355efc227SAndreas Gohr        if ($length <= 0) {
127455efc227SAndreas Gohr            return false;
127555efc227SAndreas Gohr        }
127655efc227SAndreas Gohr
127755efc227SAndreas Gohr        fputs($this->_fpout, chr(0xFF), 1);
127855efc227SAndreas Gohr        fputs($this->_fpout, chr($marker), 1);
127955efc227SAndreas Gohr        fputs($this->_fpout, chr((($length + 2) & 0x0000FF00) >> 8), 1);
128055efc227SAndreas Gohr        fputs($this->_fpout, chr((($length + 2) & 0x000000FF) >> 0), 1);
128155efc227SAndreas Gohr
128255efc227SAndreas Gohr        if (isset($data)) {
128355efc227SAndreas Gohr            // Copy the generated data
128455efc227SAndreas Gohr            fputs($this->_fpout, $data, $length);
128555efc227SAndreas Gohr
128655efc227SAndreas Gohr            if ($origLength > 0) {   // Skip the original data
128755efc227SAndreas Gohr                $result = @fseek($this->_fp, $origLength, SEEK_CUR);
128855efc227SAndreas Gohr                // fseek doesn't seem to like HTTP 'files', but fgetc has no problem
128955efc227SAndreas Gohr                if ($result != 0) {
129055efc227SAndreas Gohr                    for ($i = 0; $i < $origLength; $i++) {
129155efc227SAndreas Gohr                        fgetc($this->_fp);
129255efc227SAndreas Gohr                    }
129355efc227SAndreas Gohr                }
129455efc227SAndreas Gohr            }
12950b17fdc6SAndreas Gohr        } else {
129655efc227SAndreas Gohr            if ($marker == 0xDA) {  // Copy until EOF
129755efc227SAndreas Gohr                while (!feof($this->_fp)) {
1298ed3655c4STom N Harris                    $data = fread($this->_fp, 1024 * 16);
129955efc227SAndreas Gohr                    fputs($this->_fpout, $data, strlen($data));
130055efc227SAndreas Gohr                }
13010b17fdc6SAndreas Gohr            } else { // Copy only $length bytes
1302ed3655c4STom N Harris                $data = @fread($this->_fp, $length);
130355efc227SAndreas Gohr                fputs($this->_fpout, $data, $length);
130455efc227SAndreas Gohr            }
130555efc227SAndreas Gohr        }
130655efc227SAndreas Gohr
130755efc227SAndreas Gohr        return true;
130855efc227SAndreas Gohr    }
130955efc227SAndreas Gohr
131023a34783SAndreas Gohr    /**
131123a34783SAndreas Gohr     * Gets basic info from the file - should work with non-JPEGs
131223a34783SAndreas Gohr     *
131323a34783SAndreas Gohr     * @author  Sebastian Delmont <sdelmont@zonageek.com>
131423a34783SAndreas Gohr     * @author  Andreas Gohr <andi@splitbrain.org>
131523a34783SAndreas Gohr     */
13160b17fdc6SAndreas Gohr    function _parseFileInfo() {
1317639f8f43SAndreas Gohr        if (file_exists($this->_fileName) && is_file($this->_fileName)) {
131855efc227SAndreas Gohr            $this->_info['file'] = array();
13198cbc5ee8SAndreas Gohr            $this->_info['file']['Name'] = utf8_decodeFN(\dokuwiki\Utf8\PhpString::basename($this->_fileName));
132000976812SAndreas Gohr            $this->_info['file']['Path'] = fullpath($this->_fileName);
132155efc227SAndreas Gohr            $this->_info['file']['Size'] = filesize($this->_fileName);
132255efc227SAndreas Gohr            if ($this->_info['file']['Size'] < 1024) {
132355efc227SAndreas Gohr                $this->_info['file']['NiceSize'] = $this->_info['file']['Size'] . 'B';
13240b17fdc6SAndreas Gohr            } elseif ($this->_info['file']['Size'] < (1024 * 1024)) {
132555efc227SAndreas Gohr                $this->_info['file']['NiceSize'] = round($this->_info['file']['Size'] / 1024) . 'KB';
13260b17fdc6SAndreas Gohr            } elseif ($this->_info['file']['Size'] < (1024 * 1024 * 1024)) {
1327fe00a666SAndreas Gohr                $this->_info['file']['NiceSize'] = round($this->_info['file']['Size'] / (1024*1024)) . 'MB';
13280b17fdc6SAndreas Gohr            } else {
132955efc227SAndreas Gohr                $this->_info['file']['NiceSize'] = $this->_info['file']['Size'] . 'B';
133055efc227SAndreas Gohr            }
133155efc227SAndreas Gohr            $this->_info['file']['UnixTime'] = filemtime($this->_fileName);
133255efc227SAndreas Gohr
133355efc227SAndreas Gohr            // get image size directly from file
1334bbdbbeb8SDamien Regad            if ($size = getimagesize($this->_fileName)) {
133555efc227SAndreas Gohr                $this->_info['file']['Width'] = $size[0];
133655efc227SAndreas Gohr                $this->_info['file']['Height'] = $size[1];
1337bbdbbeb8SDamien Regad
133855efc227SAndreas Gohr                // set mime types and formats
133959752844SAnders Sandblad                // http://php.net/manual/en/function.getimagesize.php
134059752844SAnders Sandblad                // http://php.net/manual/en/function.image-type-to-mime-type.php
134155efc227SAndreas Gohr                switch ($size[2]) {
134255efc227SAndreas Gohr                    case 1:
134355efc227SAndreas Gohr                        $this->_info['file']['Mime'] = 'image/gif';
134455efc227SAndreas Gohr                        $this->_info['file']['Format'] = 'GIF';
134555efc227SAndreas Gohr                        break;
134655efc227SAndreas Gohr                    case 2:
134755efc227SAndreas Gohr                        $this->_info['file']['Mime'] = 'image/jpeg';
134855efc227SAndreas Gohr                        $this->_info['file']['Format'] = 'JPEG';
134955efc227SAndreas Gohr                        break;
135055efc227SAndreas Gohr                    case 3:
135155efc227SAndreas Gohr                        $this->_info['file']['Mime'] = 'image/png';
135255efc227SAndreas Gohr                        $this->_info['file']['Format'] = 'PNG';
135355efc227SAndreas Gohr                        break;
135455efc227SAndreas Gohr                    case 4:
135555efc227SAndreas Gohr                        $this->_info['file']['Mime'] = 'application/x-shockwave-flash';
135655efc227SAndreas Gohr                        $this->_info['file']['Format'] = 'SWF';
135755efc227SAndreas Gohr                        break;
135855efc227SAndreas Gohr                    case 5:
135955efc227SAndreas Gohr                        $this->_info['file']['Mime'] = 'image/psd';
136055efc227SAndreas Gohr                        $this->_info['file']['Format'] = 'PSD';
136155efc227SAndreas Gohr                        break;
136255efc227SAndreas Gohr                    case 6:
136355efc227SAndreas Gohr                        $this->_info['file']['Mime'] = 'image/bmp';
136455efc227SAndreas Gohr                        $this->_info['file']['Format'] = 'BMP';
136555efc227SAndreas Gohr                        break;
136655efc227SAndreas Gohr                    case 7:
136755efc227SAndreas Gohr                        $this->_info['file']['Mime'] = 'image/tiff';
136855efc227SAndreas Gohr                        $this->_info['file']['Format'] = 'TIFF (Intel)';
136955efc227SAndreas Gohr                        break;
137055efc227SAndreas Gohr                    case 8:
137155efc227SAndreas Gohr                        $this->_info['file']['Mime'] = 'image/tiff';
137255efc227SAndreas Gohr                        $this->_info['file']['Format'] = 'TIFF (Motorola)';
137355efc227SAndreas Gohr                        break;
137455efc227SAndreas Gohr                    case 9:
137555efc227SAndreas Gohr                        $this->_info['file']['Mime'] = 'application/octet-stream';
137655efc227SAndreas Gohr                        $this->_info['file']['Format'] = 'JPC';
137755efc227SAndreas Gohr                        break;
137855efc227SAndreas Gohr                    case 10:
137955efc227SAndreas Gohr                        $this->_info['file']['Mime'] = 'image/jp2';
138055efc227SAndreas Gohr                        $this->_info['file']['Format'] = 'JP2';
138155efc227SAndreas Gohr                        break;
138255efc227SAndreas Gohr                    case 11:
138355efc227SAndreas Gohr                        $this->_info['file']['Mime'] = 'application/octet-stream';
138455efc227SAndreas Gohr                        $this->_info['file']['Format'] = 'JPX';
138555efc227SAndreas Gohr                        break;
138655efc227SAndreas Gohr                    case 12:
138755efc227SAndreas Gohr                        $this->_info['file']['Mime'] = 'application/octet-stream';
138855efc227SAndreas Gohr                        $this->_info['file']['Format'] = 'JB2';
138955efc227SAndreas Gohr                        break;
139055efc227SAndreas Gohr                    case 13:
139155efc227SAndreas Gohr                        $this->_info['file']['Mime'] = 'application/x-shockwave-flash';
139255efc227SAndreas Gohr                        $this->_info['file']['Format'] = 'SWC';
139355efc227SAndreas Gohr                        break;
139455efc227SAndreas Gohr                    case 14:
139555efc227SAndreas Gohr                        $this->_info['file']['Mime'] = 'image/iff';
139655efc227SAndreas Gohr                        $this->_info['file']['Format'] = 'IFF';
139755efc227SAndreas Gohr                        break;
139855efc227SAndreas Gohr                    case 15:
139955efc227SAndreas Gohr                        $this->_info['file']['Mime'] = 'image/vnd.wap.wbmp';
140055efc227SAndreas Gohr                        $this->_info['file']['Format'] = 'WBMP';
140155efc227SAndreas Gohr                        break;
140255efc227SAndreas Gohr                    case 16:
140355efc227SAndreas Gohr                        $this->_info['file']['Mime'] = 'image/xbm';
140455efc227SAndreas Gohr                        $this->_info['file']['Format'] = 'XBM';
140555efc227SAndreas Gohr                        break;
140655efc227SAndreas Gohr                    default:
140755efc227SAndreas Gohr                        $this->_info['file']['Mime'] = 'image/unknown';
140855efc227SAndreas Gohr                }
1409bbdbbeb8SDamien Regad            }
14100b17fdc6SAndreas Gohr        } else {
141155efc227SAndreas Gohr            $this->_info['file'] = array();
14128cbc5ee8SAndreas Gohr            $this->_info['file']['Name'] = \dokuwiki\Utf8\PhpString::basename($this->_fileName);
141355efc227SAndreas Gohr            $this->_info['file']['Url'] = $this->_fileName;
141455efc227SAndreas Gohr        }
141555efc227SAndreas Gohr
141655efc227SAndreas Gohr        return true;
141755efc227SAndreas Gohr    }
141855efc227SAndreas Gohr
141955efc227SAndreas Gohr    /*************************************************************/
14200b17fdc6SAndreas Gohr    function _parseMarkerJFIF() {
142155efc227SAndreas Gohr        if (!isset($this->_markers)) {
142255efc227SAndreas Gohr            $this->_readJPEG();
142355efc227SAndreas Gohr        }
142455efc227SAndreas Gohr
1425c9c56f8aSasivery        if ($this->_markers == null || $this->_isMarkerDisabled(('jfif'))) {
142655efc227SAndreas Gohr            return false;
142755efc227SAndreas Gohr        }
142855efc227SAndreas Gohr
1429c9c56f8aSasivery        try {
143055efc227SAndreas Gohr            $data = null;
143155efc227SAndreas Gohr            $count = count($this->_markers);
143255efc227SAndreas Gohr            for ($i = 0; $i < $count; $i++) {
1433*45717242SAndreas Gohr                if ($this->_markers[$i]['marker'] == 0xE0
1434*45717242SAndreas Gohr                    && strlen($this->_markers[$i]['data']) >= 4) {
143555efc227SAndreas Gohr                    $signature = $this->_getFixedString($this->_markers[$i]['data'], 0, 4);
143655efc227SAndreas Gohr                    if ($signature == 'JFIF') {
143755efc227SAndreas Gohr                        $data =& $this->_markers[$i]['data'];
143855efc227SAndreas Gohr                        break;
143955efc227SAndreas Gohr                    }
144055efc227SAndreas Gohr                }
144155efc227SAndreas Gohr            }
144255efc227SAndreas Gohr
144355efc227SAndreas Gohr            if ($data == null) {
144455efc227SAndreas Gohr                $this->_info['jfif'] = false;
144555efc227SAndreas Gohr                return false;
144655efc227SAndreas Gohr            }
144755efc227SAndreas Gohr
144855efc227SAndreas Gohr            $this->_info['jfif'] = array();
144955efc227SAndreas Gohr
145055efc227SAndreas Gohr            $vmaj = $this->_getByte($data, 5);
145155efc227SAndreas Gohr            $vmin = $this->_getByte($data, 6);
145255efc227SAndreas Gohr
145355efc227SAndreas Gohr            $this->_info['jfif']['Version'] = sprintf('%d.%02d', $vmaj, $vmin);
145455efc227SAndreas Gohr
145555efc227SAndreas Gohr            $units = $this->_getByte($data, 7);
145655efc227SAndreas Gohr            switch ($units) {
145755efc227SAndreas Gohr                case 0:
145855efc227SAndreas Gohr                    $this->_info['jfif']['Units'] = 'pixels';
145955efc227SAndreas Gohr                    break;
146055efc227SAndreas Gohr                case 1:
146155efc227SAndreas Gohr                    $this->_info['jfif']['Units'] = 'dpi';
146255efc227SAndreas Gohr                    break;
146355efc227SAndreas Gohr                case 2:
146455efc227SAndreas Gohr                    $this->_info['jfif']['Units'] = 'dpcm';
146555efc227SAndreas Gohr                    break;
146655efc227SAndreas Gohr                default:
146755efc227SAndreas Gohr                    $this->_info['jfif']['Units'] = 'unknown';
146855efc227SAndreas Gohr                    break;
146955efc227SAndreas Gohr            }
147055efc227SAndreas Gohr
147155efc227SAndreas Gohr            $xdens = $this->_getShort($data, 8);
147255efc227SAndreas Gohr            $ydens = $this->_getShort($data, 10);
147355efc227SAndreas Gohr
147455efc227SAndreas Gohr            $this->_info['jfif']['XDensity'] = $xdens;
147555efc227SAndreas Gohr            $this->_info['jfif']['YDensity'] = $ydens;
147655efc227SAndreas Gohr
147755efc227SAndreas Gohr            $thumbx = $this->_getByte($data, 12);
147855efc227SAndreas Gohr            $thumby = $this->_getByte($data, 13);
147955efc227SAndreas Gohr
148055efc227SAndreas Gohr            $this->_info['jfif']['ThumbnailWidth'] = $thumbx;
148155efc227SAndreas Gohr            $this->_info['jfif']['ThumbnailHeight'] = $thumby;
1482c9c56f8aSasivery        } catch(Exception $e) {
1483c9c56f8aSasivery            $this->_handleMarkerParsingException($e);
1484c9c56f8aSasivery            $this->_info['jfif'] = false;
1485c9c56f8aSasivery            return false;
1486c9c56f8aSasivery        }
148755efc227SAndreas Gohr
148855efc227SAndreas Gohr        return true;
148955efc227SAndreas Gohr    }
149055efc227SAndreas Gohr
149155efc227SAndreas Gohr    /*************************************************************/
14920b17fdc6SAndreas Gohr    function _parseMarkerSOF() {
149355efc227SAndreas Gohr        if (!isset($this->_markers)) {
149455efc227SAndreas Gohr            $this->_readJPEG();
149555efc227SAndreas Gohr        }
149655efc227SAndreas Gohr
1497c9c56f8aSasivery        if ($this->_markers == null || $this->_isMarkerDisabled(('sof'))) {
149855efc227SAndreas Gohr            return false;
149955efc227SAndreas Gohr        }
150055efc227SAndreas Gohr
1501c9c56f8aSasivery        try {
150255efc227SAndreas Gohr            $data = null;
150355efc227SAndreas Gohr            $count = count($this->_markers);
150455efc227SAndreas Gohr            for ($i = 0; $i < $count; $i++) {
150555efc227SAndreas Gohr                switch ($this->_markers[$i]['marker']) {
150655efc227SAndreas Gohr                    case 0xC0: // SOF0
150755efc227SAndreas Gohr                    case 0xC1: // SOF1
150855efc227SAndreas Gohr                    case 0xC2: // SOF2
150955efc227SAndreas Gohr                    case 0xC9: // SOF9
151055efc227SAndreas Gohr                        $data =& $this->_markers[$i]['data'];
151155efc227SAndreas Gohr                        $marker = $this->_markers[$i]['marker'];
151255efc227SAndreas Gohr                        break;
151355efc227SAndreas Gohr                }
151455efc227SAndreas Gohr            }
151555efc227SAndreas Gohr
151655efc227SAndreas Gohr            if ($data == null) {
151755efc227SAndreas Gohr                $this->_info['sof'] = false;
151855efc227SAndreas Gohr                return false;
151955efc227SAndreas Gohr            }
152055efc227SAndreas Gohr
152155efc227SAndreas Gohr            $pos = 0;
152255efc227SAndreas Gohr            $this->_info['sof'] = array();
152355efc227SAndreas Gohr
152455efc227SAndreas Gohr            switch ($marker) {
152555efc227SAndreas Gohr                case 0xC0: // SOF0
152655efc227SAndreas Gohr                    $format = 'Baseline';
152755efc227SAndreas Gohr                    break;
152855efc227SAndreas Gohr                case 0xC1: // SOF1
152955efc227SAndreas Gohr                    $format = 'Progessive';
153055efc227SAndreas Gohr                    break;
153155efc227SAndreas Gohr                case 0xC2: // SOF2
153255efc227SAndreas Gohr                    $format = 'Non-baseline';
153355efc227SAndreas Gohr                    break;
153455efc227SAndreas Gohr                case 0xC9: // SOF9
153555efc227SAndreas Gohr                    $format = 'Arithmetic';
153655efc227SAndreas Gohr                    break;
153755efc227SAndreas Gohr                default:
153855efc227SAndreas Gohr                    return false;
153955efc227SAndreas Gohr            }
154055efc227SAndreas Gohr
154155efc227SAndreas Gohr            $this->_info['sof']['Format']          = $format;
154255efc227SAndreas Gohr            $this->_info['sof']['SamplePrecision'] = $this->_getByte($data, $pos + 0);
154355efc227SAndreas Gohr            $this->_info['sof']['ImageHeight']     = $this->_getShort($data, $pos + 1);
154455efc227SAndreas Gohr            $this->_info['sof']['ImageWidth']      = $this->_getShort($data, $pos + 3);
154555efc227SAndreas Gohr            $this->_info['sof']['ColorChannels']   = $this->_getByte($data, $pos + 5);
1546c9c56f8aSasivery        } catch(Exception $e) {
1547c9c56f8aSasivery            $this->_handleMarkerParsingException($e);
1548c9c56f8aSasivery            $this->_info['sof'] = false;
1549c9c56f8aSasivery            return false;
1550c9c56f8aSasivery        }
155155efc227SAndreas Gohr
155255efc227SAndreas Gohr        return true;
155355efc227SAndreas Gohr    }
155455efc227SAndreas Gohr
1555431c7fc8Shakan.sandell    /**
1556431c7fc8Shakan.sandell     * Parses the XMP data
1557431c7fc8Shakan.sandell     *
1558431c7fc8Shakan.sandell     * @author  Hakan Sandell <hakan.sandell@mydata.se>
1559431c7fc8Shakan.sandell     */
15600b17fdc6SAndreas Gohr    function _parseMarkerXmp() {
1561431c7fc8Shakan.sandell        if (!isset($this->_markers)) {
1562431c7fc8Shakan.sandell            $this->_readJPEG();
1563431c7fc8Shakan.sandell        }
1564431c7fc8Shakan.sandell
1565c9c56f8aSasivery        if ($this->_markers == null || $this->_isMarkerDisabled(('xmp'))) {
1566431c7fc8Shakan.sandell            return false;
1567431c7fc8Shakan.sandell        }
1568431c7fc8Shakan.sandell
1569c9c56f8aSasivery        try {
1570431c7fc8Shakan.sandell            $data = null;
1571431c7fc8Shakan.sandell            $count = count($this->_markers);
1572431c7fc8Shakan.sandell            for ($i = 0; $i < $count; $i++) {
1573*45717242SAndreas Gohr                if ($this->_markers[$i]['marker'] == 0xE1
1574*45717242SAndreas Gohr                    && strlen($this->_markers[$i]['data']) >= 29) {
1575431c7fc8Shakan.sandell                    $signature = $this->_getFixedString($this->_markers[$i]['data'], 0, 29);
1576431c7fc8Shakan.sandell                    if ($signature == "http://ns.adobe.com/xap/1.0/\0") {
1577f5891fa4SGerrit Uitslag                        $data = substr($this->_markers[$i]['data'], 29);
1578431c7fc8Shakan.sandell                        break;
1579431c7fc8Shakan.sandell                    }
1580431c7fc8Shakan.sandell                }
1581431c7fc8Shakan.sandell            }
1582431c7fc8Shakan.sandell
1583431c7fc8Shakan.sandell            if ($data == null) {
1584431c7fc8Shakan.sandell                $this->_info['xmp'] = false;
1585431c7fc8Shakan.sandell                return false;
1586431c7fc8Shakan.sandell            }
1587431c7fc8Shakan.sandell
1588431c7fc8Shakan.sandell            $parser = xml_parser_create();
1589431c7fc8Shakan.sandell            xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
1590431c7fc8Shakan.sandell            xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
1591d8443bf1SHakan Sandell            $result = xml_parse_into_struct($parser, $data, $values, $tags);
1592431c7fc8Shakan.sandell            xml_parser_free($parser);
1593431c7fc8Shakan.sandell
1594d8443bf1SHakan Sandell            if ($result == 0) {
1595d8443bf1SHakan Sandell                $this->_info['xmp'] = false;
1596d8443bf1SHakan Sandell                return false;
1597d8443bf1SHakan Sandell            }
1598d8443bf1SHakan Sandell
1599431c7fc8Shakan.sandell            $this->_info['xmp'] = array();
1600431c7fc8Shakan.sandell            $count = count($values);
1601431c7fc8Shakan.sandell            for ($i = 0; $i < $count; $i++) {
16020b17fdc6SAndreas Gohr                if ($values[$i]['tag'] == 'rdf:Description' && $values[$i]['type'] == 'open') {
1603431c7fc8Shakan.sandell
1604d8443bf1SHakan Sandell                    while ((++$i < $count) && ($values[$i]['tag'] != 'rdf:Description')) {
1605d8443bf1SHakan Sandell                        $this->_parseXmpNode($values, $i, $this->_info['xmp'][$values[$i]['tag']], $count);
1606431c7fc8Shakan.sandell                    }
1607431c7fc8Shakan.sandell                }
1608431c7fc8Shakan.sandell            }
1609c9c56f8aSasivery        } catch (Exception $e) {
1610c9c56f8aSasivery            $this->_handleMarkerParsingException($e);
1611c9c56f8aSasivery            $this->_info['xmp'] = false;
1612c9c56f8aSasivery            return false;
1613c9c56f8aSasivery        }
1614c9c56f8aSasivery
1615431c7fc8Shakan.sandell        return true;
1616431c7fc8Shakan.sandell    }
1617431c7fc8Shakan.sandell
1618431c7fc8Shakan.sandell    /**
1619431c7fc8Shakan.sandell     * Parses XMP nodes by recursion
1620431c7fc8Shakan.sandell     *
1621431c7fc8Shakan.sandell     * @author  Hakan Sandell <hakan.sandell@mydata.se>
1622f50a239bSTakamura     *
1623f50a239bSTakamura     * @param array $values
1624f50a239bSTakamura     * @param int $i
1625f50a239bSTakamura     * @param mixed $meta
1626276820f7SScrutinizer Auto-Fixer     * @param integer $count
1627431c7fc8Shakan.sandell     */
1628d8443bf1SHakan Sandell    function _parseXmpNode($values, &$i, &$meta, $count) {
1629831c10d0SHakan Sandell        if ($values[$i]['type'] == 'close') return;
1630831c10d0SHakan Sandell
16310b17fdc6SAndreas Gohr        if ($values[$i]['type'] == 'complete') {
1632431c7fc8Shakan.sandell            // Simple Type property
1633e3b89425Sasivery            $meta = $values[$i]['value'] ?? '';
1634431c7fc8Shakan.sandell            return;
1635431c7fc8Shakan.sandell        }
1636431c7fc8Shakan.sandell
1637431c7fc8Shakan.sandell        $i++;
1638d8443bf1SHakan Sandell        if ($i >= $count) return;
1639d8443bf1SHakan Sandell
16400b17fdc6SAndreas Gohr        if ($values[$i]['tag'] == 'rdf:Bag' || $values[$i]['tag'] == 'rdf:Seq') {
1641431c7fc8Shakan.sandell            // Array property
1642431c7fc8Shakan.sandell            $meta = array();
16430b17fdc6SAndreas Gohr            while ($values[++$i]['tag'] == 'rdf:li') {
1644d8443bf1SHakan Sandell                $this->_parseXmpNode($values, $i, $meta[], $count);
1645431c7fc8Shakan.sandell            }
1646831c10d0SHakan Sandell            $i++; // skip closing Bag/Seq tag
1647431c7fc8Shakan.sandell
16480b17fdc6SAndreas Gohr        } elseif ($values[$i]['tag'] == 'rdf:Alt') {
1649431c7fc8Shakan.sandell            // Language Alternative property, only the first (default) value is used
1650831c10d0SHakan Sandell            if ($values[$i]['type'] == 'open') {
1651431c7fc8Shakan.sandell                $i++;
1652d8443bf1SHakan Sandell                $this->_parseXmpNode($values, $i, $meta, $count);
1653d8443bf1SHakan Sandell                while ((++$i < $count) && ($values[$i]['tag'] != 'rdf:Alt'));
1654831c10d0SHakan Sandell                $i++; // skip closing Alt tag
1655831c10d0SHakan Sandell            }
1656431c7fc8Shakan.sandell
1657431c7fc8Shakan.sandell        } else {
1658431c7fc8Shakan.sandell            // Structure property
1659431c7fc8Shakan.sandell            $meta = array();
16600b17fdc6SAndreas Gohr            $startTag = $values[$i-1]['tag'];
1661431c7fc8Shakan.sandell            do {
1662d8443bf1SHakan Sandell                $this->_parseXmpNode($values, $i, $meta[$values[$i]['tag']], $count);
1663d8443bf1SHakan Sandell            } while ((++$i < $count) && ($values[$i]['tag'] != $startTag));
1664431c7fc8Shakan.sandell        }
1665431c7fc8Shakan.sandell    }
1666431c7fc8Shakan.sandell
166755efc227SAndreas Gohr    /*************************************************************/
16680b17fdc6SAndreas Gohr    function _parseMarkerExif() {
166955efc227SAndreas Gohr        if (!isset($this->_markers)) {
167055efc227SAndreas Gohr            $this->_readJPEG();
167155efc227SAndreas Gohr        }
167255efc227SAndreas Gohr
1673c9c56f8aSasivery        if ($this->_markers == null || $this->_isMarkerDisabled(('exif'))) {
167455efc227SAndreas Gohr            return false;
167555efc227SAndreas Gohr        }
167655efc227SAndreas Gohr
1677c9c56f8aSasivery        try {
167855efc227SAndreas Gohr            $data = null;
167955efc227SAndreas Gohr            $count = count($this->_markers);
168055efc227SAndreas Gohr            for ($i = 0; $i < $count; $i++) {
1681*45717242SAndreas Gohr                if ($this->_markers[$i]['marker'] == 0xE1
1682*45717242SAndreas Gohr                    && strlen($this->_markers[$i]['data']) >= 6) {
168355efc227SAndreas Gohr                    $signature = $this->_getFixedString($this->_markers[$i]['data'], 0, 6);
168455efc227SAndreas Gohr                    if ($signature == "Exif\0\0") {
168555efc227SAndreas Gohr                        $data =& $this->_markers[$i]['data'];
168655efc227SAndreas Gohr                        break;
168755efc227SAndreas Gohr                    }
168855efc227SAndreas Gohr                }
168955efc227SAndreas Gohr            }
169055efc227SAndreas Gohr
169155efc227SAndreas Gohr            if ($data == null) {
169255efc227SAndreas Gohr                $this->_info['exif'] = false;
169355efc227SAndreas Gohr                return false;
169455efc227SAndreas Gohr            }
169555efc227SAndreas Gohr            $pos = 6;
169655efc227SAndreas Gohr            $this->_info['exif'] = array();
169755efc227SAndreas Gohr
169855efc227SAndreas Gohr            // We don't increment $pos after this because Exif uses offsets relative to this point
169955efc227SAndreas Gohr
170055efc227SAndreas Gohr            $byteAlign = $this->_getShort($data, $pos + 0);
170155efc227SAndreas Gohr
170255efc227SAndreas Gohr            if ($byteAlign == 0x4949) { // "II"
170355efc227SAndreas Gohr                $isBigEndian = false;
17040b17fdc6SAndreas Gohr            } elseif ($byteAlign == 0x4D4D) { // "MM"
170555efc227SAndreas Gohr                $isBigEndian = true;
17060b17fdc6SAndreas Gohr            } else {
170755efc227SAndreas Gohr                return false; // Unexpected data
170855efc227SAndreas Gohr            }
170955efc227SAndreas Gohr
171055efc227SAndreas Gohr            $alignCheck = $this->_getShort($data, $pos + 2, $isBigEndian);
171155efc227SAndreas Gohr            if ($alignCheck != 0x002A) // That's the expected value
171255efc227SAndreas Gohr                return false; // Unexpected data
171355efc227SAndreas Gohr
171455efc227SAndreas Gohr            if ($isBigEndian) {
171555efc227SAndreas Gohr                $this->_info['exif']['ByteAlign'] = "Big Endian";
17160b17fdc6SAndreas Gohr            } else {
171755efc227SAndreas Gohr                $this->_info['exif']['ByteAlign'] = "Little Endian";
171855efc227SAndreas Gohr            }
171955efc227SAndreas Gohr
172055efc227SAndreas Gohr            $offsetIFD0 = $this->_getLong($data, $pos + 4, $isBigEndian);
172155efc227SAndreas Gohr            if ($offsetIFD0 < 8)
172255efc227SAndreas Gohr                return false; // Unexpected data
172355efc227SAndreas Gohr
172455efc227SAndreas Gohr            $offsetIFD1 = $this->_readIFD($data, $pos, $offsetIFD0, $isBigEndian, 'ifd0');
172555efc227SAndreas Gohr            if ($offsetIFD1 != 0)
172655efc227SAndreas Gohr                $this->_readIFD($data, $pos, $offsetIFD1, $isBigEndian, 'ifd1');
1727c9c56f8aSasivery        } catch(Exception $e) {
1728c9c56f8aSasivery            $this->_handleMarkerParsingException($e);
1729c9c56f8aSasivery            $this->_info['exif'] = false;
1730c9c56f8aSasivery            return false;
1731c9c56f8aSasivery        }
173255efc227SAndreas Gohr
173355efc227SAndreas Gohr        return true;
173455efc227SAndreas Gohr    }
173555efc227SAndreas Gohr
173655efc227SAndreas Gohr    /*************************************************************/
1737276820f7SScrutinizer Auto-Fixer
1738276820f7SScrutinizer Auto-Fixer    /**
1739f50a239bSTakamura     * @param mixed $data
1740276820f7SScrutinizer Auto-Fixer     * @param integer $base
1741f50a239bSTakamura     * @param integer $offset
1742276820f7SScrutinizer Auto-Fixer     * @param boolean $isBigEndian
1743276820f7SScrutinizer Auto-Fixer     * @param string $mode
1744f50a239bSTakamura     *
1745f50a239bSTakamura     * @return int
1746276820f7SScrutinizer Auto-Fixer     */
17470b17fdc6SAndreas Gohr    function _readIFD($data, $base, $offset, $isBigEndian, $mode) {
174855efc227SAndreas Gohr        $EXIFTags = $this->_exifTagNames($mode);
174955efc227SAndreas Gohr
175055efc227SAndreas Gohr        $numEntries = $this->_getShort($data, $base + $offset, $isBigEndian);
175155efc227SAndreas Gohr        $offset += 2;
175255efc227SAndreas Gohr
175355efc227SAndreas Gohr        $exifTIFFOffset = 0;
175455efc227SAndreas Gohr        $exifTIFFLength = 0;
175555efc227SAndreas Gohr        $exifThumbnailOffset = 0;
175655efc227SAndreas Gohr        $exifThumbnailLength = 0;
175755efc227SAndreas Gohr
175855efc227SAndreas Gohr        for ($i = 0; $i < $numEntries; $i++) {
175955efc227SAndreas Gohr            $tag = $this->_getShort($data, $base + $offset, $isBigEndian);
176055efc227SAndreas Gohr            $offset += 2;
176155efc227SAndreas Gohr            $type = $this->_getShort($data, $base + $offset, $isBigEndian);
176255efc227SAndreas Gohr            $offset += 2;
176355efc227SAndreas Gohr            $count = $this->_getLong($data, $base + $offset, $isBigEndian);
176455efc227SAndreas Gohr            $offset += 4;
176555efc227SAndreas Gohr
176655efc227SAndreas Gohr            if (($type < 1) || ($type > 12))
176755efc227SAndreas Gohr                return false; // Unexpected Type
176855efc227SAndreas Gohr
176955efc227SAndreas Gohr            $typeLengths = array( -1, 1, 1, 2, 4, 8, 1, 1, 2, 4, 8, 4, 8 );
177055efc227SAndreas Gohr
177155efc227SAndreas Gohr            $dataLength = $typeLengths[$type] * $count;
177255efc227SAndreas Gohr            if ($dataLength > 4) {
177355efc227SAndreas Gohr                $dataOffset = $this->_getLong($data, $base + $offset, $isBigEndian);
177455efc227SAndreas Gohr                $rawValue = $this->_getFixedString($data, $base + $dataOffset, $dataLength);
17750b17fdc6SAndreas Gohr            } else {
177655efc227SAndreas Gohr                $rawValue = $this->_getFixedString($data, $base + $offset, $dataLength);
177755efc227SAndreas Gohr            }
177855efc227SAndreas Gohr            $offset += 4;
177955efc227SAndreas Gohr
178055efc227SAndreas Gohr            switch ($type) {
178155efc227SAndreas Gohr                case 1:    // UBYTE
178255efc227SAndreas Gohr                    if ($count == 1) {
178355efc227SAndreas Gohr                        $value = $this->_getByte($rawValue, 0);
17840b17fdc6SAndreas Gohr                    } else {
178555efc227SAndreas Gohr                        $value = array();
178655efc227SAndreas Gohr                        for ($j = 0; $j < $count; $j++)
178755efc227SAndreas Gohr                            $value[$j] = $this->_getByte($rawValue, $j);
178855efc227SAndreas Gohr                    }
178955efc227SAndreas Gohr                    break;
179055efc227SAndreas Gohr                case 2:    // ASCII
179155efc227SAndreas Gohr                    $value = $rawValue;
179255efc227SAndreas Gohr                    break;
179355efc227SAndreas Gohr                case 3:    // USHORT
179455efc227SAndreas Gohr                    if ($count == 1) {
179555efc227SAndreas Gohr                        $value = $this->_getShort($rawValue, 0, $isBigEndian);
17960b17fdc6SAndreas Gohr                    } else {
179755efc227SAndreas Gohr                        $value = array();
179855efc227SAndreas Gohr                        for ($j = 0; $j < $count; $j++)
179955efc227SAndreas Gohr                            $value[$j] = $this->_getShort($rawValue, $j * 2, $isBigEndian);
180055efc227SAndreas Gohr                    }
180155efc227SAndreas Gohr                    break;
180255efc227SAndreas Gohr                case 4:    // ULONG
180355efc227SAndreas Gohr                    if ($count == 1) {
180455efc227SAndreas Gohr                        $value = $this->_getLong($rawValue, 0, $isBigEndian);
18050b17fdc6SAndreas Gohr                    } else {
180655efc227SAndreas Gohr                        $value = array();
180755efc227SAndreas Gohr                        for ($j = 0; $j < $count; $j++)
180855efc227SAndreas Gohr                            $value[$j] = $this->_getLong($rawValue, $j * 4, $isBigEndian);
180955efc227SAndreas Gohr                    }
181055efc227SAndreas Gohr                    break;
181155efc227SAndreas Gohr                case 5:    // URATIONAL
181255efc227SAndreas Gohr                    if ($count == 1) {
181355efc227SAndreas Gohr                        $a = $this->_getLong($rawValue, 0, $isBigEndian);
181455efc227SAndreas Gohr                        $b = $this->_getLong($rawValue, 4, $isBigEndian);
181555efc227SAndreas Gohr                        $value = array();
181655efc227SAndreas Gohr                        $value['val'] = 0;
181755efc227SAndreas Gohr                        $value['num'] = $a;
181855efc227SAndreas Gohr                        $value['den'] = $b;
181955efc227SAndreas Gohr                        if (($a != 0) && ($b != 0)) {
182055efc227SAndreas Gohr                            $value['val'] = $a / $b;
182155efc227SAndreas Gohr                        }
18220b17fdc6SAndreas Gohr                    } else {
182355efc227SAndreas Gohr                        $value = array();
182455efc227SAndreas Gohr                        for ($j = 0; $j < $count; $j++) {
182555efc227SAndreas Gohr                            $a = $this->_getLong($rawValue, $j * 8, $isBigEndian);
182655efc227SAndreas Gohr                            $b = $this->_getLong($rawValue, ($j * 8) + 4, $isBigEndian);
182755efc227SAndreas Gohr                            $value = array();
182855efc227SAndreas Gohr                            $value[$j]['val'] = 0;
182955efc227SAndreas Gohr                            $value[$j]['num'] = $a;
183055efc227SAndreas Gohr                            $value[$j]['den'] = $b;
183155efc227SAndreas Gohr                            if (($a != 0) && ($b != 0))
183255efc227SAndreas Gohr                                $value[$j]['val'] = $a / $b;
183355efc227SAndreas Gohr                        }
183455efc227SAndreas Gohr                    }
183555efc227SAndreas Gohr                    break;
183655efc227SAndreas Gohr                case 6:    // SBYTE
183755efc227SAndreas Gohr                    if ($count == 1) {
183855efc227SAndreas Gohr                        $value = $this->_getByte($rawValue, 0);
18390b17fdc6SAndreas Gohr                    } else {
184055efc227SAndreas Gohr                        $value = array();
184155efc227SAndreas Gohr                        for ($j = 0; $j < $count; $j++)
184255efc227SAndreas Gohr                            $value[$j] = $this->_getByte($rawValue, $j);
184355efc227SAndreas Gohr                    }
184455efc227SAndreas Gohr                    break;
184555efc227SAndreas Gohr                case 7:    // UNDEFINED
184655efc227SAndreas Gohr                    $value = $rawValue;
184755efc227SAndreas Gohr                    break;
184855efc227SAndreas Gohr                case 8:    // SSHORT
184955efc227SAndreas Gohr                    if ($count == 1) {
185055efc227SAndreas Gohr                        $value = $this->_getShort($rawValue, 0, $isBigEndian);
18510b17fdc6SAndreas Gohr                    } else {
185255efc227SAndreas Gohr                        $value = array();
185355efc227SAndreas Gohr                        for ($j = 0; $j < $count; $j++)
185455efc227SAndreas Gohr                            $value[$j] = $this->_getShort($rawValue, $j * 2, $isBigEndian);
185555efc227SAndreas Gohr                    }
185655efc227SAndreas Gohr                    break;
185755efc227SAndreas Gohr                case 9:    // SLONG
185855efc227SAndreas Gohr                    if ($count == 1) {
185955efc227SAndreas Gohr                        $value = $this->_getLong($rawValue, 0, $isBigEndian);
18600b17fdc6SAndreas Gohr                    } else {
186155efc227SAndreas Gohr                        $value = array();
186255efc227SAndreas Gohr                        for ($j = 0; $j < $count; $j++)
186355efc227SAndreas Gohr                            $value[$j] = $this->_getLong($rawValue, $j * 4, $isBigEndian);
186455efc227SAndreas Gohr                    }
186555efc227SAndreas Gohr                    break;
186655efc227SAndreas Gohr                case 10:   // SRATIONAL
186755efc227SAndreas Gohr                    if ($count == 1) {
186855efc227SAndreas Gohr                        $a = $this->_getLong($rawValue, 0, $isBigEndian);
186955efc227SAndreas Gohr                        $b = $this->_getLong($rawValue, 4, $isBigEndian);
187055efc227SAndreas Gohr                        $value = array();
187155efc227SAndreas Gohr                        $value['val'] = 0;
187255efc227SAndreas Gohr                        $value['num'] = $a;
187355efc227SAndreas Gohr                        $value['den'] = $b;
187455efc227SAndreas Gohr                        if (($a != 0) && ($b != 0))
187555efc227SAndreas Gohr                            $value['val'] = $a / $b;
18760b17fdc6SAndreas Gohr                    } else {
187755efc227SAndreas Gohr                        $value = array();
187855efc227SAndreas Gohr                        for ($j = 0; $j < $count; $j++) {
187955efc227SAndreas Gohr                            $a = $this->_getLong($rawValue, $j * 8, $isBigEndian);
188055efc227SAndreas Gohr                            $b = $this->_getLong($rawValue, ($j * 8) + 4, $isBigEndian);
188155efc227SAndreas Gohr                            $value = array();
188255efc227SAndreas Gohr                            $value[$j]['val'] = 0;
188355efc227SAndreas Gohr                            $value[$j]['num'] = $a;
188455efc227SAndreas Gohr                            $value[$j]['den'] = $b;
188555efc227SAndreas Gohr                            if (($a != 0) && ($b != 0))
188655efc227SAndreas Gohr                                $value[$j]['val'] = $a / $b;
188755efc227SAndreas Gohr                        }
188855efc227SAndreas Gohr                    }
188955efc227SAndreas Gohr                    break;
189055efc227SAndreas Gohr                case 11:   // FLOAT
189155efc227SAndreas Gohr                    $value = $rawValue;
189255efc227SAndreas Gohr                    break;
189355efc227SAndreas Gohr
189455efc227SAndreas Gohr                case 12:   // DFLOAT
189555efc227SAndreas Gohr                    $value = $rawValue;
189655efc227SAndreas Gohr                    break;
189755efc227SAndreas Gohr                default:
189855efc227SAndreas Gohr                    return false; // Unexpected Type
189955efc227SAndreas Gohr            }
190055efc227SAndreas Gohr
190155efc227SAndreas Gohr            $tagName = '';
190255efc227SAndreas Gohr            if (($mode == 'ifd0') && ($tag == 0x8769)) {  // ExifIFDOffset
190355efc227SAndreas Gohr                $this->_readIFD($data, $base, $value, $isBigEndian, 'exif');
19040b17fdc6SAndreas Gohr            } elseif (($mode == 'ifd0') && ($tag == 0x8825)) {  // GPSIFDOffset
190555efc227SAndreas Gohr                $this->_readIFD($data, $base, $value, $isBigEndian, 'gps');
19060b17fdc6SAndreas Gohr            } elseif (($mode == 'ifd1') && ($tag == 0x0111)) {  // TIFFStripOffsets
190755efc227SAndreas Gohr                $exifTIFFOffset = $value;
19080b17fdc6SAndreas Gohr            } elseif (($mode == 'ifd1') && ($tag == 0x0117)) {  // TIFFStripByteCounts
190955efc227SAndreas Gohr                $exifTIFFLength = $value;
19100b17fdc6SAndreas Gohr            } elseif (($mode == 'ifd1') && ($tag == 0x0201)) {  // TIFFJFIFOffset
191155efc227SAndreas Gohr                $exifThumbnailOffset = $value;
19120b17fdc6SAndreas Gohr            } elseif (($mode == 'ifd1') && ($tag == 0x0202)) {  // TIFFJFIFLength
191355efc227SAndreas Gohr                $exifThumbnailLength = $value;
19140b17fdc6SAndreas Gohr            } elseif (($mode == 'exif') && ($tag == 0xA005)) {  // InteropIFDOffset
191555efc227SAndreas Gohr                $this->_readIFD($data, $base, $value, $isBigEndian, 'interop');
191655efc227SAndreas Gohr            }
191755efc227SAndreas Gohr            // elseif (($mode == 'exif') && ($tag == 0x927C)) {  // MakerNote
191855efc227SAndreas Gohr            // }
191955efc227SAndreas Gohr            else {
192055efc227SAndreas Gohr                if (isset($EXIFTags[$tag])) {
192155efc227SAndreas Gohr                    $tagName = $EXIFTags[$tag];
192255efc227SAndreas Gohr                    if (isset($this->_info['exif'][$tagName])) {
192355efc227SAndreas Gohr                        if (!is_array($this->_info['exif'][$tagName])) {
192455efc227SAndreas Gohr                            $aux = array();
192555efc227SAndreas Gohr                            $aux[0] = $this->_info['exif'][$tagName];
192655efc227SAndreas Gohr                            $this->_info['exif'][$tagName] = $aux;
192755efc227SAndreas Gohr                        }
192855efc227SAndreas Gohr
192955efc227SAndreas Gohr                        $this->_info['exif'][$tagName][count($this->_info['exif'][$tagName])] = $value;
19300b17fdc6SAndreas Gohr                    } else {
193155efc227SAndreas Gohr                        $this->_info['exif'][$tagName] = $value;
193255efc227SAndreas Gohr                    }
193355efc227SAndreas Gohr                }
19340b17fdc6SAndreas Gohr                /*
193555efc227SAndreas Gohr                 else {
19360b17fdc6SAndreas Gohr                    echo sprintf("<h1>Unknown tag %02x (t: %d l: %d) %s in %s</h1>", $tag, $type, $count, $mode, $this->_fileName);
193755efc227SAndreas Gohr                    // Unknown Tags will be ignored!!!
193855efc227SAndreas Gohr                    // That's because the tag might be a pointer (like the Exif tag)
193955efc227SAndreas Gohr                    // and saving it without saving the data it points to might
194055efc227SAndreas Gohr                    // create an invalid file.
194155efc227SAndreas Gohr                }
19420b17fdc6SAndreas Gohr                */
194355efc227SAndreas Gohr            }
194455efc227SAndreas Gohr        }
194555efc227SAndreas Gohr
194655efc227SAndreas Gohr        if (($exifThumbnailOffset > 0) && ($exifThumbnailLength > 0)) {
194755efc227SAndreas Gohr            $this->_info['exif']['JFIFThumbnail'] = $this->_getFixedString($data, $base + $exifThumbnailOffset, $exifThumbnailLength);
194855efc227SAndreas Gohr        }
194955efc227SAndreas Gohr
195055efc227SAndreas Gohr        if (($exifTIFFOffset > 0) && ($exifTIFFLength > 0)) {
195155efc227SAndreas Gohr            $this->_info['exif']['TIFFStrips'] = $this->_getFixedString($data, $base + $exifTIFFOffset, $exifTIFFLength);
195255efc227SAndreas Gohr        }
195355efc227SAndreas Gohr
195455efc227SAndreas Gohr        $nextOffset = $this->_getLong($data, $base + $offset, $isBigEndian);
195555efc227SAndreas Gohr        return $nextOffset;
195655efc227SAndreas Gohr    }
195755efc227SAndreas Gohr
195855efc227SAndreas Gohr    /*************************************************************/
19590b17fdc6SAndreas Gohr    function & _createMarkerExif() {
196055efc227SAndreas Gohr        $data = null;
196155efc227SAndreas Gohr        $count = count($this->_markers);
196255efc227SAndreas Gohr        for ($i = 0; $i < $count; $i++) {
1963*45717242SAndreas Gohr            if ($this->_markers[$i]['marker'] == 0xE1
1964*45717242SAndreas Gohr                && strlen($this->_markers[$i]['data']) >= 6) {
196555efc227SAndreas Gohr                $signature = $this->_getFixedString($this->_markers[$i]['data'], 0, 6);
196655efc227SAndreas Gohr                if ($signature == "Exif\0\0") {
196755efc227SAndreas Gohr                    $data =& $this->_markers[$i]['data'];
196855efc227SAndreas Gohr                    break;
196955efc227SAndreas Gohr                }
197055efc227SAndreas Gohr            }
197155efc227SAndreas Gohr        }
197255efc227SAndreas Gohr
197355efc227SAndreas Gohr        if (!isset($this->_info['exif'])) {
197455efc227SAndreas Gohr            return false;
197555efc227SAndreas Gohr        }
197655efc227SAndreas Gohr
197755efc227SAndreas Gohr        $data = "Exif\0\0";
197855efc227SAndreas Gohr        $pos = 6;
197955efc227SAndreas Gohr        $offsetBase = 6;
198055efc227SAndreas Gohr
198155efc227SAndreas Gohr        if (isset($this->_info['exif']['ByteAlign']) && ($this->_info['exif']['ByteAlign'] == "Big Endian")) {
198255efc227SAndreas Gohr            $isBigEndian = true;
198355efc227SAndreas Gohr            $aux = "MM";
198455efc227SAndreas Gohr            $pos = $this->_putString($data, $pos, $aux);
19850b17fdc6SAndreas Gohr        } else {
198655efc227SAndreas Gohr            $isBigEndian = false;
198755efc227SAndreas Gohr            $aux = "II";
198855efc227SAndreas Gohr            $pos = $this->_putString($data, $pos, $aux);
198955efc227SAndreas Gohr        }
199055efc227SAndreas Gohr        $pos = $this->_putShort($data, $pos, 0x002A, $isBigEndian);
199155efc227SAndreas Gohr        $pos = $this->_putLong($data, $pos, 0x00000008, $isBigEndian); // IFD0 Offset is always 8
199255efc227SAndreas Gohr
199355efc227SAndreas Gohr        $ifd0 =& $this->_getIFDEntries($isBigEndian, 'ifd0');
199455efc227SAndreas Gohr        $ifd1 =& $this->_getIFDEntries($isBigEndian, 'ifd1');
199555efc227SAndreas Gohr
199655efc227SAndreas Gohr        $pos = $this->_writeIFD($data, $pos, $offsetBase, $ifd0, $isBigEndian, true);
199755efc227SAndreas Gohr        $pos = $this->_writeIFD($data, $pos, $offsetBase, $ifd1, $isBigEndian, false);
199855efc227SAndreas Gohr
199955efc227SAndreas Gohr        return $data;
200055efc227SAndreas Gohr    }
200155efc227SAndreas Gohr
200255efc227SAndreas Gohr    /*************************************************************/
2003276820f7SScrutinizer Auto-Fixer
2004276820f7SScrutinizer Auto-Fixer    /**
2005f50a239bSTakamura     * @param mixed $data
2006f50a239bSTakamura     * @param integer $pos
2007276820f7SScrutinizer Auto-Fixer     * @param integer $offsetBase
2008f50a239bSTakamura     * @param array $entries
2009276820f7SScrutinizer Auto-Fixer     * @param boolean $isBigEndian
2010276820f7SScrutinizer Auto-Fixer     * @param boolean $hasNext
2011f50a239bSTakamura     *
2012f50a239bSTakamura     * @return mixed
2013276820f7SScrutinizer Auto-Fixer     */
20140b17fdc6SAndreas Gohr    function _writeIFD(&$data, $pos, $offsetBase, &$entries, $isBigEndian, $hasNext) {
201555efc227SAndreas Gohr        $tiffData = null;
201655efc227SAndreas Gohr        $tiffDataOffsetPos = -1;
201755efc227SAndreas Gohr
201855efc227SAndreas Gohr        $entryCount = count($entries);
201955efc227SAndreas Gohr
202055efc227SAndreas Gohr        $dataPos = $pos + 2 + ($entryCount * 12) + 4;
202155efc227SAndreas Gohr        $pos = $this->_putShort($data, $pos, $entryCount, $isBigEndian);
202255efc227SAndreas Gohr
202355efc227SAndreas Gohr        for ($i = 0; $i < $entryCount; $i++) {
202455efc227SAndreas Gohr            $tag = $entries[$i]['tag'];
202555efc227SAndreas Gohr            $type = $entries[$i]['type'];
202655efc227SAndreas Gohr
202755efc227SAndreas Gohr            if ($type == -99) { // SubIFD
202855efc227SAndreas Gohr                $pos = $this->_putShort($data, $pos, $tag, $isBigEndian);
202955efc227SAndreas Gohr                $pos = $this->_putShort($data, $pos, 0x04, $isBigEndian); // LONG
203055efc227SAndreas Gohr                $pos = $this->_putLong($data, $pos, 0x01, $isBigEndian); // Count = 1
203155efc227SAndreas Gohr                $pos = $this->_putLong($data, $pos, $dataPos - $offsetBase, $isBigEndian);
203255efc227SAndreas Gohr
203355efc227SAndreas Gohr                $dataPos = $this->_writeIFD($data, $dataPos, $offsetBase, $entries[$i]['value'], $isBigEndian, false);
20340b17fdc6SAndreas Gohr            } elseif ($type == -98) { // TIFF Data
203555efc227SAndreas Gohr                $pos = $this->_putShort($data, $pos, $tag, $isBigEndian);
203655efc227SAndreas Gohr                $pos = $this->_putShort($data, $pos, 0x04, $isBigEndian); // LONG
203755efc227SAndreas Gohr                $pos = $this->_putLong($data, $pos, 0x01, $isBigEndian); // Count = 1
203855efc227SAndreas Gohr                $tiffDataOffsetPos = $pos;
203955efc227SAndreas Gohr                $pos = $this->_putLong($data, $pos, 0x00, $isBigEndian); // For Now
204055efc227SAndreas Gohr                $tiffData =& $entries[$i]['value'] ;
20410b17fdc6SAndreas Gohr            } else { // Regular Entry
204255efc227SAndreas Gohr                $pos = $this->_putShort($data, $pos, $tag, $isBigEndian);
204355efc227SAndreas Gohr                $pos = $this->_putShort($data, $pos, $type, $isBigEndian);
204455efc227SAndreas Gohr                $pos = $this->_putLong($data, $pos, $entries[$i]['count'], $isBigEndian);
204555efc227SAndreas Gohr                if (strlen($entries[$i]['value']) > 4) {
204655efc227SAndreas Gohr                    $pos = $this->_putLong($data, $pos, $dataPos - $offsetBase, $isBigEndian);
204755efc227SAndreas Gohr                    $dataPos = $this->_putString($data, $dataPos, $entries[$i]['value']);
20480b17fdc6SAndreas Gohr                } else {
204955efc227SAndreas Gohr                    $val = str_pad($entries[$i]['value'], 4, "\0");
205055efc227SAndreas Gohr                    $pos = $this->_putString($data, $pos, $val);
205155efc227SAndreas Gohr                }
205255efc227SAndreas Gohr            }
205355efc227SAndreas Gohr        }
205455efc227SAndreas Gohr
205555efc227SAndreas Gohr        if ($tiffData != null) {
205655efc227SAndreas Gohr            $this->_putLong($data, $tiffDataOffsetPos, $dataPos - $offsetBase, $isBigEndian);
205755efc227SAndreas Gohr            $dataPos = $this->_putString($data, $dataPos, $tiffData);
205855efc227SAndreas Gohr        }
205955efc227SAndreas Gohr
206055efc227SAndreas Gohr        if ($hasNext) {
206155efc227SAndreas Gohr            $pos = $this->_putLong($data, $pos, $dataPos - $offsetBase, $isBigEndian);
20620b17fdc6SAndreas Gohr        } else {
206355efc227SAndreas Gohr            $pos = $this->_putLong($data, $pos, 0, $isBigEndian);
206455efc227SAndreas Gohr        }
206555efc227SAndreas Gohr
206655efc227SAndreas Gohr        return $dataPos;
206755efc227SAndreas Gohr    }
206855efc227SAndreas Gohr
206955efc227SAndreas Gohr    /*************************************************************/
2070276820f7SScrutinizer Auto-Fixer
2071276820f7SScrutinizer Auto-Fixer    /**
2072276820f7SScrutinizer Auto-Fixer     * @param boolean $isBigEndian
2073276820f7SScrutinizer Auto-Fixer     * @param string $mode
2074f50a239bSTakamura     *
2075f50a239bSTakamura     * @return array
2076276820f7SScrutinizer Auto-Fixer     */
20770b17fdc6SAndreas Gohr    function & _getIFDEntries($isBigEndian, $mode) {
207855efc227SAndreas Gohr        $EXIFNames = $this->_exifTagNames($mode);
207955efc227SAndreas Gohr        $EXIFTags = $this->_exifNameTags($mode);
208055efc227SAndreas Gohr        $EXIFTypeInfo = $this->_exifTagTypes($mode);
208155efc227SAndreas Gohr
208255efc227SAndreas Gohr        $ifdEntries = array();
208355efc227SAndreas Gohr        $entryCount = 0;
208455efc227SAndreas Gohr
20859e491c01SAndreas Gohr        foreach($EXIFNames as $tag => $name) {
208655efc227SAndreas Gohr            $type = $EXIFTypeInfo[$tag][0];
208755efc227SAndreas Gohr            $count = $EXIFTypeInfo[$tag][1];
208855efc227SAndreas Gohr            $value = null;
208955efc227SAndreas Gohr
209055efc227SAndreas Gohr            if (($mode == 'ifd0') && ($tag == 0x8769)) {  // ExifIFDOffset
209155efc227SAndreas Gohr                if (isset($this->_info['exif']['EXIFVersion'])) {
209255efc227SAndreas Gohr                    $value =& $this->_getIFDEntries($isBigEndian, "exif");
209355efc227SAndreas Gohr                    $type = -99;
209455efc227SAndreas Gohr                }
209555efc227SAndreas Gohr                else {
209655efc227SAndreas Gohr                    $value = null;
209755efc227SAndreas Gohr                }
20980b17fdc6SAndreas Gohr            } elseif (($mode == 'ifd0') && ($tag == 0x8825)) {  // GPSIFDOffset
209955efc227SAndreas Gohr                if (isset($this->_info['exif']['GPSVersionID'])) {
210055efc227SAndreas Gohr                    $value =& $this->_getIFDEntries($isBigEndian, "gps");
210155efc227SAndreas Gohr                    $type = -99;
21020b17fdc6SAndreas Gohr                } else {
210355efc227SAndreas Gohr                    $value = null;
210455efc227SAndreas Gohr                }
21050b17fdc6SAndreas Gohr            } elseif (($mode == 'ifd1') && ($tag == 0x0111)) {  // TIFFStripOffsets
210655efc227SAndreas Gohr                if (isset($this->_info['exif']['TIFFStrips'])) {
210755efc227SAndreas Gohr                    $value =& $this->_info['exif']['TIFFStrips'];
210855efc227SAndreas Gohr                    $type = -98;
21090b17fdc6SAndreas Gohr                } else {
211055efc227SAndreas Gohr                    $value = null;
211155efc227SAndreas Gohr                }
21120b17fdc6SAndreas Gohr            } elseif (($mode == 'ifd1') && ($tag == 0x0117)) {  // TIFFStripByteCounts
211355efc227SAndreas Gohr                if (isset($this->_info['exif']['TIFFStrips'])) {
211455efc227SAndreas Gohr                    $value = strlen($this->_info['exif']['TIFFStrips']);
21150b17fdc6SAndreas Gohr                } else {
211655efc227SAndreas Gohr                    $value = null;
211755efc227SAndreas Gohr                }
21180b17fdc6SAndreas Gohr            } elseif (($mode == 'ifd1') && ($tag == 0x0201)) {  // TIFFJFIFOffset
211955efc227SAndreas Gohr                if (isset($this->_info['exif']['JFIFThumbnail'])) {
212055efc227SAndreas Gohr                    $value =& $this->_info['exif']['JFIFThumbnail'];
212155efc227SAndreas Gohr                    $type = -98;
21220b17fdc6SAndreas Gohr                } else {
212355efc227SAndreas Gohr                    $value = null;
212455efc227SAndreas Gohr                }
21250b17fdc6SAndreas Gohr            } elseif (($mode == 'ifd1') && ($tag == 0x0202)) {  // TIFFJFIFLength
212655efc227SAndreas Gohr                if (isset($this->_info['exif']['JFIFThumbnail'])) {
212755efc227SAndreas Gohr                    $value = strlen($this->_info['exif']['JFIFThumbnail']);
21280b17fdc6SAndreas Gohr                } else {
212955efc227SAndreas Gohr                    $value = null;
213055efc227SAndreas Gohr                }
21310b17fdc6SAndreas Gohr            } elseif (($mode == 'exif') && ($tag == 0xA005)) {  // InteropIFDOffset
213255efc227SAndreas Gohr                if (isset($this->_info['exif']['InteroperabilityIndex'])) {
213355efc227SAndreas Gohr                    $value =& $this->_getIFDEntries($isBigEndian, "interop");
213455efc227SAndreas Gohr                    $type = -99;
21350b17fdc6SAndreas Gohr                } else {
213655efc227SAndreas Gohr                    $value = null;
213755efc227SAndreas Gohr                }
21380b17fdc6SAndreas Gohr            } elseif (isset($this->_info['exif'][$name])) {
213955efc227SAndreas Gohr                $origValue =& $this->_info['exif'][$name];
214055efc227SAndreas Gohr
214155efc227SAndreas Gohr                // This makes it easier to process variable size elements
214255efc227SAndreas Gohr                if (!is_array($origValue) || isset($origValue['val'])) {
214355efc227SAndreas Gohr                    unset($origValue); // Break the reference
214455efc227SAndreas Gohr                    $origValue = array($this->_info['exif'][$name]);
214555efc227SAndreas Gohr                }
214655efc227SAndreas Gohr                $origCount = count($origValue);
214755efc227SAndreas Gohr
214855efc227SAndreas Gohr                if ($origCount == 0 ) {
214955efc227SAndreas Gohr                    $type = -1;  // To ignore this field
215055efc227SAndreas Gohr                }
215155efc227SAndreas Gohr
215255efc227SAndreas Gohr                $value = " ";
215355efc227SAndreas Gohr
215455efc227SAndreas Gohr                switch ($type) {
215555efc227SAndreas Gohr                    case 1:    // UBYTE
215655efc227SAndreas Gohr                        if ($count == 0) {
215755efc227SAndreas Gohr                            $count = $origCount;
215855efc227SAndreas Gohr                        }
215955efc227SAndreas Gohr
216055efc227SAndreas Gohr                        $j = 0;
216155efc227SAndreas Gohr                        while (($j < $count) && ($j < $origCount)) {
216255efc227SAndreas Gohr
216355efc227SAndreas Gohr                            $this->_putByte($value, $j, $origValue[$j]);
216455efc227SAndreas Gohr                            $j++;
216555efc227SAndreas Gohr                        }
216655efc227SAndreas Gohr
216755efc227SAndreas Gohr                        while ($j < $count) {
216855efc227SAndreas Gohr                            $this->_putByte($value, $j, 0);
216955efc227SAndreas Gohr                            $j++;
217055efc227SAndreas Gohr                        }
217155efc227SAndreas Gohr                        break;
217255efc227SAndreas Gohr                    case 2:    // ASCII
217355efc227SAndreas Gohr                        $v = strval($origValue[0]);
217455efc227SAndreas Gohr                        if (($count != 0) && (strlen($v) > $count)) {
217555efc227SAndreas Gohr                            $v = substr($v, 0, $count);
217655efc227SAndreas Gohr                        }
217755efc227SAndreas Gohr                        elseif (($count > 0) && (strlen($v) < $count)) {
217855efc227SAndreas Gohr                            $v = str_pad($v, $count, "\0");
217955efc227SAndreas Gohr                        }
218055efc227SAndreas Gohr
218155efc227SAndreas Gohr                        $count = strlen($v);
218255efc227SAndreas Gohr
218355efc227SAndreas Gohr                        $this->_putString($value, 0, $v);
218455efc227SAndreas Gohr                        break;
218555efc227SAndreas Gohr                    case 3:    // USHORT
218655efc227SAndreas Gohr                        if ($count == 0) {
218755efc227SAndreas Gohr                            $count = $origCount;
218855efc227SAndreas Gohr                        }
218955efc227SAndreas Gohr
219055efc227SAndreas Gohr                        $j = 0;
219155efc227SAndreas Gohr                        while (($j < $count) && ($j < $origCount)) {
219255efc227SAndreas Gohr                            $this->_putShort($value, $j * 2, $origValue[$j], $isBigEndian);
219355efc227SAndreas Gohr                            $j++;
219455efc227SAndreas Gohr                        }
219555efc227SAndreas Gohr
219655efc227SAndreas Gohr                        while ($j < $count) {
219755efc227SAndreas Gohr                            $this->_putShort($value, $j * 2, 0, $isBigEndian);
219855efc227SAndreas Gohr                            $j++;
219955efc227SAndreas Gohr                        }
220055efc227SAndreas Gohr                        break;
220155efc227SAndreas Gohr                    case 4:    // ULONG
220255efc227SAndreas Gohr                        if ($count == 0) {
220355efc227SAndreas Gohr                            $count = $origCount;
220455efc227SAndreas Gohr                        }
220555efc227SAndreas Gohr
220655efc227SAndreas Gohr                        $j = 0;
220755efc227SAndreas Gohr                        while (($j < $count) && ($j < $origCount)) {
220855efc227SAndreas Gohr                            $this->_putLong($value, $j * 4, $origValue[$j], $isBigEndian);
220955efc227SAndreas Gohr                            $j++;
221055efc227SAndreas Gohr                        }
221155efc227SAndreas Gohr
221255efc227SAndreas Gohr                        while ($j < $count) {
221355efc227SAndreas Gohr                            $this->_putLong($value, $j * 4, 0, $isBigEndian);
221455efc227SAndreas Gohr                            $j++;
221555efc227SAndreas Gohr                        }
221655efc227SAndreas Gohr                        break;
221755efc227SAndreas Gohr                    case 5:    // URATIONAL
221855efc227SAndreas Gohr                        if ($count == 0) {
221955efc227SAndreas Gohr                            $count = $origCount;
222055efc227SAndreas Gohr                        }
222155efc227SAndreas Gohr
222255efc227SAndreas Gohr                        $j = 0;
222355efc227SAndreas Gohr                        while (($j < $count) && ($j < $origCount)) {
222455efc227SAndreas Gohr                            $v = $origValue[$j];
222555efc227SAndreas Gohr                            if (is_array($v)) {
222655efc227SAndreas Gohr                                $a = $v['num'];
222755efc227SAndreas Gohr                                $b = $v['den'];
222855efc227SAndreas Gohr                            }
222955efc227SAndreas Gohr                            else {
223055efc227SAndreas Gohr                                $a = 0;
223155efc227SAndreas Gohr                                $b = 0;
223255efc227SAndreas Gohr                                // TODO: Allow other types and convert them
223355efc227SAndreas Gohr                            }
223455efc227SAndreas Gohr                            $this->_putLong($value, $j * 8, $a, $isBigEndian);
223555efc227SAndreas Gohr                            $this->_putLong($value, ($j * 8) + 4, $b, $isBigEndian);
223655efc227SAndreas Gohr                            $j++;
223755efc227SAndreas Gohr                        }
223855efc227SAndreas Gohr
223955efc227SAndreas Gohr                        while ($j < $count) {
224055efc227SAndreas Gohr                            $this->_putLong($value, $j * 8, 0, $isBigEndian);
224155efc227SAndreas Gohr                            $this->_putLong($value, ($j * 8) + 4, 0, $isBigEndian);
224255efc227SAndreas Gohr                            $j++;
224355efc227SAndreas Gohr                        }
224455efc227SAndreas Gohr                        break;
224555efc227SAndreas Gohr                    case 6:    // SBYTE
224655efc227SAndreas Gohr                        if ($count == 0) {
224755efc227SAndreas Gohr                            $count = $origCount;
224855efc227SAndreas Gohr                        }
224955efc227SAndreas Gohr
225055efc227SAndreas Gohr                        $j = 0;
225155efc227SAndreas Gohr                        while (($j < $count) && ($j < $origCount)) {
225255efc227SAndreas Gohr                            $this->_putByte($value, $j, $origValue[$j]);
225355efc227SAndreas Gohr                            $j++;
225455efc227SAndreas Gohr                        }
225555efc227SAndreas Gohr
225655efc227SAndreas Gohr                        while ($j < $count) {
225755efc227SAndreas Gohr                            $this->_putByte($value, $j, 0);
225855efc227SAndreas Gohr                            $j++;
225955efc227SAndreas Gohr                        }
226055efc227SAndreas Gohr                        break;
226155efc227SAndreas Gohr                    case 7:    // UNDEFINED
226255efc227SAndreas Gohr                        $v = strval($origValue[0]);
226355efc227SAndreas Gohr                        if (($count != 0) && (strlen($v) > $count)) {
226455efc227SAndreas Gohr                            $v = substr($v, 0, $count);
226555efc227SAndreas Gohr                        }
226655efc227SAndreas Gohr                        elseif (($count > 0) && (strlen($v) < $count)) {
226755efc227SAndreas Gohr                            $v = str_pad($v, $count, "\0");
226855efc227SAndreas Gohr                        }
226955efc227SAndreas Gohr
227055efc227SAndreas Gohr                        $count = strlen($v);
227155efc227SAndreas Gohr
227255efc227SAndreas Gohr                        $this->_putString($value, 0, $v);
227355efc227SAndreas Gohr                        break;
227455efc227SAndreas Gohr                    case 8:    // SSHORT
227555efc227SAndreas Gohr                        if ($count == 0) {
227655efc227SAndreas Gohr                            $count = $origCount;
227755efc227SAndreas Gohr                        }
227855efc227SAndreas Gohr
227955efc227SAndreas Gohr                        $j = 0;
228055efc227SAndreas Gohr                        while (($j < $count) && ($j < $origCount)) {
228155efc227SAndreas Gohr                            $this->_putShort($value, $j * 2, $origValue[$j], $isBigEndian);
228255efc227SAndreas Gohr                            $j++;
228355efc227SAndreas Gohr                        }
228455efc227SAndreas Gohr
228555efc227SAndreas Gohr                        while ($j < $count) {
228655efc227SAndreas Gohr                            $this->_putShort($value, $j * 2, 0, $isBigEndian);
228755efc227SAndreas Gohr                            $j++;
228855efc227SAndreas Gohr                        }
228955efc227SAndreas Gohr                        break;
229055efc227SAndreas Gohr                    case 9:    // SLONG
229155efc227SAndreas Gohr                        if ($count == 0) {
229255efc227SAndreas Gohr                            $count = $origCount;
229355efc227SAndreas Gohr                        }
229455efc227SAndreas Gohr
229555efc227SAndreas Gohr                        $j = 0;
229655efc227SAndreas Gohr                        while (($j < $count) && ($j < $origCount)) {
229755efc227SAndreas Gohr                            $this->_putLong($value, $j * 4, $origValue[$j], $isBigEndian);
229855efc227SAndreas Gohr                            $j++;
229955efc227SAndreas Gohr                        }
230055efc227SAndreas Gohr
230155efc227SAndreas Gohr                        while ($j < $count) {
230255efc227SAndreas Gohr                            $this->_putLong($value, $j * 4, 0, $isBigEndian);
230355efc227SAndreas Gohr                            $j++;
230455efc227SAndreas Gohr                        }
230555efc227SAndreas Gohr                        break;
230655efc227SAndreas Gohr                    case 10:   // SRATIONAL
230755efc227SAndreas Gohr                        if ($count == 0) {
230855efc227SAndreas Gohr                            $count = $origCount;
230955efc227SAndreas Gohr                        }
231055efc227SAndreas Gohr
231155efc227SAndreas Gohr                        $j = 0;
231255efc227SAndreas Gohr                        while (($j < $count) && ($j < $origCount)) {
231355efc227SAndreas Gohr                            $v = $origValue[$j];
231455efc227SAndreas Gohr                            if (is_array($v)) {
231555efc227SAndreas Gohr                                $a = $v['num'];
231655efc227SAndreas Gohr                                $b = $v['den'];
231755efc227SAndreas Gohr                            }
231855efc227SAndreas Gohr                            else {
231955efc227SAndreas Gohr                                $a = 0;
232055efc227SAndreas Gohr                                $b = 0;
232155efc227SAndreas Gohr                                // TODO: Allow other types and convert them
232255efc227SAndreas Gohr                            }
232355efc227SAndreas Gohr
232455efc227SAndreas Gohr                            $this->_putLong($value, $j * 8, $a, $isBigEndian);
232555efc227SAndreas Gohr                            $this->_putLong($value, ($j * 8) + 4, $b, $isBigEndian);
232655efc227SAndreas Gohr                            $j++;
232755efc227SAndreas Gohr                        }
232855efc227SAndreas Gohr
232955efc227SAndreas Gohr                        while ($j < $count) {
233055efc227SAndreas Gohr                            $this->_putLong($value, $j * 8, 0, $isBigEndian);
233155efc227SAndreas Gohr                            $this->_putLong($value, ($j * 8) + 4, 0, $isBigEndian);
233255efc227SAndreas Gohr                            $j++;
233355efc227SAndreas Gohr                        }
233455efc227SAndreas Gohr                        break;
233555efc227SAndreas Gohr                    case 11:   // FLOAT
233655efc227SAndreas Gohr                        if ($count == 0) {
233755efc227SAndreas Gohr                            $count = $origCount;
233855efc227SAndreas Gohr                        }
233955efc227SAndreas Gohr
234055efc227SAndreas Gohr                        $j = 0;
234155efc227SAndreas Gohr                        while (($j < $count) && ($j < $origCount)) {
234255efc227SAndreas Gohr                            $v = strval($origValue[$j]);
234355efc227SAndreas Gohr                            if (strlen($v) > 4) {
234455efc227SAndreas Gohr                                $v = substr($v, 0, 4);
234555efc227SAndreas Gohr                            }
234655efc227SAndreas Gohr                            elseif (strlen($v) < 4) {
234755efc227SAndreas Gohr                                $v = str_pad($v, 4, "\0");
234855efc227SAndreas Gohr                            }
234955efc227SAndreas Gohr                            $this->_putString($value, $j * 4, $v);
235055efc227SAndreas Gohr                            $j++;
235155efc227SAndreas Gohr                        }
235255efc227SAndreas Gohr
235355efc227SAndreas Gohr                        while ($j < $count) {
23545aaca723SGerrit Uitslag                            $v = "\0\0\0\0";
23555aaca723SGerrit Uitslag                            $this->_putString($value, $j * 4, $v);
235655efc227SAndreas Gohr                            $j++;
235755efc227SAndreas Gohr                        }
235855efc227SAndreas Gohr                        break;
235955efc227SAndreas Gohr                    case 12:   // DFLOAT
236055efc227SAndreas Gohr                        if ($count == 0) {
236155efc227SAndreas Gohr                            $count = $origCount;
236255efc227SAndreas Gohr                        }
236355efc227SAndreas Gohr
236455efc227SAndreas Gohr                        $j = 0;
236555efc227SAndreas Gohr                        while (($j < $count) && ($j < $origCount)) {
236655efc227SAndreas Gohr                            $v = strval($origValue[$j]);
236755efc227SAndreas Gohr                            if (strlen($v) > 8) {
236855efc227SAndreas Gohr                                $v = substr($v, 0, 8);
236955efc227SAndreas Gohr                            }
237055efc227SAndreas Gohr                            elseif (strlen($v) < 8) {
237155efc227SAndreas Gohr                                $v = str_pad($v, 8, "\0");
237255efc227SAndreas Gohr                            }
237355efc227SAndreas Gohr                            $this->_putString($value, $j * 8, $v);
237455efc227SAndreas Gohr                            $j++;
237555efc227SAndreas Gohr                        }
237655efc227SAndreas Gohr
237755efc227SAndreas Gohr                        while ($j < $count) {
23785aaca723SGerrit Uitslag                            $v = "\0\0\0\0\0\0\0\0";
23795aaca723SGerrit Uitslag                            $this->_putString($value, $j * 8, $v);
238055efc227SAndreas Gohr                            $j++;
238155efc227SAndreas Gohr                        }
238255efc227SAndreas Gohr                        break;
238355efc227SAndreas Gohr                    default:
238455efc227SAndreas Gohr                        $value = null;
238555efc227SAndreas Gohr                        break;
238655efc227SAndreas Gohr                }
238755efc227SAndreas Gohr            }
238855efc227SAndreas Gohr
238955efc227SAndreas Gohr            if ($value != null) {
239055efc227SAndreas Gohr                $ifdEntries[$entryCount] = array();
239155efc227SAndreas Gohr                $ifdEntries[$entryCount]['tag'] = $tag;
239255efc227SAndreas Gohr                $ifdEntries[$entryCount]['type'] = $type;
239355efc227SAndreas Gohr                $ifdEntries[$entryCount]['count'] = $count;
239455efc227SAndreas Gohr                $ifdEntries[$entryCount]['value'] = $value;
239555efc227SAndreas Gohr
239655efc227SAndreas Gohr                $entryCount++;
239755efc227SAndreas Gohr            }
239855efc227SAndreas Gohr        }
239955efc227SAndreas Gohr
240055efc227SAndreas Gohr        return $ifdEntries;
240155efc227SAndreas Gohr    }
2402c9c56f8aSasivery    /*************************************************************/
2403c9c56f8aSasivery    function _handleMarkerParsingException($e) {
2404d40e7d0eSAndreas Gohr        \dokuwiki\ErrorHandler::logException($e, $this->_fileName);
2405c9c56f8aSasivery    }
2406c9c56f8aSasivery
2407c9c56f8aSasivery    /*************************************************************/
2408c9c56f8aSasivery    function _isMarkerDisabled($name) {
2409c9c56f8aSasivery        if (!isset($this->_info)) return false;
2410c9c56f8aSasivery        return isset($this->_info[$name]) && $this->_info[$name] === false;
2411c9c56f8aSasivery    }
241255efc227SAndreas Gohr
241355efc227SAndreas Gohr    /*************************************************************/
24140b17fdc6SAndreas Gohr    function _parseMarkerAdobe() {
241555efc227SAndreas Gohr        if (!isset($this->_markers)) {
241655efc227SAndreas Gohr            $this->_readJPEG();
241755efc227SAndreas Gohr        }
241855efc227SAndreas Gohr
2419c9c56f8aSasivery        if ($this->_markers == null || $this->_isMarkerDisabled('adobe')) {
242055efc227SAndreas Gohr            return false;
242155efc227SAndreas Gohr        }
2422c9c56f8aSasivery        try {
242355efc227SAndreas Gohr            $data = null;
242455efc227SAndreas Gohr            $count = count($this->_markers);
242555efc227SAndreas Gohr            for ($i = 0; $i < $count; $i++) {
2426*45717242SAndreas Gohr                if ($this->_markers[$i]['marker'] == 0xED
2427*45717242SAndreas Gohr                    && strlen($this->_markers[$i]['data']) >= 14) {
242855efc227SAndreas Gohr                    $signature = $this->_getFixedString($this->_markers[$i]['data'], 0, 14);
242955efc227SAndreas Gohr                    if ($signature == "Photoshop 3.0\0") {
243055efc227SAndreas Gohr                        $data =& $this->_markers[$i]['data'];
243155efc227SAndreas Gohr                        break;
243255efc227SAndreas Gohr                    }
243355efc227SAndreas Gohr                }
243455efc227SAndreas Gohr            }
243555efc227SAndreas Gohr
243655efc227SAndreas Gohr            if ($data == null) {
243755efc227SAndreas Gohr                $this->_info['adobe'] = false;
243855efc227SAndreas Gohr                $this->_info['iptc'] = false;
243955efc227SAndreas Gohr                return false;
244055efc227SAndreas Gohr            }
244155efc227SAndreas Gohr            $pos = 14;
244255efc227SAndreas Gohr            $this->_info['adobe'] = array();
244355efc227SAndreas Gohr            $this->_info['adobe']['raw'] = array();
244455efc227SAndreas Gohr            $this->_info['iptc'] = array();
244555efc227SAndreas Gohr
244655efc227SAndreas Gohr            $datasize = strlen($data);
244755efc227SAndreas Gohr
244855efc227SAndreas Gohr            while ($pos < $datasize) {
244955efc227SAndreas Gohr                $signature = $this->_getFixedString($data, $pos, 4);
245055efc227SAndreas Gohr                if ($signature != '8BIM')
245155efc227SAndreas Gohr                    return false;
245255efc227SAndreas Gohr                $pos += 4;
245355efc227SAndreas Gohr
245455efc227SAndreas Gohr                $type = $this->_getShort($data, $pos);
245555efc227SAndreas Gohr                $pos += 2;
245655efc227SAndreas Gohr
245755efc227SAndreas Gohr                $strlen = $this->_getByte($data, $pos);
245855efc227SAndreas Gohr                $pos += 1;
245955efc227SAndreas Gohr                $header = '';
246055efc227SAndreas Gohr                for ($i = 0; $i < $strlen; $i++) {
24612401f18dSSyntaxseed                    $header .= $data[$pos + $i];
246255efc227SAndreas Gohr                }
246355efc227SAndreas Gohr                $pos += $strlen + 1 - ($strlen % 2);  // The string is padded to even length, counting the length byte itself
246455efc227SAndreas Gohr
246555efc227SAndreas Gohr                $length = $this->_getLong($data, $pos);
246655efc227SAndreas Gohr                $pos += 4;
246755efc227SAndreas Gohr
246855efc227SAndreas Gohr                $basePos = $pos;
246955efc227SAndreas Gohr
247055efc227SAndreas Gohr                switch ($type) {
247155efc227SAndreas Gohr                    case 0x0404: // Caption (IPTC Data)
247255efc227SAndreas Gohr                        $pos = $this->_readIPTC($data, $pos);
247355efc227SAndreas Gohr                        if ($pos == false)
247455efc227SAndreas Gohr                            return false;
247555efc227SAndreas Gohr                        break;
247655efc227SAndreas Gohr                    case 0x040A: // CopyrightFlag
247755efc227SAndreas Gohr                        $this->_info['adobe']['CopyrightFlag'] = $this->_getByte($data, $pos);
247855efc227SAndreas Gohr                        $pos += $length;
247955efc227SAndreas Gohr                        break;
248055efc227SAndreas Gohr                    case 0x040B: // ImageURL
248155efc227SAndreas Gohr                        $this->_info['adobe']['ImageURL'] = $this->_getFixedString($data, $pos, $length);
248255efc227SAndreas Gohr                        $pos += $length;
248355efc227SAndreas Gohr                        break;
248455efc227SAndreas Gohr                    case 0x040C: // Thumbnail
248555efc227SAndreas Gohr                        $aux = $this->_getLong($data, $pos);
248655efc227SAndreas Gohr                        $pos += 4;
248755efc227SAndreas Gohr                        if ($aux == 1) {
248855efc227SAndreas Gohr                            $this->_info['adobe']['ThumbnailWidth'] = $this->_getLong($data, $pos);
248955efc227SAndreas Gohr                            $pos += 4;
249055efc227SAndreas Gohr                            $this->_info['adobe']['ThumbnailHeight'] = $this->_getLong($data, $pos);
249155efc227SAndreas Gohr                            $pos += 4;
249255efc227SAndreas Gohr
249355efc227SAndreas Gohr                            $pos += 16; // Skip some data
249455efc227SAndreas Gohr
249555efc227SAndreas Gohr                            $this->_info['adobe']['ThumbnailData'] = $this->_getFixedString($data, $pos, $length - 28);
249655efc227SAndreas Gohr                            $pos += $length - 28;
249755efc227SAndreas Gohr                        }
249855efc227SAndreas Gohr                        break;
249955efc227SAndreas Gohr                    default:
250055efc227SAndreas Gohr                        break;
250155efc227SAndreas Gohr                }
250255efc227SAndreas Gohr
250355efc227SAndreas Gohr                // We save all blocks, even those we recognized
250455efc227SAndreas Gohr                $label = sprintf('8BIM_0x%04x', $type);
250555efc227SAndreas Gohr                $this->_info['adobe']['raw'][$label] = array();
250655efc227SAndreas Gohr                $this->_info['adobe']['raw'][$label]['type'] = $type;
250755efc227SAndreas Gohr                $this->_info['adobe']['raw'][$label]['header'] = $header;
250855efc227SAndreas Gohr                $this->_info['adobe']['raw'][$label]['data'] =& $this->_getFixedString($data, $basePos, $length);
250955efc227SAndreas Gohr
251055efc227SAndreas Gohr                $pos = $basePos + $length + ($length % 2); // Even padding
251155efc227SAndreas Gohr            }
2512c9c56f8aSasivery        } catch(Exception $e) {
2513c9c56f8aSasivery            $this->_handleMarkerParsingException($e);
2514c9c56f8aSasivery            $this->_info['adobe'] = false;
2515c9c56f8aSasivery            $this->_info['iptc'] = false;
2516c9c56f8aSasivery            return false;
2517c9c56f8aSasivery        }
251855efc227SAndreas Gohr    }
251955efc227SAndreas Gohr
252055efc227SAndreas Gohr    /*************************************************************/
25210b17fdc6SAndreas Gohr    function _readIPTC(&$data, $pos = 0) {
252255efc227SAndreas Gohr        $totalLength = strlen($data);
252355efc227SAndreas Gohr
2524f5891fa4SGerrit Uitslag        $IPTCTags = $this->_iptcTagNames();
252555efc227SAndreas Gohr
252655efc227SAndreas Gohr        while ($pos < ($totalLength - 5)) {
252755efc227SAndreas Gohr            $signature = $this->_getShort($data, $pos);
252855efc227SAndreas Gohr            if ($signature != 0x1C02)
252955efc227SAndreas Gohr                return $pos;
253055efc227SAndreas Gohr            $pos += 2;
253155efc227SAndreas Gohr
253255efc227SAndreas Gohr            $type = $this->_getByte($data, $pos);
253355efc227SAndreas Gohr            $pos += 1;
253455efc227SAndreas Gohr            $length = $this->_getShort($data, $pos);
253555efc227SAndreas Gohr            $pos += 2;
253655efc227SAndreas Gohr
253755efc227SAndreas Gohr            $basePos = $pos;
253855efc227SAndreas Gohr            $label = '';
253955efc227SAndreas Gohr
254055efc227SAndreas Gohr            if (isset($IPTCTags[$type])) {
254155efc227SAndreas Gohr                $label = $IPTCTags[$type];
25420b17fdc6SAndreas Gohr            } else {
254355efc227SAndreas Gohr                $label = sprintf('IPTC_0x%02x', $type);
254455efc227SAndreas Gohr            }
254555efc227SAndreas Gohr
254655efc227SAndreas Gohr            if ($label != '') {
254755efc227SAndreas Gohr                if (isset($this->_info['iptc'][$label])) {
254855efc227SAndreas Gohr                    if (!is_array($this->_info['iptc'][$label])) {
254955efc227SAndreas Gohr                        $aux = array();
255055efc227SAndreas Gohr                        $aux[0] = $this->_info['iptc'][$label];
255155efc227SAndreas Gohr                        $this->_info['iptc'][$label] = $aux;
255255efc227SAndreas Gohr                    }
255355efc227SAndreas Gohr                    $this->_info['iptc'][$label][ count($this->_info['iptc'][$label]) ] = $this->_getFixedString($data, $pos, $length);
25540b17fdc6SAndreas Gohr                } else {
255555efc227SAndreas Gohr                    $this->_info['iptc'][$label] = $this->_getFixedString($data, $pos, $length);
255655efc227SAndreas Gohr                }
255755efc227SAndreas Gohr            }
255855efc227SAndreas Gohr
255955efc227SAndreas Gohr            $pos = $basePos + $length; // No padding
256055efc227SAndreas Gohr        }
256155efc227SAndreas Gohr        return $pos;
256255efc227SAndreas Gohr    }
256355efc227SAndreas Gohr
256455efc227SAndreas Gohr    /*************************************************************/
25650b17fdc6SAndreas Gohr    function & _createMarkerAdobe() {
256655efc227SAndreas Gohr        if (isset($this->_info['iptc'])) {
256755efc227SAndreas Gohr            if (!isset($this->_info['adobe'])) {
256855efc227SAndreas Gohr                $this->_info['adobe'] = array();
256955efc227SAndreas Gohr            }
257055efc227SAndreas Gohr            if (!isset($this->_info['adobe']['raw'])) {
257155efc227SAndreas Gohr                $this->_info['adobe']['raw'] = array();
257255efc227SAndreas Gohr            }
257355efc227SAndreas Gohr            if (!isset($this->_info['adobe']['raw']['8BIM_0x0404'])) {
257455efc227SAndreas Gohr                $this->_info['adobe']['raw']['8BIM_0x0404'] = array();
257555efc227SAndreas Gohr            }
257655efc227SAndreas Gohr            $this->_info['adobe']['raw']['8BIM_0x0404']['type'] = 0x0404;
257755efc227SAndreas Gohr            $this->_info['adobe']['raw']['8BIM_0x0404']['header'] = "Caption";
257855efc227SAndreas Gohr            $this->_info['adobe']['raw']['8BIM_0x0404']['data'] =& $this->_writeIPTC();
257955efc227SAndreas Gohr        }
258055efc227SAndreas Gohr
258155efc227SAndreas Gohr        if (isset($this->_info['adobe']['raw']) && (count($this->_info['adobe']['raw']) > 0)) {
258255efc227SAndreas Gohr            $data = "Photoshop 3.0\0";
258355efc227SAndreas Gohr            $pos = 14;
258455efc227SAndreas Gohr
258555efc227SAndreas Gohr            reset($this->_info['adobe']['raw']);
2586dd5c3e2bSPhy            foreach ($this->_info['adobe']['raw'] as $value){
258755efc227SAndreas Gohr                $pos = $this->_write8BIM(
258855efc227SAndreas Gohr                        $data,
258955efc227SAndreas Gohr                        $pos,
2590dd5c3e2bSPhy                        $value['type'],
2591dd5c3e2bSPhy                        $value['header'],
2592dd5c3e2bSPhy                        $value['data'] );
259355efc227SAndreas Gohr            }
259455efc227SAndreas Gohr        }
259555efc227SAndreas Gohr
259655efc227SAndreas Gohr        return $data;
259755efc227SAndreas Gohr    }
259855efc227SAndreas Gohr
259955efc227SAndreas Gohr    /*************************************************************/
2600276820f7SScrutinizer Auto-Fixer
2601276820f7SScrutinizer Auto-Fixer    /**
2602f50a239bSTakamura     * @param mixed $data
2603276820f7SScrutinizer Auto-Fixer     * @param integer $pos
2604f50a239bSTakamura     *
2605f50a239bSTakamura     * @param string $type
2606f50a239bSTakamura     * @param string $header
2607f50a239bSTakamura     * @param mixed $value
2608f50a239bSTakamura     *
2609f50a239bSTakamura     * @return int|mixed
2610276820f7SScrutinizer Auto-Fixer     */
26110b17fdc6SAndreas Gohr    function _write8BIM(&$data, $pos, $type, $header, &$value) {
261255efc227SAndreas Gohr        $signature = "8BIM";
261355efc227SAndreas Gohr
261455efc227SAndreas Gohr        $pos = $this->_putString($data, $pos, $signature);
261555efc227SAndreas Gohr        $pos = $this->_putShort($data, $pos, $type);
261655efc227SAndreas Gohr
261755efc227SAndreas Gohr        $len = strlen($header);
261855efc227SAndreas Gohr
261955efc227SAndreas Gohr        $pos = $this->_putByte($data, $pos, $len);
262055efc227SAndreas Gohr        $pos = $this->_putString($data, $pos, $header);
262155efc227SAndreas Gohr        if (($len % 2) == 0) {  // Even padding, including the length byte
262255efc227SAndreas Gohr            $pos = $this->_putByte($data, $pos, 0);
262355efc227SAndreas Gohr        }
262455efc227SAndreas Gohr
262555efc227SAndreas Gohr        $len = strlen($value);
262655efc227SAndreas Gohr        $pos = $this->_putLong($data, $pos, $len);
262755efc227SAndreas Gohr        $pos = $this->_putString($data, $pos, $value);
262855efc227SAndreas Gohr        if (($len % 2) != 0) {  // Even padding
262955efc227SAndreas Gohr            $pos = $this->_putByte($data, $pos, 0);
263055efc227SAndreas Gohr        }
263155efc227SAndreas Gohr        return $pos;
263255efc227SAndreas Gohr    }
263355efc227SAndreas Gohr
263455efc227SAndreas Gohr    /*************************************************************/
26350b17fdc6SAndreas Gohr    function & _writeIPTC() {
263655efc227SAndreas Gohr        $data = " ";
263755efc227SAndreas Gohr        $pos = 0;
263855efc227SAndreas Gohr
263955efc227SAndreas Gohr        $IPTCNames =& $this->_iptcNameTags();
264055efc227SAndreas Gohr
26419e491c01SAndreas Gohr        foreach($this->_info['iptc'] as $label => $value) {
264255efc227SAndreas Gohr            $value =& $this->_info['iptc'][$label];
264355efc227SAndreas Gohr            $type = -1;
264455efc227SAndreas Gohr
264555efc227SAndreas Gohr            if (isset($IPTCNames[$label])) {
264655efc227SAndreas Gohr                $type = $IPTCNames[$label];
264755efc227SAndreas Gohr            }
26486c16a3a9Sfiwswe            elseif (str_starts_with($label, 'IPTC_0x')) {
264955efc227SAndreas Gohr                $type = hexdec(substr($label, 7, 2));
265055efc227SAndreas Gohr            }
265155efc227SAndreas Gohr
265255efc227SAndreas Gohr            if ($type != -1) {
265355efc227SAndreas Gohr                if (is_array($value)) {
26540b17fdc6SAndreas Gohr                    $vcnt = count($value);
26550b17fdc6SAndreas Gohr                    for ($i = 0; $i < $vcnt; $i++) {
265655efc227SAndreas Gohr                        $pos = $this->_writeIPTCEntry($data, $pos, $type, $value[$i]);
265755efc227SAndreas Gohr                    }
265855efc227SAndreas Gohr                }
265955efc227SAndreas Gohr                else {
266055efc227SAndreas Gohr                    $pos = $this->_writeIPTCEntry($data, $pos, $type, $value);
266155efc227SAndreas Gohr                }
266255efc227SAndreas Gohr            }
266355efc227SAndreas Gohr        }
266455efc227SAndreas Gohr
266555efc227SAndreas Gohr        return $data;
266655efc227SAndreas Gohr    }
266755efc227SAndreas Gohr
266855efc227SAndreas Gohr    /*************************************************************/
2669276820f7SScrutinizer Auto-Fixer
2670276820f7SScrutinizer Auto-Fixer    /**
2671f50a239bSTakamura     * @param mixed $data
2672276820f7SScrutinizer Auto-Fixer     * @param integer $pos
2673f50a239bSTakamura     *
2674f50a239bSTakamura     * @param string $type
2675f50a239bSTakamura     * @param mixed $value
2676f50a239bSTakamura     *
2677f50a239bSTakamura     * @return int|mixed
2678276820f7SScrutinizer Auto-Fixer     */
26790b17fdc6SAndreas Gohr    function _writeIPTCEntry(&$data, $pos, $type, &$value) {
268055efc227SAndreas Gohr        $pos = $this->_putShort($data, $pos, 0x1C02);
268155efc227SAndreas Gohr        $pos = $this->_putByte($data, $pos, $type);
268255efc227SAndreas Gohr        $pos = $this->_putShort($data, $pos, strlen($value));
268355efc227SAndreas Gohr        $pos = $this->_putString($data, $pos, $value);
268455efc227SAndreas Gohr
268555efc227SAndreas Gohr        return $pos;
268655efc227SAndreas Gohr    }
268755efc227SAndreas Gohr
268855efc227SAndreas Gohr    /*************************************************************/
26890b17fdc6SAndreas Gohr    function _exifTagNames($mode) {
269055efc227SAndreas Gohr        $tags = array();
269155efc227SAndreas Gohr
269255efc227SAndreas Gohr        if ($mode == 'ifd0') {
269355efc227SAndreas Gohr            $tags[0x010E] = 'ImageDescription';
269455efc227SAndreas Gohr            $tags[0x010F] = 'Make';
269555efc227SAndreas Gohr            $tags[0x0110] = 'Model';
269655efc227SAndreas Gohr            $tags[0x0112] = 'Orientation';
269755efc227SAndreas Gohr            $tags[0x011A] = 'XResolution';
269855efc227SAndreas Gohr            $tags[0x011B] = 'YResolution';
269955efc227SAndreas Gohr            $tags[0x0128] = 'ResolutionUnit';
270055efc227SAndreas Gohr            $tags[0x0131] = 'Software';
270155efc227SAndreas Gohr            $tags[0x0132] = 'DateTime';
270255efc227SAndreas Gohr            $tags[0x013B] = 'Artist';
270355efc227SAndreas Gohr            $tags[0x013E] = 'WhitePoint';
270455efc227SAndreas Gohr            $tags[0x013F] = 'PrimaryChromaticities';
270555efc227SAndreas Gohr            $tags[0x0211] = 'YCbCrCoefficients';
270655efc227SAndreas Gohr            $tags[0x0212] = 'YCbCrSubSampling';
270755efc227SAndreas Gohr            $tags[0x0213] = 'YCbCrPositioning';
270855efc227SAndreas Gohr            $tags[0x0214] = 'ReferenceBlackWhite';
270955efc227SAndreas Gohr            $tags[0x8298] = 'Copyright';
271055efc227SAndreas Gohr            $tags[0x8769] = 'ExifIFDOffset';
271155efc227SAndreas Gohr            $tags[0x8825] = 'GPSIFDOffset';
271255efc227SAndreas Gohr        }
271355efc227SAndreas Gohr        if ($mode == 'ifd1') {
271455efc227SAndreas Gohr            $tags[0x00FE] = 'TIFFNewSubfileType';
271555efc227SAndreas Gohr            $tags[0x00FF] = 'TIFFSubfileType';
271655efc227SAndreas Gohr            $tags[0x0100] = 'TIFFImageWidth';
271755efc227SAndreas Gohr            $tags[0x0101] = 'TIFFImageHeight';
271855efc227SAndreas Gohr            $tags[0x0102] = 'TIFFBitsPerSample';
271955efc227SAndreas Gohr            $tags[0x0103] = 'TIFFCompression';
272055efc227SAndreas Gohr            $tags[0x0106] = 'TIFFPhotometricInterpretation';
272155efc227SAndreas Gohr            $tags[0x0107] = 'TIFFThreshholding';
272255efc227SAndreas Gohr            $tags[0x0108] = 'TIFFCellWidth';
272355efc227SAndreas Gohr            $tags[0x0109] = 'TIFFCellLength';
272455efc227SAndreas Gohr            $tags[0x010A] = 'TIFFFillOrder';
272555efc227SAndreas Gohr            $tags[0x010E] = 'TIFFImageDescription';
272655efc227SAndreas Gohr            $tags[0x010F] = 'TIFFMake';
272755efc227SAndreas Gohr            $tags[0x0110] = 'TIFFModel';
272855efc227SAndreas Gohr            $tags[0x0111] = 'TIFFStripOffsets';
272955efc227SAndreas Gohr            $tags[0x0112] = 'TIFFOrientation';
273055efc227SAndreas Gohr            $tags[0x0115] = 'TIFFSamplesPerPixel';
273155efc227SAndreas Gohr            $tags[0x0116] = 'TIFFRowsPerStrip';
273255efc227SAndreas Gohr            $tags[0x0117] = 'TIFFStripByteCounts';
273355efc227SAndreas Gohr            $tags[0x0118] = 'TIFFMinSampleValue';
273455efc227SAndreas Gohr            $tags[0x0119] = 'TIFFMaxSampleValue';
273555efc227SAndreas Gohr            $tags[0x011A] = 'TIFFXResolution';
273655efc227SAndreas Gohr            $tags[0x011B] = 'TIFFYResolution';
273755efc227SAndreas Gohr            $tags[0x011C] = 'TIFFPlanarConfiguration';
273855efc227SAndreas Gohr            $tags[0x0122] = 'TIFFGrayResponseUnit';
273955efc227SAndreas Gohr            $tags[0x0123] = 'TIFFGrayResponseCurve';
274055efc227SAndreas Gohr            $tags[0x0128] = 'TIFFResolutionUnit';
274155efc227SAndreas Gohr            $tags[0x0131] = 'TIFFSoftware';
274255efc227SAndreas Gohr            $tags[0x0132] = 'TIFFDateTime';
274355efc227SAndreas Gohr            $tags[0x013B] = 'TIFFArtist';
274455efc227SAndreas Gohr            $tags[0x013C] = 'TIFFHostComputer';
274555efc227SAndreas Gohr            $tags[0x0140] = 'TIFFColorMap';
274655efc227SAndreas Gohr            $tags[0x0152] = 'TIFFExtraSamples';
274755efc227SAndreas Gohr            $tags[0x0201] = 'TIFFJFIFOffset';
274855efc227SAndreas Gohr            $tags[0x0202] = 'TIFFJFIFLength';
274955efc227SAndreas Gohr            $tags[0x0211] = 'TIFFYCbCrCoefficients';
275055efc227SAndreas Gohr            $tags[0x0212] = 'TIFFYCbCrSubSampling';
275155efc227SAndreas Gohr            $tags[0x0213] = 'TIFFYCbCrPositioning';
275255efc227SAndreas Gohr            $tags[0x0214] = 'TIFFReferenceBlackWhite';
275355efc227SAndreas Gohr            $tags[0x8298] = 'TIFFCopyright';
275455efc227SAndreas Gohr            $tags[0x9286] = 'TIFFUserComment';
27550b17fdc6SAndreas Gohr        } elseif ($mode == 'exif') {
275655efc227SAndreas Gohr            $tags[0x829A] = 'ExposureTime';
275755efc227SAndreas Gohr            $tags[0x829D] = 'FNumber';
275855efc227SAndreas Gohr            $tags[0x8822] = 'ExposureProgram';
275955efc227SAndreas Gohr            $tags[0x8824] = 'SpectralSensitivity';
276055efc227SAndreas Gohr            $tags[0x8827] = 'ISOSpeedRatings';
276155efc227SAndreas Gohr            $tags[0x8828] = 'OECF';
276255efc227SAndreas Gohr            $tags[0x9000] = 'EXIFVersion';
27635d00293dSMichael Aschauer            $tags[0x9003] = 'DateTimeOriginal';
27645d00293dSMichael Aschauer            $tags[0x9004] = 'DateTimeDigitized';
276555efc227SAndreas Gohr            $tags[0x9101] = 'ComponentsConfiguration';
276655efc227SAndreas Gohr            $tags[0x9102] = 'CompressedBitsPerPixel';
276755efc227SAndreas Gohr            $tags[0x9201] = 'ShutterSpeedValue';
276855efc227SAndreas Gohr            $tags[0x9202] = 'ApertureValue';
276955efc227SAndreas Gohr            $tags[0x9203] = 'BrightnessValue';
277055efc227SAndreas Gohr            $tags[0x9204] = 'ExposureBiasValue';
277155efc227SAndreas Gohr            $tags[0x9205] = 'MaxApertureValue';
277255efc227SAndreas Gohr            $tags[0x9206] = 'SubjectDistance';
277355efc227SAndreas Gohr            $tags[0x9207] = 'MeteringMode';
277455efc227SAndreas Gohr            $tags[0x9208] = 'LightSource';
277555efc227SAndreas Gohr            $tags[0x9209] = 'Flash';
277655efc227SAndreas Gohr            $tags[0x920A] = 'FocalLength';
277755efc227SAndreas Gohr            $tags[0x927C] = 'MakerNote';
277855efc227SAndreas Gohr            $tags[0x9286] = 'UserComment';
277955efc227SAndreas Gohr            $tags[0x9290] = 'SubSecTime';
278055efc227SAndreas Gohr            $tags[0x9291] = 'SubSecTimeOriginal';
278155efc227SAndreas Gohr            $tags[0x9292] = 'SubSecTimeDigitized';
278255efc227SAndreas Gohr            $tags[0xA000] = 'FlashPixVersion';
278355efc227SAndreas Gohr            $tags[0xA001] = 'ColorSpace';
278455efc227SAndreas Gohr            $tags[0xA002] = 'PixelXDimension';
278555efc227SAndreas Gohr            $tags[0xA003] = 'PixelYDimension';
278655efc227SAndreas Gohr            $tags[0xA004] = 'RelatedSoundFile';
278755efc227SAndreas Gohr            $tags[0xA005] = 'InteropIFDOffset';
278855efc227SAndreas Gohr            $tags[0xA20B] = 'FlashEnergy';
278955efc227SAndreas Gohr            $tags[0xA20C] = 'SpatialFrequencyResponse';
279055efc227SAndreas Gohr            $tags[0xA20E] = 'FocalPlaneXResolution';
279155efc227SAndreas Gohr            $tags[0xA20F] = 'FocalPlaneYResolution';
279255efc227SAndreas Gohr            $tags[0xA210] = 'FocalPlaneResolutionUnit';
279355efc227SAndreas Gohr            $tags[0xA214] = 'SubjectLocation';
279455efc227SAndreas Gohr            $tags[0xA215] = 'ExposureIndex';
279555efc227SAndreas Gohr            $tags[0xA217] = 'SensingMethod';
279655efc227SAndreas Gohr            $tags[0xA300] = 'FileSource';
279755efc227SAndreas Gohr            $tags[0xA301] = 'SceneType';
279855efc227SAndreas Gohr            $tags[0xA302] = 'CFAPattern';
27990b17fdc6SAndreas Gohr        } elseif ($mode == 'interop') {
280055efc227SAndreas Gohr            $tags[0x0001] = 'InteroperabilityIndex';
280155efc227SAndreas Gohr            $tags[0x0002] = 'InteroperabilityVersion';
280255efc227SAndreas Gohr            $tags[0x1000] = 'RelatedImageFileFormat';
280355efc227SAndreas Gohr            $tags[0x1001] = 'RelatedImageWidth';
280455efc227SAndreas Gohr            $tags[0x1002] = 'RelatedImageLength';
28050b17fdc6SAndreas Gohr        } elseif ($mode == 'gps') {
280655efc227SAndreas Gohr            $tags[0x0000] = 'GPSVersionID';
280755efc227SAndreas Gohr            $tags[0x0001] = 'GPSLatitudeRef';
280855efc227SAndreas Gohr            $tags[0x0002] = 'GPSLatitude';
280955efc227SAndreas Gohr            $tags[0x0003] = 'GPSLongitudeRef';
281055efc227SAndreas Gohr            $tags[0x0004] = 'GPSLongitude';
281155efc227SAndreas Gohr            $tags[0x0005] = 'GPSAltitudeRef';
281255efc227SAndreas Gohr            $tags[0x0006] = 'GPSAltitude';
281355efc227SAndreas Gohr            $tags[0x0007] = 'GPSTimeStamp';
281455efc227SAndreas Gohr            $tags[0x0008] = 'GPSSatellites';
281555efc227SAndreas Gohr            $tags[0x0009] = 'GPSStatus';
281655efc227SAndreas Gohr            $tags[0x000A] = 'GPSMeasureMode';
281755efc227SAndreas Gohr            $tags[0x000B] = 'GPSDOP';
281855efc227SAndreas Gohr            $tags[0x000C] = 'GPSSpeedRef';
281955efc227SAndreas Gohr            $tags[0x000D] = 'GPSSpeed';
282055efc227SAndreas Gohr            $tags[0x000E] = 'GPSTrackRef';
282155efc227SAndreas Gohr            $tags[0x000F] = 'GPSTrack';
282255efc227SAndreas Gohr            $tags[0x0010] = 'GPSImgDirectionRef';
282355efc227SAndreas Gohr            $tags[0x0011] = 'GPSImgDirection';
282455efc227SAndreas Gohr            $tags[0x0012] = 'GPSMapDatum';
282555efc227SAndreas Gohr            $tags[0x0013] = 'GPSDestLatitudeRef';
282655efc227SAndreas Gohr            $tags[0x0014] = 'GPSDestLatitude';
282755efc227SAndreas Gohr            $tags[0x0015] = 'GPSDestLongitudeRef';
282855efc227SAndreas Gohr            $tags[0x0016] = 'GPSDestLongitude';
282955efc227SAndreas Gohr            $tags[0x0017] = 'GPSDestBearingRef';
283055efc227SAndreas Gohr            $tags[0x0018] = 'GPSDestBearing';
283155efc227SAndreas Gohr            $tags[0x0019] = 'GPSDestDistanceRef';
283255efc227SAndreas Gohr            $tags[0x001A] = 'GPSDestDistance';
283355efc227SAndreas Gohr        }
283455efc227SAndreas Gohr
283555efc227SAndreas Gohr        return $tags;
283655efc227SAndreas Gohr    }
283755efc227SAndreas Gohr
283855efc227SAndreas Gohr    /*************************************************************/
28390b17fdc6SAndreas Gohr    function _exifTagTypes($mode) {
284055efc227SAndreas Gohr        $tags = array();
284155efc227SAndreas Gohr
284255efc227SAndreas Gohr        if ($mode == 'ifd0') {
284355efc227SAndreas Gohr            $tags[0x010E] = array(2, 0); // ImageDescription -> ASCII, Any
284455efc227SAndreas Gohr            $tags[0x010F] = array(2, 0); // Make -> ASCII, Any
284555efc227SAndreas Gohr            $tags[0x0110] = array(2, 0); // Model -> ASCII, Any
284655efc227SAndreas Gohr            $tags[0x0112] = array(3, 1); // Orientation -> SHORT, 1
284755efc227SAndreas Gohr            $tags[0x011A] = array(5, 1); // XResolution -> RATIONAL, 1
284855efc227SAndreas Gohr            $tags[0x011B] = array(5, 1); // YResolution -> RATIONAL, 1
284955efc227SAndreas Gohr            $tags[0x0128] = array(3, 1); // ResolutionUnit -> SHORT
285055efc227SAndreas Gohr            $tags[0x0131] = array(2, 0); // Software -> ASCII, Any
285155efc227SAndreas Gohr            $tags[0x0132] = array(2, 20); // DateTime -> ASCII, 20
285255efc227SAndreas Gohr            $tags[0x013B] = array(2, 0); // Artist -> ASCII, Any
285355efc227SAndreas Gohr            $tags[0x013E] = array(5, 2); // WhitePoint -> RATIONAL, 2
285455efc227SAndreas Gohr            $tags[0x013F] = array(5, 6); // PrimaryChromaticities -> RATIONAL, 6
285555efc227SAndreas Gohr            $tags[0x0211] = array(5, 3); // YCbCrCoefficients -> RATIONAL, 3
285655efc227SAndreas Gohr            $tags[0x0212] = array(3, 2); // YCbCrSubSampling -> SHORT, 2
285755efc227SAndreas Gohr            $tags[0x0213] = array(3, 1); // YCbCrPositioning -> SHORT, 1
285855efc227SAndreas Gohr            $tags[0x0214] = array(5, 6); // ReferenceBlackWhite -> RATIONAL, 6
285955efc227SAndreas Gohr            $tags[0x8298] = array(2, 0); // Copyright -> ASCII, Any
286055efc227SAndreas Gohr            $tags[0x8769] = array(4, 1); // ExifIFDOffset -> LONG, 1
286155efc227SAndreas Gohr            $tags[0x8825] = array(4, 1); // GPSIFDOffset -> LONG, 1
286255efc227SAndreas Gohr        }
286355efc227SAndreas Gohr        if ($mode == 'ifd1') {
286455efc227SAndreas Gohr            $tags[0x00FE] = array(4, 1); // TIFFNewSubfileType -> LONG, 1
286555efc227SAndreas Gohr            $tags[0x00FF] = array(3, 1); // TIFFSubfileType -> SHORT, 1
286655efc227SAndreas Gohr            $tags[0x0100] = array(4, 1); // TIFFImageWidth -> LONG (or SHORT), 1
286755efc227SAndreas Gohr            $tags[0x0101] = array(4, 1); // TIFFImageHeight -> LONG (or SHORT), 1
286855efc227SAndreas Gohr            $tags[0x0102] = array(3, 3); // TIFFBitsPerSample -> SHORT, 3
286955efc227SAndreas Gohr            $tags[0x0103] = array(3, 1); // TIFFCompression -> SHORT, 1
287055efc227SAndreas Gohr            $tags[0x0106] = array(3, 1); // TIFFPhotometricInterpretation -> SHORT, 1
287155efc227SAndreas Gohr            $tags[0x0107] = array(3, 1); // TIFFThreshholding -> SHORT, 1
287255efc227SAndreas Gohr            $tags[0x0108] = array(3, 1); // TIFFCellWidth -> SHORT, 1
287355efc227SAndreas Gohr            $tags[0x0109] = array(3, 1); // TIFFCellLength -> SHORT, 1
287455efc227SAndreas Gohr            $tags[0x010A] = array(3, 1); // TIFFFillOrder -> SHORT, 1
287555efc227SAndreas Gohr            $tags[0x010E] = array(2, 0); // TIFFImageDescription -> ASCII, Any
287655efc227SAndreas Gohr            $tags[0x010F] = array(2, 0); // TIFFMake -> ASCII, Any
287755efc227SAndreas Gohr            $tags[0x0110] = array(2, 0); // TIFFModel -> ASCII, Any
287855efc227SAndreas Gohr            $tags[0x0111] = array(4, 0); // TIFFStripOffsets -> LONG (or SHORT), Any (one per strip)
287955efc227SAndreas Gohr            $tags[0x0112] = array(3, 1); // TIFFOrientation -> SHORT, 1
288055efc227SAndreas Gohr            $tags[0x0115] = array(3, 1); // TIFFSamplesPerPixel -> SHORT, 1
288155efc227SAndreas Gohr            $tags[0x0116] = array(4, 1); // TIFFRowsPerStrip -> LONG (or SHORT), 1
288255efc227SAndreas Gohr            $tags[0x0117] = array(4, 0); // TIFFStripByteCounts -> LONG (or SHORT), Any (one per strip)
288355efc227SAndreas Gohr            $tags[0x0118] = array(3, 0); // TIFFMinSampleValue -> SHORT, Any (SamplesPerPixel)
288455efc227SAndreas Gohr            $tags[0x0119] = array(3, 0); // TIFFMaxSampleValue -> SHORT, Any (SamplesPerPixel)
288555efc227SAndreas Gohr            $tags[0x011A] = array(5, 1); // TIFFXResolution -> RATIONAL, 1
288655efc227SAndreas Gohr            $tags[0x011B] = array(5, 1); // TIFFYResolution -> RATIONAL, 1
288755efc227SAndreas Gohr            $tags[0x011C] = array(3, 1); // TIFFPlanarConfiguration -> SHORT, 1
288855efc227SAndreas Gohr            $tags[0x0122] = array(3, 1); // TIFFGrayResponseUnit -> SHORT, 1
288955efc227SAndreas Gohr            $tags[0x0123] = array(3, 0); // TIFFGrayResponseCurve -> SHORT, Any (2^BitsPerSample)
289055efc227SAndreas Gohr            $tags[0x0128] = array(3, 1); // TIFFResolutionUnit -> SHORT, 1
289155efc227SAndreas Gohr            $tags[0x0131] = array(2, 0); // TIFFSoftware -> ASCII, Any
289255efc227SAndreas Gohr            $tags[0x0132] = array(2, 20); // TIFFDateTime -> ASCII, 20
289355efc227SAndreas Gohr            $tags[0x013B] = array(2, 0); // TIFFArtist -> ASCII, Any
289455efc227SAndreas Gohr            $tags[0x013C] = array(2, 0); // TIFFHostComputer -> ASCII, Any
289555efc227SAndreas Gohr            $tags[0x0140] = array(3, 0); // TIFFColorMap -> SHORT, Any (3 * 2^BitsPerSample)
289655efc227SAndreas Gohr            $tags[0x0152] = array(3, 0); // TIFFExtraSamples -> SHORT, Any (SamplesPerPixel - 3)
289755efc227SAndreas Gohr            $tags[0x0201] = array(4, 1); // TIFFJFIFOffset -> LONG, 1
289855efc227SAndreas Gohr            $tags[0x0202] = array(4, 1); // TIFFJFIFLength -> LONG, 1
289955efc227SAndreas Gohr            $tags[0x0211] = array(5, 3); // TIFFYCbCrCoefficients -> RATIONAL, 3
290055efc227SAndreas Gohr            $tags[0x0212] = array(3, 2); // TIFFYCbCrSubSampling -> SHORT, 2
290155efc227SAndreas Gohr            $tags[0x0213] = array(3, 1); // TIFFYCbCrPositioning -> SHORT, 1
290255efc227SAndreas Gohr            $tags[0x0214] = array(5, 6); // TIFFReferenceBlackWhite -> RATIONAL, 6
290355efc227SAndreas Gohr            $tags[0x8298] = array(2, 0); // TIFFCopyright -> ASCII, Any
290455efc227SAndreas Gohr            $tags[0x9286] = array(2, 0); // TIFFUserComment -> ASCII, Any
29050b17fdc6SAndreas Gohr        } elseif ($mode == 'exif') {
290655efc227SAndreas Gohr            $tags[0x829A] = array(5, 1); // ExposureTime -> RATIONAL, 1
290755efc227SAndreas Gohr            $tags[0x829D] = array(5, 1); // FNumber -> RATIONAL, 1
290855efc227SAndreas Gohr            $tags[0x8822] = array(3, 1); // ExposureProgram -> SHORT, 1
290955efc227SAndreas Gohr            $tags[0x8824] = array(2, 0); // SpectralSensitivity -> ASCII, Any
291055efc227SAndreas Gohr            $tags[0x8827] = array(3, 0); // ISOSpeedRatings -> SHORT, Any
291155efc227SAndreas Gohr            $tags[0x8828] = array(7, 0); // OECF -> UNDEFINED, Any
291255efc227SAndreas Gohr            $tags[0x9000] = array(7, 4); // EXIFVersion -> UNDEFINED, 4
29135d00293dSMichael Aschauer            $tags[0x9003] = array(2, 20); // DateTimeOriginal -> ASCII, 20
29145d00293dSMichael Aschauer            $tags[0x9004] = array(2, 20); // DateTimeDigitized -> ASCII, 20
291555efc227SAndreas Gohr            $tags[0x9101] = array(7, 4); // ComponentsConfiguration -> UNDEFINED, 4
291655efc227SAndreas Gohr            $tags[0x9102] = array(5, 1); // CompressedBitsPerPixel -> RATIONAL, 1
291755efc227SAndreas Gohr            $tags[0x9201] = array(10, 1); // ShutterSpeedValue -> SRATIONAL, 1
291855efc227SAndreas Gohr            $tags[0x9202] = array(5, 1); // ApertureValue -> RATIONAL, 1
291955efc227SAndreas Gohr            $tags[0x9203] = array(10, 1); // BrightnessValue -> SRATIONAL, 1
292055efc227SAndreas Gohr            $tags[0x9204] = array(10, 1); // ExposureBiasValue -> SRATIONAL, 1
292155efc227SAndreas Gohr            $tags[0x9205] = array(5, 1); // MaxApertureValue -> RATIONAL, 1
292255efc227SAndreas Gohr            $tags[0x9206] = array(5, 1); // SubjectDistance -> RATIONAL, 1
292355efc227SAndreas Gohr            $tags[0x9207] = array(3, 1); // MeteringMode -> SHORT, 1
292455efc227SAndreas Gohr            $tags[0x9208] = array(3, 1); // LightSource -> SHORT, 1
292555efc227SAndreas Gohr            $tags[0x9209] = array(3, 1); // Flash -> SHORT, 1
292655efc227SAndreas Gohr            $tags[0x920A] = array(5, 1); // FocalLength -> RATIONAL, 1
292755efc227SAndreas Gohr            $tags[0x927C] = array(7, 0); // MakerNote -> UNDEFINED, Any
292855efc227SAndreas Gohr            $tags[0x9286] = array(7, 0); // UserComment -> UNDEFINED, Any
292955efc227SAndreas Gohr            $tags[0x9290] = array(2, 0); // SubSecTime -> ASCII, Any
293055efc227SAndreas Gohr            $tags[0x9291] = array(2, 0); // SubSecTimeOriginal -> ASCII, Any
293155efc227SAndreas Gohr            $tags[0x9292] = array(2, 0); // SubSecTimeDigitized -> ASCII, Any
293255efc227SAndreas Gohr            $tags[0xA000] = array(7, 4); // FlashPixVersion -> UNDEFINED, 4
293355efc227SAndreas Gohr            $tags[0xA001] = array(3, 1); // ColorSpace -> SHORT, 1
293455efc227SAndreas Gohr            $tags[0xA002] = array(4, 1); // PixelXDimension -> LONG (or SHORT), 1
293555efc227SAndreas Gohr            $tags[0xA003] = array(4, 1); // PixelYDimension -> LONG (or SHORT), 1
293655efc227SAndreas Gohr            $tags[0xA004] = array(2, 13); // RelatedSoundFile -> ASCII, 13
293755efc227SAndreas Gohr            $tags[0xA005] = array(4, 1); // InteropIFDOffset -> LONG, 1
293855efc227SAndreas Gohr            $tags[0xA20B] = array(5, 1); // FlashEnergy -> RATIONAL, 1
293955efc227SAndreas Gohr            $tags[0xA20C] = array(7, 0); // SpatialFrequencyResponse -> UNDEFINED, Any
294055efc227SAndreas Gohr            $tags[0xA20E] = array(5, 1); // FocalPlaneXResolution -> RATIONAL, 1
294155efc227SAndreas Gohr            $tags[0xA20F] = array(5, 1); // FocalPlaneYResolution -> RATIONAL, 1
294255efc227SAndreas Gohr            $tags[0xA210] = array(3, 1); // FocalPlaneResolutionUnit -> SHORT, 1
294355efc227SAndreas Gohr            $tags[0xA214] = array(3, 2); // SubjectLocation -> SHORT, 2
294455efc227SAndreas Gohr            $tags[0xA215] = array(5, 1); // ExposureIndex -> RATIONAL, 1
294555efc227SAndreas Gohr            $tags[0xA217] = array(3, 1); // SensingMethod -> SHORT, 1
294655efc227SAndreas Gohr            $tags[0xA300] = array(7, 1); // FileSource -> UNDEFINED, 1
294755efc227SAndreas Gohr            $tags[0xA301] = array(7, 1); // SceneType -> UNDEFINED, 1
294855efc227SAndreas Gohr            $tags[0xA302] = array(7, 0); // CFAPattern -> UNDEFINED, Any
29490b17fdc6SAndreas Gohr        } elseif ($mode == 'interop') {
295055efc227SAndreas Gohr            $tags[0x0001] = array(2, 0); // InteroperabilityIndex -> ASCII, Any
295155efc227SAndreas Gohr            $tags[0x0002] = array(7, 4); // InteroperabilityVersion -> UNKNOWN, 4
295255efc227SAndreas Gohr            $tags[0x1000] = array(2, 0); // RelatedImageFileFormat -> ASCII, Any
295355efc227SAndreas Gohr            $tags[0x1001] = array(4, 1); // RelatedImageWidth -> LONG (or SHORT), 1
295455efc227SAndreas Gohr            $tags[0x1002] = array(4, 1); // RelatedImageLength -> LONG (or SHORT), 1
29550b17fdc6SAndreas Gohr        } elseif ($mode == 'gps') {
295655efc227SAndreas Gohr            $tags[0x0000] = array(1, 4); // GPSVersionID -> BYTE, 4
295755efc227SAndreas Gohr            $tags[0x0001] = array(2, 2); // GPSLatitudeRef -> ASCII, 2
295855efc227SAndreas Gohr            $tags[0x0002] = array(5, 3); // GPSLatitude -> RATIONAL, 3
295955efc227SAndreas Gohr            $tags[0x0003] = array(2, 2); // GPSLongitudeRef -> ASCII, 2
296055efc227SAndreas Gohr            $tags[0x0004] = array(5, 3); // GPSLongitude -> RATIONAL, 3
296155efc227SAndreas Gohr            $tags[0x0005] = array(2, 2); // GPSAltitudeRef -> ASCII, 2
296255efc227SAndreas Gohr            $tags[0x0006] = array(5, 1); // GPSAltitude -> RATIONAL, 1
296355efc227SAndreas Gohr            $tags[0x0007] = array(5, 3); // GPSTimeStamp -> RATIONAL, 3
296455efc227SAndreas Gohr            $tags[0x0008] = array(2, 0); // GPSSatellites -> ASCII, Any
296555efc227SAndreas Gohr            $tags[0x0009] = array(2, 2); // GPSStatus -> ASCII, 2
296655efc227SAndreas Gohr            $tags[0x000A] = array(2, 2); // GPSMeasureMode -> ASCII, 2
296755efc227SAndreas Gohr            $tags[0x000B] = array(5, 1); // GPSDOP -> RATIONAL, 1
296855efc227SAndreas Gohr            $tags[0x000C] = array(2, 2); // GPSSpeedRef -> ASCII, 2
296955efc227SAndreas Gohr            $tags[0x000D] = array(5, 1); // GPSSpeed -> RATIONAL, 1
297055efc227SAndreas Gohr            $tags[0x000E] = array(2, 2); // GPSTrackRef -> ASCII, 2
297155efc227SAndreas Gohr            $tags[0x000F] = array(5, 1); // GPSTrack -> RATIONAL, 1
297255efc227SAndreas Gohr            $tags[0x0010] = array(2, 2); // GPSImgDirectionRef -> ASCII, 2
297355efc227SAndreas Gohr            $tags[0x0011] = array(5, 1); // GPSImgDirection -> RATIONAL, 1
297455efc227SAndreas Gohr            $tags[0x0012] = array(2, 0); // GPSMapDatum -> ASCII, Any
297555efc227SAndreas Gohr            $tags[0x0013] = array(2, 2); // GPSDestLatitudeRef -> ASCII, 2
297655efc227SAndreas Gohr            $tags[0x0014] = array(5, 3); // GPSDestLatitude -> RATIONAL, 3
297755efc227SAndreas Gohr            $tags[0x0015] = array(2, 2); // GPSDestLongitudeRef -> ASCII, 2
297855efc227SAndreas Gohr            $tags[0x0016] = array(5, 3); // GPSDestLongitude -> RATIONAL, 3
297955efc227SAndreas Gohr            $tags[0x0017] = array(2, 2); // GPSDestBearingRef -> ASCII, 2
298055efc227SAndreas Gohr            $tags[0x0018] = array(5, 1); // GPSDestBearing -> RATIONAL, 1
298155efc227SAndreas Gohr            $tags[0x0019] = array(2, 2); // GPSDestDistanceRef -> ASCII, 2
298255efc227SAndreas Gohr            $tags[0x001A] = array(5, 1); // GPSDestDistance -> RATIONAL, 1
298355efc227SAndreas Gohr        }
298455efc227SAndreas Gohr
298555efc227SAndreas Gohr        return $tags;
298655efc227SAndreas Gohr    }
298755efc227SAndreas Gohr
298855efc227SAndreas Gohr    /*************************************************************/
29890b17fdc6SAndreas Gohr    function _exifNameTags($mode) {
299055efc227SAndreas Gohr        $tags = $this->_exifTagNames($mode);
299155efc227SAndreas Gohr        return $this->_names2Tags($tags);
299255efc227SAndreas Gohr    }
299355efc227SAndreas Gohr
299455efc227SAndreas Gohr    /*************************************************************/
29950b17fdc6SAndreas Gohr    function _iptcTagNames() {
299655efc227SAndreas Gohr        $tags = array();
299755efc227SAndreas Gohr        $tags[0x14] = 'SuplementalCategories';
299855efc227SAndreas Gohr        $tags[0x19] = 'Keywords';
299955efc227SAndreas Gohr        $tags[0x78] = 'Caption';
300055efc227SAndreas Gohr        $tags[0x7A] = 'CaptionWriter';
300155efc227SAndreas Gohr        $tags[0x69] = 'Headline';
300255efc227SAndreas Gohr        $tags[0x28] = 'SpecialInstructions';
300355efc227SAndreas Gohr        $tags[0x0F] = 'Category';
300455efc227SAndreas Gohr        $tags[0x50] = 'Byline';
300555efc227SAndreas Gohr        $tags[0x55] = 'BylineTitle';
300655efc227SAndreas Gohr        $tags[0x6E] = 'Credit';
300755efc227SAndreas Gohr        $tags[0x73] = 'Source';
300855efc227SAndreas Gohr        $tags[0x74] = 'CopyrightNotice';
300955efc227SAndreas Gohr        $tags[0x05] = 'ObjectName';
301055efc227SAndreas Gohr        $tags[0x5A] = 'City';
3011493531a8SJoe Lapp        $tags[0x5C] = 'Sublocation';
301255efc227SAndreas Gohr        $tags[0x5F] = 'ProvinceState';
301355efc227SAndreas Gohr        $tags[0x65] = 'CountryName';
301455efc227SAndreas Gohr        $tags[0x67] = 'OriginalTransmissionReference';
301555efc227SAndreas Gohr        $tags[0x37] = 'DateCreated';
301655efc227SAndreas Gohr        $tags[0x0A] = 'CopyrightFlag';
301755efc227SAndreas Gohr
301855efc227SAndreas Gohr        return $tags;
301955efc227SAndreas Gohr    }
302055efc227SAndreas Gohr
302155efc227SAndreas Gohr    /*************************************************************/
30220b17fdc6SAndreas Gohr    function & _iptcNameTags() {
302355efc227SAndreas Gohr        $tags = $this->_iptcTagNames();
302455efc227SAndreas Gohr        return $this->_names2Tags($tags);
302555efc227SAndreas Gohr    }
302655efc227SAndreas Gohr
302755efc227SAndreas Gohr    /*************************************************************/
30280b17fdc6SAndreas Gohr    function _names2Tags($tags2Names) {
302955efc227SAndreas Gohr        $names2Tags = array();
30309e491c01SAndreas Gohr
30319e491c01SAndreas Gohr        foreach($tags2Names as $tag => $name) {
303255efc227SAndreas Gohr            $names2Tags[$name] = $tag;
303355efc227SAndreas Gohr        }
303455efc227SAndreas Gohr
303555efc227SAndreas Gohr        return $names2Tags;
303655efc227SAndreas Gohr    }
303755efc227SAndreas Gohr
303855efc227SAndreas Gohr    /*************************************************************/
3039276820f7SScrutinizer Auto-Fixer
3040276820f7SScrutinizer Auto-Fixer    /**
3041f50a239bSTakamura     * @param $data
3042276820f7SScrutinizer Auto-Fixer     * @param integer $pos
3043f50a239bSTakamura     *
3044f50a239bSTakamura     * @return int
3045276820f7SScrutinizer Auto-Fixer     */
30460b17fdc6SAndreas Gohr    function _getByte(&$data, $pos) {
3047c9c56f8aSasivery        if (!isset($data[$pos])) {
3048c9c56f8aSasivery            throw new Exception("Requested byte at ".$pos.". Reading outside of file's boundaries.");
3049c9c56f8aSasivery        }
3050c9c56f8aSasivery
30512401f18dSSyntaxseed        return ord($data[$pos]);
305255efc227SAndreas Gohr    }
305355efc227SAndreas Gohr
305455efc227SAndreas Gohr    /*************************************************************/
3055276820f7SScrutinizer Auto-Fixer
3056276820f7SScrutinizer Auto-Fixer    /**
3057f50a239bSTakamura     * @param mixed $data
3058276820f7SScrutinizer Auto-Fixer     * @param integer $pos
3059f50a239bSTakamura     *
3060f50a239bSTakamura     * @param mixed $val
3061f50a239bSTakamura     *
3062f50a239bSTakamura     * @return int
3063276820f7SScrutinizer Auto-Fixer     */
30640b17fdc6SAndreas Gohr    function _putByte(&$data, $pos, $val) {
306555efc227SAndreas Gohr        $val = intval($val);
306655efc227SAndreas Gohr
30672401f18dSSyntaxseed        $data[$pos] = chr($val);
306855efc227SAndreas Gohr
306955efc227SAndreas Gohr        return $pos + 1;
307055efc227SAndreas Gohr    }
307155efc227SAndreas Gohr
307255efc227SAndreas Gohr    /*************************************************************/
30730b17fdc6SAndreas Gohr    function _getShort(&$data, $pos, $bigEndian = true) {
3074c9c56f8aSasivery        if (!isset($data[$pos]) || !isset($data[$pos + 1])) {
3075c9c56f8aSasivery            throw new Exception("Requested short at ".$pos.". Reading outside of file's boundaries.");
3076c9c56f8aSasivery        }
3077c9c56f8aSasivery
307855efc227SAndreas Gohr        if ($bigEndian) {
30792401f18dSSyntaxseed            return (ord($data[$pos]) << 8)
30802401f18dSSyntaxseed                + ord($data[$pos + 1]);
30810b17fdc6SAndreas Gohr        } else {
30822401f18dSSyntaxseed            return ord($data[$pos])
30832401f18dSSyntaxseed                + (ord($data[$pos + 1]) << 8);
308455efc227SAndreas Gohr        }
308555efc227SAndreas Gohr    }
308655efc227SAndreas Gohr
308755efc227SAndreas Gohr    /*************************************************************/
30880b17fdc6SAndreas Gohr    function _putShort(&$data, $pos = 0, $val = 0, $bigEndian = true) {
308955efc227SAndreas Gohr        $val = intval($val);
309055efc227SAndreas Gohr
309155efc227SAndreas Gohr        if ($bigEndian) {
30922401f18dSSyntaxseed            $data[$pos + 0] = chr(($val & 0x0000FF00) >> 8);
30932401f18dSSyntaxseed            $data[$pos + 1] = chr(($val & 0x000000FF) >> 0);
30940b17fdc6SAndreas Gohr        } else {
30952401f18dSSyntaxseed            $data[$pos + 0] = chr(($val & 0x00FF) >> 0);
30962401f18dSSyntaxseed            $data[$pos + 1] = chr(($val & 0xFF00) >> 8);
309755efc227SAndreas Gohr        }
309855efc227SAndreas Gohr
309955efc227SAndreas Gohr        return $pos + 2;
310055efc227SAndreas Gohr    }
310155efc227SAndreas Gohr
310255efc227SAndreas Gohr    /*************************************************************/
3103276820f7SScrutinizer Auto-Fixer
3104276820f7SScrutinizer Auto-Fixer    /**
3105f50a239bSTakamura     * @param mixed $data
3106276820f7SScrutinizer Auto-Fixer     * @param integer $pos
3107f50a239bSTakamura     *
3108f50a239bSTakamura     * @param bool $bigEndian
3109f50a239bSTakamura     *
3110f50a239bSTakamura     * @return int
3111276820f7SScrutinizer Auto-Fixer     */
31120b17fdc6SAndreas Gohr    function _getLong(&$data, $pos, $bigEndian = true) {
3113c9c56f8aSasivery        // Assume that if the start and end bytes are defined, the bytes inbetween are defined as well.
3114c9c56f8aSasivery        if (!isset($data[$pos]) || !isset($data[$pos + 3])){
3115c9c56f8aSasivery            throw new Exception("Requested long at ".$pos.". Reading outside of file's boundaries.");
3116c9c56f8aSasivery        }
311755efc227SAndreas Gohr        if ($bigEndian) {
31182401f18dSSyntaxseed            return (ord($data[$pos]) << 24)
31192401f18dSSyntaxseed                + (ord($data[$pos + 1]) << 16)
31202401f18dSSyntaxseed                + (ord($data[$pos + 2]) << 8)
31212401f18dSSyntaxseed                + ord($data[$pos + 3]);
31220b17fdc6SAndreas Gohr        } else {
31232401f18dSSyntaxseed            return ord($data[$pos])
31242401f18dSSyntaxseed                + (ord($data[$pos + 1]) << 8)
31252401f18dSSyntaxseed                + (ord($data[$pos + 2]) << 16)
31262401f18dSSyntaxseed                + (ord($data[$pos + 3]) << 24);
312755efc227SAndreas Gohr        }
312855efc227SAndreas Gohr    }
312955efc227SAndreas Gohr
313055efc227SAndreas Gohr    /*************************************************************/
3131276820f7SScrutinizer Auto-Fixer
3132276820f7SScrutinizer Auto-Fixer    /**
3133f50a239bSTakamura     * @param mixed $data
3134276820f7SScrutinizer Auto-Fixer     * @param integer $pos
3135f50a239bSTakamura     *
3136f50a239bSTakamura     * @param mixed $val
3137f50a239bSTakamura     * @param bool $bigEndian
3138f50a239bSTakamura     *
3139f50a239bSTakamura     * @return int
3140276820f7SScrutinizer Auto-Fixer     */
31410b17fdc6SAndreas Gohr    function _putLong(&$data, $pos, $val, $bigEndian = true) {
314255efc227SAndreas Gohr        $val = intval($val);
314355efc227SAndreas Gohr
314455efc227SAndreas Gohr        if ($bigEndian) {
31452401f18dSSyntaxseed            $data[$pos + 0] = chr(($val & 0xFF000000) >> 24);
31462401f18dSSyntaxseed            $data[$pos + 1] = chr(($val & 0x00FF0000) >> 16);
31472401f18dSSyntaxseed            $data[$pos + 2] = chr(($val & 0x0000FF00) >> 8);
31482401f18dSSyntaxseed            $data[$pos + 3] = chr(($val & 0x000000FF) >> 0);
31490b17fdc6SAndreas Gohr        } else {
31502401f18dSSyntaxseed            $data[$pos + 0] = chr(($val & 0x000000FF) >> 0);
31512401f18dSSyntaxseed            $data[$pos + 1] = chr(($val & 0x0000FF00) >> 8);
31522401f18dSSyntaxseed            $data[$pos + 2] = chr(($val & 0x00FF0000) >> 16);
31532401f18dSSyntaxseed            $data[$pos + 3] = chr(($val & 0xFF000000) >> 24);
315455efc227SAndreas Gohr        }
315555efc227SAndreas Gohr
315655efc227SAndreas Gohr        return $pos + 4;
315755efc227SAndreas Gohr    }
315855efc227SAndreas Gohr
315955efc227SAndreas Gohr    /*************************************************************/
31600b17fdc6SAndreas Gohr    function & _getNullString(&$data, $pos) {
316155efc227SAndreas Gohr        $str = '';
316255efc227SAndreas Gohr        $max = strlen($data);
316355efc227SAndreas Gohr
316455efc227SAndreas Gohr        while ($pos < $max) {
3165c9c56f8aSasivery            if (!isset($data[$pos])) {
3166c9c56f8aSasivery                throw new Exception("Requested null-terminated string at offset ".$pos.". File terminated before the null-byte.");
3167c9c56f8aSasivery            }
31682401f18dSSyntaxseed            if (ord($data[$pos]) == 0) {
316955efc227SAndreas Gohr                return $str;
31700b17fdc6SAndreas Gohr            } else {
31712401f18dSSyntaxseed                $str .= $data[$pos];
317255efc227SAndreas Gohr            }
317355efc227SAndreas Gohr            $pos++;
317455efc227SAndreas Gohr        }
317555efc227SAndreas Gohr
317655efc227SAndreas Gohr        return $str;
317755efc227SAndreas Gohr    }
317855efc227SAndreas Gohr
317955efc227SAndreas Gohr    /*************************************************************/
31800b17fdc6SAndreas Gohr    function & _getFixedString(&$data, $pos, $length = -1) {
318155efc227SAndreas Gohr        if ($length == -1) {
318255efc227SAndreas Gohr            $length = strlen($data) - $pos;
318355efc227SAndreas Gohr        }
318455efc227SAndreas Gohr
318517dd401eSChristopher Smith        $rv = substr($data, $pos, $length);
318636fb33ceSasivery        if (strlen($rv) != $length) {
3187d40e7d0eSAndreas Gohr            throw new ErrorException(sprintf(
3188d40e7d0eSAndreas Gohr                "JPEGMeta failed parsing image metadata of %s. Got %d instead of %d bytes at offset %d.",
3189d40e7d0eSAndreas Gohr                $this->_fileName, strlen($rv), $length, $pos
3190d40e7d0eSAndreas Gohr            ), 0, E_WARNING);
319136fb33ceSasivery        }
319217dd401eSChristopher Smith        return $rv;
319355efc227SAndreas Gohr    }
319455efc227SAndreas Gohr
319555efc227SAndreas Gohr    /*************************************************************/
31960b17fdc6SAndreas Gohr    function _putString(&$data, $pos, &$str) {
319755efc227SAndreas Gohr        $len = strlen($str);
319855efc227SAndreas Gohr        for ($i = 0; $i < $len; $i++) {
31992401f18dSSyntaxseed            $data[$pos + $i] = $str[$i];
320055efc227SAndreas Gohr        }
320155efc227SAndreas Gohr
320255efc227SAndreas Gohr        return $pos + $len;
320355efc227SAndreas Gohr    }
320455efc227SAndreas Gohr
320555efc227SAndreas Gohr    /*************************************************************/
32060b17fdc6SAndreas Gohr    function _hexDump(&$data, $start = 0, $length = -1) {
320755efc227SAndreas Gohr        if (($length == -1) || (($length + $start) > strlen($data))) {
320855efc227SAndreas Gohr            $end = strlen($data);
32090b17fdc6SAndreas Gohr        } else {
321055efc227SAndreas Gohr            $end = $start + $length;
321155efc227SAndreas Gohr        }
321255efc227SAndreas Gohr
321355efc227SAndreas Gohr        $ascii = '';
321455efc227SAndreas Gohr        $count = 0;
321555efc227SAndreas Gohr
321655efc227SAndreas Gohr        echo "<tt>\n";
321755efc227SAndreas Gohr
321855efc227SAndreas Gohr        while ($start < $end) {
321955efc227SAndreas Gohr            if (($count % 16) == 0) {
322055efc227SAndreas Gohr                echo sprintf('%04d', $count) . ': ';
322155efc227SAndreas Gohr            }
322255efc227SAndreas Gohr
32232401f18dSSyntaxseed            $c = ord($data[$start]);
322455efc227SAndreas Gohr            $count++;
322555efc227SAndreas Gohr            $start++;
322655efc227SAndreas Gohr
322755efc227SAndreas Gohr            $aux = dechex($c);
322855efc227SAndreas Gohr            if (strlen($aux) == 1)
322955efc227SAndreas Gohr                echo '0';
323055efc227SAndreas Gohr            echo $aux . ' ';
323155efc227SAndreas Gohr
323255efc227SAndreas Gohr            if ($c == 60)
323355efc227SAndreas Gohr                $ascii .= '&lt;';
323455efc227SAndreas Gohr            elseif ($c == 62)
323555efc227SAndreas Gohr                $ascii .= '&gt;';
323655efc227SAndreas Gohr            elseif ($c == 32)
3237e260f93bSAnika Henke                $ascii .= '&#160;';
323855efc227SAndreas Gohr            elseif ($c > 32)
323955efc227SAndreas Gohr                $ascii .= chr($c);
324055efc227SAndreas Gohr            else
324155efc227SAndreas Gohr                $ascii .= '.';
324255efc227SAndreas Gohr
324355efc227SAndreas Gohr            if (($count % 4) == 0) {
324455efc227SAndreas Gohr                echo ' - ';
324555efc227SAndreas Gohr            }
324655efc227SAndreas Gohr
324755efc227SAndreas Gohr            if (($count % 16) == 0) {
324855efc227SAndreas Gohr                echo ': ' . $ascii . "<br>\n";
324955efc227SAndreas Gohr                $ascii = '';
325055efc227SAndreas Gohr            }
325155efc227SAndreas Gohr        }
325255efc227SAndreas Gohr
325355efc227SAndreas Gohr        if ($ascii != '') {
325455efc227SAndreas Gohr            while (($count % 16) != 0) {
325555efc227SAndreas Gohr                echo '-- ';
325655efc227SAndreas Gohr                $count++;
325755efc227SAndreas Gohr                if (($count % 4) == 0) {
325855efc227SAndreas Gohr                    echo ' - ';
325955efc227SAndreas Gohr                }
326055efc227SAndreas Gohr            }
326155efc227SAndreas Gohr            echo ': ' . $ascii . "<br>\n";
326255efc227SAndreas Gohr        }
326355efc227SAndreas Gohr
326455efc227SAndreas Gohr        echo "</tt>\n";
326555efc227SAndreas Gohr    }
326655efc227SAndreas Gohr
326755efc227SAndreas Gohr    /*****************************************************************/
326855efc227SAndreas Gohr}
326955efc227SAndreas Gohr
327055efc227SAndreas Gohr/* vim: set expandtab tabstop=4 shiftwidth=4: */
3271