xref: /dokuwiki/inc/JpegMeta.php (revision ed3655c4c7f9692340f7b54054d35f24a0cc3f68)
155efc227SAndreas Gohr<?php
255efc227SAndreas Gohr/**
355efc227SAndreas Gohr * JPEG metadata reader/writer
455efc227SAndreas Gohr *
555efc227SAndreas Gohr * @license    PHP license 2.0 (http://www.php.net/license/2_02.txt)
636df6fa3SAndreas Gohr * @link       http://www.zonageek.com/software/php/jpeg/index.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
1355efc227SAndreas Gohr// This class is a modified and enhanced version of the JPEG class by
1455efc227SAndreas Gohr// Sebastian Delmont. Original Copyright notice follows:
1555efc227SAndreas Gohr//
1655efc227SAndreas Gohr// +----------------------------------------------------------------------+
1755efc227SAndreas Gohr// | PHP version 4.0                                                      |
1855efc227SAndreas Gohr// +----------------------------------------------------------------------+
1955efc227SAndreas Gohr// | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group             |
2055efc227SAndreas Gohr// +----------------------------------------------------------------------+
2155efc227SAndreas Gohr// | This source file is subject to version 2.0 of the PHP license,       |
2255efc227SAndreas Gohr// | that is bundled with this package in the file LICENSE, and is        |
2355efc227SAndreas Gohr// | available at through the world-wide-web at                           |
2455efc227SAndreas Gohr// | http://www.php.net/license/2_02.txt.                                 |
2555efc227SAndreas Gohr// | If you did not receive a copy of the PHP license and are unable to   |
2655efc227SAndreas Gohr// | obtain it through the world-wide-web, please send a note to          |
2755efc227SAndreas Gohr// | license@php.net so we can mail you a copy immediately.               |
2855efc227SAndreas Gohr// +----------------------------------------------------------------------+
2955efc227SAndreas Gohr// | Authors: Sebastian Delmont <sdelmont@zonageek.com>                   |
3055efc227SAndreas Gohr// +----------------------------------------------------------------------+
3155efc227SAndreas Gohr
3255efc227SAndreas Gohrclass JpegMeta
3355efc227SAndreas Gohr{
3455efc227SAndreas Gohr    var $_fileName;
3555efc227SAndreas Gohr    var $_fp = null;
3655efc227SAndreas Gohr    var $_type = 'unknown';
3755efc227SAndreas Gohr
3855efc227SAndreas Gohr    var $_markers;
3955efc227SAndreas Gohr    var $_info;
4055efc227SAndreas Gohr
4155efc227SAndreas Gohr
4255efc227SAndreas Gohr    /**
4355efc227SAndreas Gohr     * Constructor
4455efc227SAndreas Gohr     *
4555efc227SAndreas Gohr     * @author Sebastian Delmont <sdelmont@zonageek.com>
4655efc227SAndreas Gohr     */
4755efc227SAndreas Gohr    function JpegMeta($fileName)
4855efc227SAndreas Gohr    {
4955efc227SAndreas Gohr
5055efc227SAndreas Gohr        $this->_fileName = $fileName;
5155efc227SAndreas Gohr
5255efc227SAndreas Gohr        $this->_fp = null;
5355efc227SAndreas Gohr        $this->_type = 'unknown';
5455efc227SAndreas Gohr
5555efc227SAndreas Gohr        unset($this->_info);
5655efc227SAndreas Gohr        unset($this->_markers);
5755efc227SAndreas Gohr    }
5855efc227SAndreas Gohr
5955efc227SAndreas Gohr    /**
6055efc227SAndreas Gohr     * Returns all gathered info as multidim array
6155efc227SAndreas Gohr     *
6255efc227SAndreas Gohr     * @author Sebastian Delmont <sdelmont@zonageek.com>
6355efc227SAndreas Gohr     */
6455efc227SAndreas Gohr    function & getRawInfo()
6555efc227SAndreas Gohr    {
6655efc227SAndreas Gohr        $this->_parseAll();
6755efc227SAndreas Gohr
6855efc227SAndreas Gohr        if ($this->_markers == null) {
6955efc227SAndreas Gohr            return false;
7055efc227SAndreas Gohr        }
7155efc227SAndreas Gohr
7255efc227SAndreas Gohr        return $this->_info;
7355efc227SAndreas Gohr    }
7455efc227SAndreas Gohr
7555efc227SAndreas Gohr    /**
7655efc227SAndreas Gohr     * Returns basic image info
7755efc227SAndreas Gohr     *
7855efc227SAndreas Gohr     * @author Sebastian Delmont <sdelmont@zonageek.com>
7955efc227SAndreas Gohr     */
8055efc227SAndreas Gohr    function & getBasicInfo()
8155efc227SAndreas Gohr    {
8255efc227SAndreas Gohr        $this->_parseAll();
8355efc227SAndreas Gohr
8455efc227SAndreas Gohr        $info = array();
8555efc227SAndreas Gohr
8655efc227SAndreas Gohr        if ($this->_markers == null) {
8755efc227SAndreas Gohr            return false;
8855efc227SAndreas Gohr        }
8955efc227SAndreas Gohr
9055efc227SAndreas Gohr        $info['Name'] = $this->_info['file']['Name'];
9155efc227SAndreas Gohr        if (isset($this->_info['file']['Url'])) {
9255efc227SAndreas Gohr            $info['Url'] = $this->_info['file']['Url'];
9355efc227SAndreas Gohr            $info['NiceSize'] = "???KB";
9455efc227SAndreas Gohr        }
9555efc227SAndreas Gohr        else {
9655efc227SAndreas Gohr            $info['Size'] = $this->_info['file']['Size'];
9755efc227SAndreas Gohr            $info['NiceSize'] = $this->_info['file']['NiceSize'];
9855efc227SAndreas Gohr        }
9955efc227SAndreas Gohr
10055efc227SAndreas Gohr        if (@isset($this->_info['sof']['Format'])) {
10155efc227SAndreas Gohr            $info['Format'] = $this->_info['sof']['Format'] . " JPEG";
10255efc227SAndreas Gohr        }
10355efc227SAndreas Gohr        else {
10455efc227SAndreas Gohr            $info['Format'] = $this->_info['sof']['Format'] . " JPEG";
10555efc227SAndreas Gohr        }
10655efc227SAndreas Gohr
10755efc227SAndreas Gohr        if (@isset($this->_info['sof']['ColorChannels'])) {
10855efc227SAndreas Gohr            $info['ColorMode'] = ($this->_info['sof']['ColorChannels'] > 1) ? "Color" : "B&W";
10955efc227SAndreas Gohr        }
11055efc227SAndreas Gohr
11155efc227SAndreas Gohr        $info['Width'] = $this->getWidth();
11255efc227SAndreas Gohr        $info['Height'] = $this->getHeight();
11355efc227SAndreas Gohr        $info['DimStr'] = $this->getDimStr();
11455efc227SAndreas Gohr
11555efc227SAndreas Gohr        $dates = $this->getDates();
11655efc227SAndreas Gohr
11755efc227SAndreas Gohr        $info['DateTime'] = $dates['EarliestTime'];
11855efc227SAndreas Gohr        $info['DateTimeStr'] = $dates['EarliestTimeStr'];
11955efc227SAndreas Gohr
12055efc227SAndreas Gohr        $info['HasThumbnail'] = $this->hasThumbnail();
12155efc227SAndreas Gohr
12255efc227SAndreas Gohr        return $info;
12355efc227SAndreas Gohr    }
12455efc227SAndreas Gohr
12555efc227SAndreas Gohr
12655efc227SAndreas Gohr    /**
12755efc227SAndreas Gohr     * Convinience function to access nearly all available Data
12855efc227SAndreas Gohr     * through one function
12955efc227SAndreas Gohr     *
13055efc227SAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
13155efc227SAndreas Gohr     */
13255efc227SAndreas Gohr    function getField($fields)
13355efc227SAndreas Gohr    {
13455efc227SAndreas Gohr        if(!is_array($fields)) $fields = array($fields);
13555efc227SAndreas Gohr        $info = false;
13655efc227SAndreas Gohr        foreach($fields as $field){
13755efc227SAndreas Gohr            if(strtolower(substr($field,0,5)) == 'iptc.'){
13855efc227SAndreas Gohr                $info = $this->getIPTCField(substr($field,5));
13955efc227SAndreas Gohr            }elseif(strtolower(substr($field,0,5)) == 'exif.'){
14055efc227SAndreas Gohr                $info = $this->getExifField(substr($field,5));
141431c7fc8Shakan.sandell            }elseif(strtolower(substr($field,0,4)) == 'xmp.'){
142431c7fc8Shakan.sandell                $info = $this->getXmpField(substr($field,4));
14355efc227SAndreas Gohr            }elseif(strtolower(substr($field,0,5)) == 'file.'){
14455efc227SAndreas Gohr                $info = $this->getFileField(substr($field,5));
14555efc227SAndreas Gohr            }elseif(strtolower(substr($field,0,5)) == 'date.'){
14655efc227SAndreas Gohr                $info = $this->getDateField(substr($field,5));
14755efc227SAndreas Gohr            }elseif(strtolower($field) == 'simple.camera'){
14855efc227SAndreas Gohr                $info = $this->getCamera();
14955efc227SAndreas Gohr            }elseif(strtolower($field) == 'simple.raw'){
15055efc227SAndreas Gohr                return $this->getRawInfo();
15155efc227SAndreas Gohr            }elseif(strtolower($field) == 'simple.title'){
15255efc227SAndreas Gohr                $info = $this->getTitle();
1536db72d46SJoe Lapp            }elseif(strtolower($field) == 'simple.shutterspeed'){
1546db72d46SJoe Lapp                $info = $this->getShutterSpeed();
15555efc227SAndreas Gohr            }else{
15655efc227SAndreas Gohr                $info = $this->getExifField($field);
15755efc227SAndreas Gohr            }
15855efc227SAndreas Gohr            if($info != false) break;
15955efc227SAndreas Gohr        }
16055efc227SAndreas Gohr
16155efc227SAndreas Gohr        if($info === false)  $info = $alt;
16255efc227SAndreas Gohr        if(is_array($info)){
16355efc227SAndreas Gohr            if(isset($info['val'])){
16455efc227SAndreas Gohr                $info = $info['val'];
16555efc227SAndreas Gohr            }else{
16655efc227SAndreas Gohr                $info = join(', ',$info);
16755efc227SAndreas Gohr            }
16855efc227SAndreas Gohr        }
16955efc227SAndreas Gohr        return trim($info);
17055efc227SAndreas Gohr    }
17155efc227SAndreas Gohr
17255efc227SAndreas Gohr    /**
17336df6fa3SAndreas Gohr     * Convinience function to set nearly all available Data
17436df6fa3SAndreas Gohr     * through one function
17536df6fa3SAndreas Gohr     *
17636df6fa3SAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
17736df6fa3SAndreas Gohr     */
17836df6fa3SAndreas Gohr    function setField($field, $value)
17936df6fa3SAndreas Gohr    {
18036df6fa3SAndreas Gohr        if(strtolower(substr($field,0,5)) == 'iptc.'){
18136df6fa3SAndreas Gohr            return $this->setIPTCField(substr($field,5),$value);
18236df6fa3SAndreas Gohr        }elseif(strtolower(substr($field,0,5)) == 'exif.'){
18336df6fa3SAndreas Gohr            return $this->setExifField(substr($field,5),$value);
18436df6fa3SAndreas Gohr        }else{
18536df6fa3SAndreas Gohr            return $this->setExifField($field,$value);
18636df6fa3SAndreas Gohr        }
18736df6fa3SAndreas Gohr    }
18836df6fa3SAndreas Gohr
18936df6fa3SAndreas Gohr    /**
19036df6fa3SAndreas Gohr     * Convinience function to delete nearly all available Data
19136df6fa3SAndreas Gohr     * through one function
19236df6fa3SAndreas Gohr     *
19336df6fa3SAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
19436df6fa3SAndreas Gohr     */
19536df6fa3SAndreas Gohr    function deleteField($field)
19636df6fa3SAndreas Gohr    {
19736df6fa3SAndreas Gohr        if(strtolower(substr($field,0,5)) == 'iptc.'){
19836df6fa3SAndreas Gohr            return $this->deleteIPTCField(substr($field,5));
19936df6fa3SAndreas Gohr        }elseif(strtolower(substr($field,0,5)) == 'exif.'){
20036df6fa3SAndreas Gohr            return $this->deleteExifField(substr($field,5));
20136df6fa3SAndreas Gohr        }else{
20236df6fa3SAndreas Gohr            return $this->deleteExifField($field);
20336df6fa3SAndreas Gohr        }
20436df6fa3SAndreas Gohr    }
20536df6fa3SAndreas Gohr
20636df6fa3SAndreas Gohr    /**
20755efc227SAndreas Gohr     * Return a date field
20855efc227SAndreas Gohr     *
20955efc227SAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
21055efc227SAndreas Gohr     */
21155efc227SAndreas Gohr    function getDateField($field)
21255efc227SAndreas Gohr    {
21355efc227SAndreas Gohr        if (!isset($this->_info['dates'])) {
21455efc227SAndreas Gohr            $this->_info['dates'] = $this->getDates();
21555efc227SAndreas Gohr        }
21655efc227SAndreas Gohr
21755efc227SAndreas Gohr        if (isset($this->_info['dates'][$field])) {
21855efc227SAndreas Gohr            return $this->_info['dates'][$field];
21955efc227SAndreas Gohr        }
22055efc227SAndreas Gohr
22155efc227SAndreas Gohr        return false;
22255efc227SAndreas Gohr    }
22355efc227SAndreas Gohr
22455efc227SAndreas Gohr    /**
22555efc227SAndreas Gohr     * Return a file info field
22655efc227SAndreas Gohr     *
22755efc227SAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
22855efc227SAndreas Gohr     */
22955efc227SAndreas Gohr    function getFileField($field)
23055efc227SAndreas Gohr    {
23155efc227SAndreas Gohr        if (!isset($this->_info['file'])) {
23255efc227SAndreas Gohr            $this->_parseFileInfo();
23355efc227SAndreas Gohr        }
23455efc227SAndreas Gohr
23555efc227SAndreas Gohr        if (isset($this->_info['file'][$field])) {
23655efc227SAndreas Gohr            return $this->_info['file'][$field];
23755efc227SAndreas Gohr        }
23855efc227SAndreas Gohr
23955efc227SAndreas Gohr        return false;
24055efc227SAndreas Gohr    }
24155efc227SAndreas Gohr
24255efc227SAndreas Gohr    /**
24355efc227SAndreas Gohr     * Return the camera info (Maker and Model)
24455efc227SAndreas Gohr     *
24555efc227SAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
24655efc227SAndreas Gohr     * @todo   handle makernotes
24755efc227SAndreas Gohr     */
24855efc227SAndreas Gohr    function getCamera(){
24955efc227SAndreas Gohr        $make  = $this->getField(array('Exif.Make','Exif.TIFFMake'));
25055efc227SAndreas Gohr        $model = $this->getField(array('Exif.Model','Exif.TIFFModel'));
25155efc227SAndreas Gohr        $cam = trim("$make $model");
25255efc227SAndreas Gohr        if(empty($cam)) return false;
25355efc227SAndreas Gohr        return $cam;
25455efc227SAndreas Gohr    }
25555efc227SAndreas Gohr
25655efc227SAndreas Gohr    /**
2576db72d46SJoe Lapp     * Return shutter speed as a ratio
2586db72d46SJoe Lapp     *
2596db72d46SJoe Lapp     * @author Joe Lapp <joe.lapp@pobox.com>
2606db72d46SJoe Lapp     */
2616db72d46SJoe Lapp    function getShutterSpeed()
2626db72d46SJoe Lapp    {
2636db72d46SJoe Lapp        if (!isset($this->_info['exif'])) {
2646db72d46SJoe Lapp            $this->_parseMarkerExif();
2656db72d46SJoe Lapp        }
2666db72d46SJoe Lapp        if(!isset($this->_info['exif']['ExposureTime'])){
2676db72d46SJoe Lapp            return '';
2686db72d46SJoe Lapp        }
2696db72d46SJoe Lapp
2706db72d46SJoe Lapp        $field = $this->_info['exif']['ExposureTime'];
2716db72d46SJoe Lapp        if($field['den'] == 1) return $field['num'];
2726db72d46SJoe Lapp        return $field['num'].'/'.$field['den'];
2736db72d46SJoe Lapp    }
2746db72d46SJoe Lapp
2756db72d46SJoe Lapp    /**
27655efc227SAndreas Gohr     * Return an EXIF field
27755efc227SAndreas Gohr     *
27855efc227SAndreas Gohr     * @author Sebastian Delmont <sdelmont@zonageek.com>
27955efc227SAndreas Gohr     */
28055efc227SAndreas Gohr    function getExifField($field)
28155efc227SAndreas Gohr    {
28255efc227SAndreas Gohr        if (!isset($this->_info['exif'])) {
28355efc227SAndreas Gohr            $this->_parseMarkerExif();
28455efc227SAndreas Gohr        }
28555efc227SAndreas Gohr
28655efc227SAndreas Gohr        if ($this->_markers == null) {
28755efc227SAndreas Gohr            return false;
28855efc227SAndreas Gohr        }
28955efc227SAndreas Gohr
29055efc227SAndreas Gohr        if (isset($this->_info['exif'][$field])) {
29155efc227SAndreas Gohr            return $this->_info['exif'][$field];
29255efc227SAndreas Gohr        }
29355efc227SAndreas Gohr
29455efc227SAndreas Gohr        return false;
29555efc227SAndreas Gohr    }
29655efc227SAndreas Gohr
29755efc227SAndreas Gohr    /**
298431c7fc8Shakan.sandell     * Return an XMP field
299431c7fc8Shakan.sandell     *
300431c7fc8Shakan.sandell     * @author Hakan Sandell <hakan.sandell@mydata.se>
301431c7fc8Shakan.sandell     */
302431c7fc8Shakan.sandell    function getXmpField($field)
303431c7fc8Shakan.sandell    {
304431c7fc8Shakan.sandell        if (!isset($this->_info['xmp'])) {
305431c7fc8Shakan.sandell            $this->_parseMarkerXmp();
306431c7fc8Shakan.sandell        }
307431c7fc8Shakan.sandell
308431c7fc8Shakan.sandell        if ($this->_markers == null) {
309431c7fc8Shakan.sandell            return false;
310431c7fc8Shakan.sandell        }
311431c7fc8Shakan.sandell
312431c7fc8Shakan.sandell        if (isset($this->_info['xmp'][$field])) {
313431c7fc8Shakan.sandell            return $this->_info['xmp'][$field];
314431c7fc8Shakan.sandell        }
315431c7fc8Shakan.sandell
316431c7fc8Shakan.sandell        return false;
317431c7fc8Shakan.sandell    }
318431c7fc8Shakan.sandell
319431c7fc8Shakan.sandell    /**
32055efc227SAndreas Gohr     * Return an Adobe Field
32155efc227SAndreas Gohr     *
32255efc227SAndreas Gohr     * @author Sebastian Delmont <sdelmont@zonageek.com>
32355efc227SAndreas Gohr     */
32455efc227SAndreas Gohr    function getAdobeField($field)
32555efc227SAndreas Gohr    {
32655efc227SAndreas Gohr        if (!isset($this->_info['adobe'])) {
32755efc227SAndreas Gohr            $this->_parseMarkerAdobe();
32855efc227SAndreas Gohr        }
32955efc227SAndreas Gohr
33055efc227SAndreas Gohr        if ($this->_markers == null) {
33155efc227SAndreas Gohr            return false;
33255efc227SAndreas Gohr        }
33355efc227SAndreas Gohr
33455efc227SAndreas Gohr        if (isset($this->_info['adobe'][$field])) {
33555efc227SAndreas Gohr            return $this->_info['adobe'][$field];
33655efc227SAndreas Gohr        }
33755efc227SAndreas Gohr
33855efc227SAndreas Gohr        return false;
33955efc227SAndreas Gohr    }
34055efc227SAndreas Gohr
34155efc227SAndreas Gohr    /**
34255efc227SAndreas Gohr     * Return an IPTC field
34355efc227SAndreas Gohr     *
34455efc227SAndreas Gohr     * @author Sebastian Delmont <sdelmont@zonageek.com>
34555efc227SAndreas Gohr     */
34655efc227SAndreas Gohr    function getIPTCField($field)
34755efc227SAndreas Gohr    {
34855efc227SAndreas Gohr        if (!isset($this->_info['iptc'])) {
34955efc227SAndreas Gohr            $this->_parseMarkerAdobe();
35055efc227SAndreas Gohr        }
35155efc227SAndreas Gohr
35255efc227SAndreas Gohr        if ($this->_markers == null) {
35355efc227SAndreas Gohr            return false;
35455efc227SAndreas Gohr        }
35555efc227SAndreas Gohr
35655efc227SAndreas Gohr        if (isset($this->_info['iptc'][$field])) {
35755efc227SAndreas Gohr            return $this->_info['iptc'][$field];
35855efc227SAndreas Gohr        }
35955efc227SAndreas Gohr
36055efc227SAndreas Gohr        return false;
36155efc227SAndreas Gohr    }
36255efc227SAndreas Gohr
36355efc227SAndreas Gohr    /**
36455efc227SAndreas Gohr     * Set an EXIF field
36555efc227SAndreas Gohr     *
36655efc227SAndreas Gohr     * @author Sebastian Delmont <sdelmont@zonageek.com>
367b5a81756SJoe Lapp     * @author Joe Lapp <joe.lapp@pobox.com>
36855efc227SAndreas Gohr     */
36955efc227SAndreas Gohr    function setExifField($field, $value)
37055efc227SAndreas Gohr    {
37155efc227SAndreas Gohr        if (!isset($this->_info['exif'])) {
37255efc227SAndreas Gohr            $this->_parseMarkerExif();
37355efc227SAndreas Gohr        }
37455efc227SAndreas Gohr
37555efc227SAndreas Gohr        if ($this->_markers == null) {
37655efc227SAndreas Gohr            return false;
37755efc227SAndreas Gohr        }
37855efc227SAndreas Gohr
37955efc227SAndreas Gohr        if ($this->_info['exif'] == false) {
38055efc227SAndreas Gohr            $this->_info['exif'] = array();
38155efc227SAndreas Gohr        }
38255efc227SAndreas Gohr
383b5a81756SJoe Lapp        // make sure datetimes are in correct format
384b5a81756SJoe Lapp        if(strlen($field) >= 8 && strtolower(substr($field, 0, 8)) == 'datetime') {
385b5a81756SJoe Lapp            if(strlen($value) < 8 || $value{4} != ':' || $value{7} != ':') {
386b5a81756SJoe Lapp                $value = date('Y:m:d H:i:s', strtotime($value));
387b5a81756SJoe Lapp            }
388b5a81756SJoe Lapp        }
389b5a81756SJoe Lapp
39055efc227SAndreas Gohr        $this->_info['exif'][$field] = $value;
39155efc227SAndreas Gohr
39255efc227SAndreas Gohr        return true;
39355efc227SAndreas Gohr    }
39455efc227SAndreas Gohr
39555efc227SAndreas Gohr    /**
39655efc227SAndreas Gohr     * Set an Adobe Field
39755efc227SAndreas Gohr     *
39855efc227SAndreas Gohr     * @author Sebastian Delmont <sdelmont@zonageek.com>
39955efc227SAndreas Gohr     */
40055efc227SAndreas Gohr    function setAdobeField($field, $value)
40155efc227SAndreas Gohr    {
40255efc227SAndreas Gohr        if (!isset($this->_info['adobe'])) {
40355efc227SAndreas Gohr            $this->_parseMarkerAdobe();
40455efc227SAndreas Gohr        }
40555efc227SAndreas Gohr
40655efc227SAndreas Gohr        if ($this->_markers == null) {
40755efc227SAndreas Gohr            return false;
40855efc227SAndreas Gohr        }
40955efc227SAndreas Gohr
41055efc227SAndreas Gohr        if ($this->_info['adobe'] == false) {
41155efc227SAndreas Gohr            $this->_info['adobe'] = array();
41255efc227SAndreas Gohr        }
41355efc227SAndreas Gohr
41455efc227SAndreas Gohr        $this->_info['adobe'][$field] = $value;
41555efc227SAndreas Gohr
41655efc227SAndreas Gohr        return true;
41755efc227SAndreas Gohr    }
41855efc227SAndreas Gohr
41955efc227SAndreas Gohr    /**
42023a34783SAndreas Gohr     * Calculates the multiplier needed to resize the image to the given
42123a34783SAndreas Gohr     * dimensions
42223a34783SAndreas Gohr     *
42323a34783SAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
42423a34783SAndreas Gohr     */
42523a34783SAndreas Gohr    function getResizeRatio($maxwidth,$maxheight=0){
42623a34783SAndreas Gohr        if(!$maxheight) $maxheight = $maxwidth;
42723a34783SAndreas Gohr
42823a34783SAndreas Gohr        $w = $this->getField('File.Width');
42923a34783SAndreas Gohr        $h = $this->getField('File.Height');
43023a34783SAndreas Gohr
43123a34783SAndreas Gohr        $ratio = 1;
43223a34783SAndreas Gohr        if($w >= $h){
43323a34783SAndreas Gohr            if($w >= $maxwidth){
43423a34783SAndreas Gohr                $ratio = $maxwidth/$w;
43523a34783SAndreas Gohr            }elseif($h > $maxheight){
43623a34783SAndreas Gohr                $ratio = $maxheight/$h;
43723a34783SAndreas Gohr            }
43823a34783SAndreas Gohr        }else{
43923a34783SAndreas Gohr            if($h >= $maxheight){
44023a34783SAndreas Gohr                $ratio = $maxheight/$h;
44123a34783SAndreas Gohr            }elseif($w > $maxwidth){
44223a34783SAndreas Gohr                $ratio = $maxwidth/$w;
44323a34783SAndreas Gohr            }
44423a34783SAndreas Gohr        }
44523a34783SAndreas Gohr        return $ratio;
44623a34783SAndreas Gohr    }
44723a34783SAndreas Gohr
44823a34783SAndreas Gohr
44923a34783SAndreas Gohr    /**
45055efc227SAndreas Gohr     * Set an IPTC field
45155efc227SAndreas Gohr     *
45255efc227SAndreas Gohr     * @author Sebastian Delmont <sdelmont@zonageek.com>
45355efc227SAndreas Gohr     */
45455efc227SAndreas Gohr    function setIPTCField($field, $value)
45555efc227SAndreas Gohr    {
45655efc227SAndreas Gohr        if (!isset($this->_info['iptc'])) {
45755efc227SAndreas Gohr            $this->_parseMarkerAdobe();
45855efc227SAndreas Gohr        }
45955efc227SAndreas Gohr
46055efc227SAndreas Gohr        if ($this->_markers == null) {
46155efc227SAndreas Gohr            return false;
46255efc227SAndreas Gohr        }
46355efc227SAndreas Gohr
46455efc227SAndreas Gohr        if ($this->_info['iptc'] == false) {
46555efc227SAndreas Gohr            $this->_info['iptc'] = array();
46655efc227SAndreas Gohr        }
46755efc227SAndreas Gohr
46855efc227SAndreas Gohr        $this->_info['iptc'][$field] = $value;
46955efc227SAndreas Gohr
47055efc227SAndreas Gohr        return true;
47155efc227SAndreas Gohr    }
47255efc227SAndreas Gohr
47355efc227SAndreas Gohr    /**
47455efc227SAndreas Gohr     * Delete an EXIF field
47555efc227SAndreas Gohr     *
47655efc227SAndreas Gohr     * @author Sebastian Delmont <sdelmont@zonageek.com>
47755efc227SAndreas Gohr     */
47855efc227SAndreas Gohr    function deleteExifField($field)
47955efc227SAndreas Gohr    {
48055efc227SAndreas Gohr        if (!isset($this->_info['exif'])) {
48155efc227SAndreas Gohr            $this->_parseMarkerAdobe();
48255efc227SAndreas Gohr        }
48355efc227SAndreas Gohr
48455efc227SAndreas Gohr        if ($this->_markers == null) {
48555efc227SAndreas Gohr            return false;
48655efc227SAndreas Gohr        }
48755efc227SAndreas Gohr
48855efc227SAndreas Gohr        if ($this->_info['exif'] != false) {
48955efc227SAndreas Gohr            unset($this->_info['exif'][$field]);
49055efc227SAndreas Gohr        }
49155efc227SAndreas Gohr
49255efc227SAndreas Gohr        return true;
49355efc227SAndreas Gohr    }
49455efc227SAndreas Gohr
49555efc227SAndreas Gohr    /**
49655efc227SAndreas Gohr     * Delete an Adobe field
49755efc227SAndreas Gohr     *
49855efc227SAndreas Gohr     * @author Sebastian Delmont <sdelmont@zonageek.com>
49955efc227SAndreas Gohr     */
50055efc227SAndreas Gohr    function deleteAdobeField($field)
50155efc227SAndreas Gohr    {
50255efc227SAndreas Gohr        if (!isset($this->_info['adobe'])) {
50355efc227SAndreas Gohr            $this->_parseMarkerAdobe();
50455efc227SAndreas Gohr        }
50555efc227SAndreas Gohr
50655efc227SAndreas Gohr        if ($this->_markers == null) {
50755efc227SAndreas Gohr            return false;
50855efc227SAndreas Gohr        }
50955efc227SAndreas Gohr
51055efc227SAndreas Gohr        if ($this->_info['adobe'] != false) {
51155efc227SAndreas Gohr            unset($this->_info['adobe'][$field]);
51255efc227SAndreas Gohr        }
51355efc227SAndreas Gohr
51455efc227SAndreas Gohr        return true;
51555efc227SAndreas Gohr    }
51655efc227SAndreas Gohr
51755efc227SAndreas Gohr    /**
51855efc227SAndreas Gohr     * Delete an IPTC field
51955efc227SAndreas Gohr     *
52055efc227SAndreas Gohr     * @author Sebastian Delmont <sdelmont@zonageek.com>
52155efc227SAndreas Gohr     */
52255efc227SAndreas Gohr    function deleteIPTCField($field)
52355efc227SAndreas Gohr    {
52455efc227SAndreas Gohr        if (!isset($this->_info['iptc'])) {
52555efc227SAndreas Gohr            $this->_parseMarkerAdobe();
52655efc227SAndreas Gohr        }
52755efc227SAndreas Gohr
52855efc227SAndreas Gohr        if ($this->_markers == null) {
52955efc227SAndreas Gohr            return false;
53055efc227SAndreas Gohr        }
53155efc227SAndreas Gohr
53255efc227SAndreas Gohr        if ($this->_info['iptc'] != false) {
53355efc227SAndreas Gohr            unset($this->_info['iptc'][$field]);
53455efc227SAndreas Gohr        }
53555efc227SAndreas Gohr
53655efc227SAndreas Gohr        return true;
53755efc227SAndreas Gohr    }
53855efc227SAndreas Gohr
53955efc227SAndreas Gohr    /**
54055efc227SAndreas Gohr     * Get the image's title, tries various fields
54155efc227SAndreas Gohr     *
54255efc227SAndreas Gohr     * @param int $max  maximum number chars (keeps words)
54355efc227SAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
54455efc227SAndreas Gohr     */
54555efc227SAndreas Gohr    function getTitle($max=80){
54655efc227SAndreas Gohr        $cap = '';
54755efc227SAndreas Gohr
54855efc227SAndreas Gohr        // try various fields
54955efc227SAndreas Gohr        $cap = $this->getField(array('Iptc.Headline',
55055efc227SAndreas Gohr                                     'Iptc.Caption',
551431c7fc8Shakan.sandell                                     'Xmp.dc:title',
55255efc227SAndreas Gohr                                     'Exif.UserComment',
55355efc227SAndreas Gohr                                     'Exif.TIFFUserComment',
5542684e50aSAndreas Gohr                                     'Exif.TIFFImageDescription',
5552684e50aSAndreas Gohr                                     'File.Name'));
55655efc227SAndreas Gohr        if (empty($cap)) return false;
55755efc227SAndreas Gohr
55855efc227SAndreas Gohr        if(!$max) return $cap;
55955efc227SAndreas Gohr        // Shorten to 80 chars (keeping words)
56055efc227SAndreas Gohr        $new = preg_replace('/\n.+$/','',wordwrap($cap, $max));
56155efc227SAndreas Gohr        if($new != $cap) $new .= '...';
56255efc227SAndreas Gohr
56355efc227SAndreas Gohr        return $new;
56455efc227SAndreas Gohr    }
56555efc227SAndreas Gohr
56655efc227SAndreas Gohr    /**
56755efc227SAndreas Gohr     * Gather various date fields
56855efc227SAndreas Gohr     *
56955efc227SAndreas Gohr     * @author Sebastian Delmont <sdelmont@zonageek.com>
57055efc227SAndreas Gohr     */
57155efc227SAndreas Gohr    function getDates()
57255efc227SAndreas Gohr    {
57355efc227SAndreas Gohr        $this->_parseAll();
57455efc227SAndreas Gohr        if ($this->_markers == null) {
575a73b5b7eSAndreas Gohr            if (@isset($this->_info['file']['UnixTime'])) {
576a73b5b7eSAndreas Gohr                $dates['FileModified'] = $this->_info['file']['UnixTime'];
577a73b5b7eSAndreas Gohr                $dates['Time'] = $this->_info['file']['UnixTime'];
578a73b5b7eSAndreas Gohr                $dates['TimeSource'] = 'FileModified';
579a73b5b7eSAndreas Gohr                $dates['TimeStr'] = date("Y-m-d H:i:s", $this->_info['file']['UnixTime']);
580a73b5b7eSAndreas Gohr                $dates['EarliestTime'] = $this->_info['file']['UnixTime'];
581a73b5b7eSAndreas Gohr                $dates['EarliestTimeSource'] = 'FileModified';
582a73b5b7eSAndreas Gohr                $dates['EarliestTimeStr'] = date("Y-m-d H:i:s", $this->_info['file']['UnixTime']);
583a73b5b7eSAndreas Gohr                $dates['LatestTime'] = $this->_info['file']['UnixTime'];
584a73b5b7eSAndreas Gohr                $dates['LatestTimeSource'] = 'FileModified';
585a73b5b7eSAndreas Gohr                $dates['LatestTimeStr'] = date("Y-m-d H:i:s", $this->_info['file']['UnixTime']);
586a73b5b7eSAndreas Gohr                return $dates;
587a73b5b7eSAndreas Gohr            }
58855efc227SAndreas Gohr            return false;
58955efc227SAndreas Gohr        }
59055efc227SAndreas Gohr
59155efc227SAndreas Gohr        $dates = array();
59255efc227SAndreas Gohr
59355efc227SAndreas Gohr        $latestTime = 0;
59455efc227SAndreas Gohr        $latestTimeSource = "";
59555efc227SAndreas Gohr        $earliestTime = time();
59655efc227SAndreas Gohr        $earliestTimeSource = "";
59755efc227SAndreas Gohr
59855efc227SAndreas Gohr        if (@isset($this->_info['exif']['DateTime'])) {
59955efc227SAndreas Gohr            $dates['ExifDateTime'] = $this->_info['exif']['DateTime'];
60055efc227SAndreas Gohr
60155efc227SAndreas Gohr            $aux = $this->_info['exif']['DateTime'];
60255efc227SAndreas Gohr            $aux{4} = "-";
60355efc227SAndreas Gohr            $aux{7} = "-";
60455efc227SAndreas Gohr            $t = strtotime($aux);
60555efc227SAndreas Gohr
6062114dafdSAndreas Gohr            if ($t && $t > $latestTime) {
60755efc227SAndreas Gohr                $latestTime = $t;
60855efc227SAndreas Gohr                $latestTimeSource = "ExifDateTime";
60955efc227SAndreas Gohr            }
61055efc227SAndreas Gohr
6112114dafdSAndreas Gohr            if ($t && $t < $earliestTime) {
61255efc227SAndreas Gohr                $earliestTime = $t;
61355efc227SAndreas Gohr                $earliestTimeSource = "ExifDateTime";
61455efc227SAndreas Gohr            }
61555efc227SAndreas Gohr        }
61655efc227SAndreas Gohr
61755efc227SAndreas Gohr        if (@isset($this->_info['exif']['DateTimeOriginal'])) {
61855efc227SAndreas Gohr            $dates['ExifDateTimeOriginal'] = $this->_info['exif']['DateTime'];
61955efc227SAndreas Gohr
62055efc227SAndreas Gohr            $aux = $this->_info['exif']['DateTimeOriginal'];
62155efc227SAndreas Gohr            $aux{4} = "-";
62255efc227SAndreas Gohr            $aux{7} = "-";
62355efc227SAndreas Gohr            $t = strtotime($aux);
62455efc227SAndreas Gohr
6252114dafdSAndreas Gohr            if ($t && $t > $latestTime) {
62655efc227SAndreas Gohr                $latestTime = $t;
62755efc227SAndreas Gohr                $latestTimeSource = "ExifDateTimeOriginal";
62855efc227SAndreas Gohr            }
62955efc227SAndreas Gohr
6302114dafdSAndreas Gohr            if ($t && $t < $earliestTime) {
63155efc227SAndreas Gohr                $earliestTime = $t;
63255efc227SAndreas Gohr                $earliestTimeSource = "ExifDateTimeOriginal";
63355efc227SAndreas Gohr            }
63455efc227SAndreas Gohr        }
63555efc227SAndreas Gohr
63655efc227SAndreas Gohr        if (@isset($this->_info['exif']['DateTimeDigitized'])) {
63755efc227SAndreas Gohr            $dates['ExifDateTimeDigitized'] = $this->_info['exif']['DateTime'];
63855efc227SAndreas Gohr
63955efc227SAndreas Gohr            $aux = $this->_info['exif']['DateTimeDigitized'];
64055efc227SAndreas Gohr            $aux{4} = "-";
64155efc227SAndreas Gohr            $aux{7} = "-";
64255efc227SAndreas Gohr            $t = strtotime($aux);
64355efc227SAndreas Gohr
6442114dafdSAndreas Gohr            if ($t && $t > $latestTime) {
64555efc227SAndreas Gohr                $latestTime = $t;
64655efc227SAndreas Gohr                $latestTimeSource = "ExifDateTimeDigitized";
64755efc227SAndreas Gohr            }
64855efc227SAndreas Gohr
6492114dafdSAndreas Gohr            if ($t && $t < $earliestTime) {
65055efc227SAndreas Gohr                $earliestTime = $t;
65155efc227SAndreas Gohr                $earliestTimeSource = "ExifDateTimeDigitized";
65255efc227SAndreas Gohr            }
65355efc227SAndreas Gohr        }
65455efc227SAndreas Gohr
65555efc227SAndreas Gohr        if (@isset($this->_info['iptc']['DateCreated'])) {
65655efc227SAndreas Gohr            $dates['IPTCDateCreated'] = $this->_info['iptc']['DateCreated'];
65755efc227SAndreas Gohr
65855efc227SAndreas Gohr            $aux = $this->_info['iptc']['DateCreated'];
65955efc227SAndreas Gohr            $aux = substr($aux, 0, 4) . "-" . substr($aux, 4, 2) . "-" . substr($aux, 6, 2);
66055efc227SAndreas Gohr            $t = strtotime($aux);
66155efc227SAndreas Gohr
6622114dafdSAndreas Gohr            if ($t && $t > $latestTime) {
66355efc227SAndreas Gohr                $latestTime = $t;
66455efc227SAndreas Gohr                $latestTimeSource = "IPTCDateCreated";
66555efc227SAndreas Gohr            }
66655efc227SAndreas Gohr
6672114dafdSAndreas Gohr            if ($t && $t < $earliestTime) {
66855efc227SAndreas Gohr                $earliestTime = $t;
66955efc227SAndreas Gohr                $earliestTimeSource = "IPTCDateCreated";
67055efc227SAndreas Gohr            }
67155efc227SAndreas Gohr        }
67255efc227SAndreas Gohr
67355efc227SAndreas Gohr        if (@isset($this->_info['file']['UnixTime'])) {
67455efc227SAndreas Gohr            $dates['FileModified'] = $this->_info['file']['UnixTime'];
67555efc227SAndreas Gohr
67655efc227SAndreas Gohr            $t = $this->_info['file']['UnixTime'];
67755efc227SAndreas Gohr
6782114dafdSAndreas Gohr            if ($t && $t > $latestTime) {
67955efc227SAndreas Gohr                $latestTime = $t;
68055efc227SAndreas Gohr                $latestTimeSource = "FileModified";
68155efc227SAndreas Gohr            }
68255efc227SAndreas Gohr
6832114dafdSAndreas Gohr            if ($t && $t < $earliestTime) {
68455efc227SAndreas Gohr                $earliestTime = $t;
68555efc227SAndreas Gohr                $earliestTimeSource = "FileModified";
68655efc227SAndreas Gohr            }
68755efc227SAndreas Gohr        }
68855efc227SAndreas Gohr
68955efc227SAndreas Gohr        $dates['Time'] = $earliestTime;
69055efc227SAndreas Gohr        $dates['TimeSource'] = $earliestTimeSource;
69155efc227SAndreas Gohr        $dates['TimeStr'] = date("Y-m-d H:i:s", $earliestTime);
69255efc227SAndreas Gohr        $dates['EarliestTime'] = $earliestTime;
69355efc227SAndreas Gohr        $dates['EarliestTimeSource'] = $earliestTimeSource;
69455efc227SAndreas Gohr        $dates['EarliestTimeStr'] = date("Y-m-d H:i:s", $earliestTime);
69555efc227SAndreas Gohr        $dates['LatestTime'] = $latestTime;
69655efc227SAndreas Gohr        $dates['LatestTimeSource'] = $latestTimeSource;
69755efc227SAndreas Gohr        $dates['LatestTimeStr'] = date("Y-m-d H:i:s", $latestTime);
69855efc227SAndreas Gohr
69955efc227SAndreas Gohr        return $dates;
70055efc227SAndreas Gohr    }
70155efc227SAndreas Gohr
70255efc227SAndreas Gohr    /**
70355efc227SAndreas Gohr     * Get the image width, tries various fields
70455efc227SAndreas Gohr     *
70555efc227SAndreas Gohr     * @author Sebastian Delmont <sdelmont@zonageek.com>
70655efc227SAndreas Gohr     */
70755efc227SAndreas Gohr    function getWidth()
70855efc227SAndreas Gohr    {
70955efc227SAndreas Gohr        if (!isset($this->_info['sof'])) {
71055efc227SAndreas Gohr            $this->_parseMarkerSOF();
71155efc227SAndreas Gohr        }
71255efc227SAndreas Gohr
71355efc227SAndreas Gohr        if ($this->_markers == null) {
71455efc227SAndreas Gohr            return false;
71555efc227SAndreas Gohr        }
71655efc227SAndreas Gohr
71755efc227SAndreas Gohr        if (isset($this->_info['sof']['ImageWidth'])) {
71855efc227SAndreas Gohr            return $this->_info['sof']['ImageWidth'];
71955efc227SAndreas Gohr        }
72055efc227SAndreas Gohr
72155efc227SAndreas Gohr        if (!isset($this->_info['exif'])) {
72255efc227SAndreas Gohr            $this->_parseMarkerExif();
72355efc227SAndreas Gohr        }
72455efc227SAndreas Gohr
72555efc227SAndreas Gohr        if (isset($this->_info['exif']['PixelXDimension'])) {
72655efc227SAndreas Gohr            return $this->_info['exif']['PixelXDimension'];
72755efc227SAndreas Gohr        }
72855efc227SAndreas Gohr
72955efc227SAndreas Gohr        return false;
73055efc227SAndreas Gohr    }
73155efc227SAndreas Gohr
73255efc227SAndreas Gohr    /**
73355efc227SAndreas Gohr     * Get the image height, tries various fields
73455efc227SAndreas Gohr     *
73555efc227SAndreas Gohr     * @author Sebastian Delmont <sdelmont@zonageek.com>
73655efc227SAndreas Gohr     */
73755efc227SAndreas Gohr    function getHeight()
73855efc227SAndreas Gohr    {
73955efc227SAndreas Gohr        if (!isset($this->_info['sof'])) {
74055efc227SAndreas Gohr            $this->_parseMarkerSOF();
74155efc227SAndreas Gohr        }
74255efc227SAndreas Gohr
74355efc227SAndreas Gohr        if ($this->_markers == null) {
74455efc227SAndreas Gohr            return false;
74555efc227SAndreas Gohr        }
74655efc227SAndreas Gohr
74755efc227SAndreas Gohr        if (isset($this->_info['sof']['ImageHeight'])) {
74855efc227SAndreas Gohr            return $this->_info['sof']['ImageHeight'];
74955efc227SAndreas Gohr        }
75055efc227SAndreas Gohr
75155efc227SAndreas Gohr        if (!isset($this->_info['exif'])) {
75255efc227SAndreas Gohr            $this->_parseMarkerExif();
75355efc227SAndreas Gohr        }
75455efc227SAndreas Gohr
75555efc227SAndreas Gohr        if (isset($this->_info['exif']['PixelYDimension'])) {
75655efc227SAndreas Gohr            return $this->_info['exif']['PixelYDimension'];
75755efc227SAndreas Gohr        }
75855efc227SAndreas Gohr
75955efc227SAndreas Gohr        return false;
76055efc227SAndreas Gohr    }
76155efc227SAndreas Gohr
76255efc227SAndreas Gohr    /**
76355efc227SAndreas Gohr     * Get an dimension string for use in img tag
76455efc227SAndreas Gohr     *
76555efc227SAndreas Gohr     * @author Sebastian Delmont <sdelmont@zonageek.com>
76655efc227SAndreas Gohr     */
76755efc227SAndreas Gohr    function getDimStr()
76855efc227SAndreas Gohr    {
76955efc227SAndreas Gohr        if ($this->_markers == null) {
77055efc227SAndreas Gohr            return false;
77155efc227SAndreas Gohr        }
77255efc227SAndreas Gohr
77355efc227SAndreas Gohr        $w = $this->getWidth();
77455efc227SAndreas Gohr        $h = $this->getHeight();
77555efc227SAndreas Gohr
77655efc227SAndreas Gohr        return "width='" . $w . "' height='" . $h . "'";
77755efc227SAndreas Gohr    }
77855efc227SAndreas Gohr
77955efc227SAndreas Gohr    /**
78055efc227SAndreas Gohr     * Checks for an embedded thumbnail
78155efc227SAndreas Gohr     *
78255efc227SAndreas Gohr     * @author Sebastian Delmont <sdelmont@zonageek.com>
78355efc227SAndreas Gohr     */
78455efc227SAndreas Gohr    function hasThumbnail($which = 'any')
78555efc227SAndreas Gohr    {
78655efc227SAndreas Gohr        if (($which == 'any') || ($which == 'exif')) {
78755efc227SAndreas Gohr            if (!isset($this->_info['exif'])) {
78855efc227SAndreas Gohr                $this->_parseMarkerExif();
78955efc227SAndreas Gohr            }
79055efc227SAndreas Gohr
79155efc227SAndreas Gohr            if ($this->_markers == null) {
79255efc227SAndreas Gohr                return false;
79355efc227SAndreas Gohr            }
79455efc227SAndreas Gohr
79555efc227SAndreas Gohr            if (isset($this->_info['exif']) && is_array($this->_info['exif'])) {
79655efc227SAndreas Gohr                if (isset($this->_info['exif']['JFIFThumbnail'])) {
79755efc227SAndreas Gohr                    return 'exif';
79855efc227SAndreas Gohr                }
79955efc227SAndreas Gohr            }
80055efc227SAndreas Gohr        }
80155efc227SAndreas Gohr
80255efc227SAndreas Gohr        if ($which == 'adobe') {
80355efc227SAndreas Gohr            if (!isset($this->_info['adobe'])) {
80455efc227SAndreas Gohr                $this->_parseMarkerAdobe();
80555efc227SAndreas Gohr            }
80655efc227SAndreas Gohr
80755efc227SAndreas Gohr            if ($this->_markers == null) {
80855efc227SAndreas Gohr                return false;
80955efc227SAndreas Gohr            }
81055efc227SAndreas Gohr
81155efc227SAndreas Gohr            if (isset($this->_info['adobe']) && is_array($this->_info['adobe'])) {
81255efc227SAndreas Gohr                if (isset($this->_info['adobe']['ThumbnailData'])) {
81355efc227SAndreas Gohr                    return 'exif';
81455efc227SAndreas Gohr                }
81555efc227SAndreas Gohr            }
81655efc227SAndreas Gohr        }
81755efc227SAndreas Gohr
81855efc227SAndreas Gohr        return false;
81955efc227SAndreas Gohr    }
82055efc227SAndreas Gohr
82155efc227SAndreas Gohr    /**
82255efc227SAndreas Gohr     * Send embedded thumbnail to browser
82355efc227SAndreas Gohr     *
82455efc227SAndreas Gohr     * @author Sebastian Delmont <sdelmont@zonageek.com>
82555efc227SAndreas Gohr     */
82655efc227SAndreas Gohr    function sendThumbnail($which = 'any')
82755efc227SAndreas Gohr    {
82855efc227SAndreas Gohr        $data = null;
82955efc227SAndreas Gohr
83055efc227SAndreas Gohr        if (($which == 'any') || ($which == 'exif')) {
83155efc227SAndreas Gohr            if (!isset($this->_info['exif'])) {
83255efc227SAndreas Gohr                $this->_parseMarkerExif();
83355efc227SAndreas Gohr            }
83455efc227SAndreas Gohr
83555efc227SAndreas Gohr            if ($this->_markers == null) {
83655efc227SAndreas Gohr                return false;
83755efc227SAndreas Gohr            }
83855efc227SAndreas Gohr
83955efc227SAndreas Gohr            if (isset($this->_info['exif']) && is_array($this->_info['exif'])) {
84055efc227SAndreas Gohr                if (isset($this->_info['exif']['JFIFThumbnail'])) {
84155efc227SAndreas Gohr                    $data =& $this->_info['exif']['JFIFThumbnail'];
84255efc227SAndreas Gohr                }
84355efc227SAndreas Gohr            }
84455efc227SAndreas Gohr        }
84555efc227SAndreas Gohr
84655efc227SAndreas Gohr        if (($which == 'adobe') || ($data == null)){
84755efc227SAndreas Gohr            if (!isset($this->_info['adobe'])) {
84855efc227SAndreas Gohr                $this->_parseMarkerAdobe();
84955efc227SAndreas Gohr            }
85055efc227SAndreas Gohr
85155efc227SAndreas Gohr            if ($this->_markers == null) {
85255efc227SAndreas Gohr                return false;
85355efc227SAndreas Gohr            }
85455efc227SAndreas Gohr
85555efc227SAndreas Gohr            if (isset($this->_info['adobe']) && is_array($this->_info['adobe'])) {
85655efc227SAndreas Gohr                if (isset($this->_info['adobe']['ThumbnailData'])) {
85755efc227SAndreas Gohr                    $data =& $this->_info['adobe']['ThumbnailData'];
85855efc227SAndreas Gohr                }
85955efc227SAndreas Gohr            }
86055efc227SAndreas Gohr        }
86155efc227SAndreas Gohr
86255efc227SAndreas Gohr        if ($data != null) {
86355efc227SAndreas Gohr            header("Content-type: image/jpeg");
86455efc227SAndreas Gohr            echo $data;
86555efc227SAndreas Gohr            return true;
86655efc227SAndreas Gohr        }
86755efc227SAndreas Gohr
86855efc227SAndreas Gohr        return false;
86955efc227SAndreas Gohr    }
87055efc227SAndreas Gohr
87155efc227SAndreas Gohr    /**
87255efc227SAndreas Gohr     * Save changed Metadata
87355efc227SAndreas Gohr     *
87455efc227SAndreas Gohr     * @author Sebastian Delmont <sdelmont@zonageek.com>
87536df6fa3SAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
87655efc227SAndreas Gohr     */
87755efc227SAndreas Gohr    function save($fileName = "") {
87855efc227SAndreas Gohr        if ($fileName == "") {
87936df6fa3SAndreas Gohr            $tmpName = tempnam(dirname($this->_fileName),'_metatemp_');
88055efc227SAndreas Gohr            $this->_writeJPEG($tmpName);
88136df6fa3SAndreas Gohr            if (@file_exists($tmpName)) {
882bf5e5a5bSAndreas Gohr                return io_rename($tmpName, $this->_fileName);
88355efc227SAndreas Gohr            }
88436df6fa3SAndreas Gohr        } else {
88536df6fa3SAndreas Gohr            return $this->_writeJPEG($fileName);
88655efc227SAndreas Gohr        }
88736df6fa3SAndreas Gohr        return false;
88855efc227SAndreas Gohr    }
88955efc227SAndreas Gohr
89055efc227SAndreas Gohr    /*************************************************************/
89155efc227SAndreas Gohr    /* PRIVATE FUNCTIONS (Internal Use Only!)                    */
89255efc227SAndreas Gohr    /*************************************************************/
89355efc227SAndreas Gohr
89455efc227SAndreas Gohr    /*************************************************************/
89555efc227SAndreas Gohr    function _dispose()
89655efc227SAndreas Gohr    {
89755efc227SAndreas Gohr        $this->_fileName = $fileName;
89855efc227SAndreas Gohr
89955efc227SAndreas Gohr        $this->_fp = null;
90055efc227SAndreas Gohr        $this->_type = 'unknown';
90155efc227SAndreas Gohr
90255efc227SAndreas Gohr        unset($this->_markers);
90355efc227SAndreas Gohr        unset($this->_info);
90455efc227SAndreas Gohr    }
90555efc227SAndreas Gohr
90655efc227SAndreas Gohr    /*************************************************************/
90755efc227SAndreas Gohr    function _readJPEG()
90855efc227SAndreas Gohr    {
90955efc227SAndreas Gohr        unset($this->_markers);
910a73b5b7eSAndreas Gohr        //unset($this->_info);
91155efc227SAndreas Gohr        $this->_markers = array();
912a73b5b7eSAndreas Gohr        //$this->_info = array();
91355efc227SAndreas Gohr
91455efc227SAndreas Gohr        $this->_fp = @fopen($this->_fileName, 'rb');
91555efc227SAndreas Gohr        if ($this->_fp) {
91655efc227SAndreas Gohr            if (file_exists($this->_fileName)) {
91755efc227SAndreas Gohr                $this->_type = 'file';
91855efc227SAndreas Gohr            }
91955efc227SAndreas Gohr            else {
92055efc227SAndreas Gohr                $this->_type = 'url';
92155efc227SAndreas Gohr            }
92255efc227SAndreas Gohr        }
92355efc227SAndreas Gohr        else {
92455efc227SAndreas Gohr            $this->_fp = null;
92555efc227SAndreas Gohr            return false;  // ERROR: Can't open file
92655efc227SAndreas Gohr        }
92755efc227SAndreas Gohr
92855efc227SAndreas Gohr        // Check for the JPEG signature
92955efc227SAndreas Gohr        $c1 = ord(fgetc($this->_fp));
93055efc227SAndreas Gohr        $c2 = ord(fgetc($this->_fp));
93155efc227SAndreas Gohr
93255efc227SAndreas Gohr        if ($c1 != 0xFF || $c2 != 0xD8) {   // (0xFF + SOI)
93355efc227SAndreas Gohr            $this->_markers = null;
93455efc227SAndreas Gohr            return false;  // ERROR: File is not a JPEG
93555efc227SAndreas Gohr        }
93655efc227SAndreas Gohr
93755efc227SAndreas Gohr        $count = 0;
93855efc227SAndreas Gohr
93955efc227SAndreas Gohr        $done = false;
94055efc227SAndreas Gohr        $ok = true;
94155efc227SAndreas Gohr
94255efc227SAndreas Gohr        while (!$done) {
94355efc227SAndreas Gohr            $capture = false;
94455efc227SAndreas Gohr
94555efc227SAndreas Gohr          // First, skip any non 0xFF bytes
94655efc227SAndreas Gohr            $discarded = 0;
94755efc227SAndreas Gohr            $c = ord(fgetc($this->_fp));
94855efc227SAndreas Gohr            while (!feof($this->_fp) && ($c != 0xFF)) {
94955efc227SAndreas Gohr                $discarded++;
95055efc227SAndreas Gohr                $c = ord(fgetc($this->_fp));
95155efc227SAndreas Gohr            }
95255efc227SAndreas Gohr          // Then skip all 0xFF until the marker byte
95355efc227SAndreas Gohr            do {
95455efc227SAndreas Gohr                $marker = ord(fgetc($this->_fp));
95555efc227SAndreas Gohr            } while (!feof($this->_fp) && ($marker == 0xFF));
95655efc227SAndreas Gohr
95755efc227SAndreas Gohr            if (feof($this->_fp)) {
95855efc227SAndreas Gohr                return false; // ERROR: Unexpected EOF
95955efc227SAndreas Gohr            }
96055efc227SAndreas Gohr            if ($discarded != 0) {
96155efc227SAndreas Gohr                return false; // ERROR: Extraneous data
96255efc227SAndreas Gohr            }
96355efc227SAndreas Gohr
96455efc227SAndreas Gohr            $length = ord(fgetc($this->_fp)) * 256 + ord(fgetc($this->_fp));
96555efc227SAndreas Gohr            if (feof($this->_fp)) {
96655efc227SAndreas Gohr                return false; // ERROR: Unexpected EOF
96755efc227SAndreas Gohr            }
96855efc227SAndreas Gohr            if ($length < 2) {
96955efc227SAndreas Gohr                return false; // ERROR: Extraneous data
97055efc227SAndreas Gohr            }
97155efc227SAndreas Gohr            $length = $length - 2; // The length we got counts itself
97255efc227SAndreas Gohr
97355efc227SAndreas Gohr            switch ($marker) {
97455efc227SAndreas Gohr            case 0xC0:    // SOF0
97555efc227SAndreas Gohr            case 0xC1:    // SOF1
97655efc227SAndreas Gohr            case 0xC2:    // SOF2
97755efc227SAndreas Gohr            case 0xC9:    // SOF9
97855efc227SAndreas Gohr            case 0xE0:    // APP0: JFIF data
979431c7fc8Shakan.sandell            case 0xE1:    // APP1: EXIF or XMP data
98055efc227SAndreas Gohr            case 0xED:    // APP13: IPTC / Photoshop data
98155efc227SAndreas Gohr                $capture = true;
98255efc227SAndreas Gohr                break;
98355efc227SAndreas Gohr            case 0xDA:    // SOS: Start of scan... the image itself and the last block on the file
98455efc227SAndreas Gohr                $capture = false;
98555efc227SAndreas Gohr                $length = -1;  // This field has no length... it includes all data until EOF
98655efc227SAndreas Gohr                $done = true;
98755efc227SAndreas Gohr                break;
98855efc227SAndreas Gohr            default:
98955efc227SAndreas Gohr                $capture = true;//false;
99055efc227SAndreas Gohr                break;
99155efc227SAndreas Gohr            }
99255efc227SAndreas Gohr
99355efc227SAndreas Gohr            $this->_markers[$count] = array();
99455efc227SAndreas Gohr            $this->_markers[$count]['marker'] = $marker;
99555efc227SAndreas Gohr            $this->_markers[$count]['length'] = $length;
99655efc227SAndreas Gohr
99755efc227SAndreas Gohr            if ($capture) {
998*ed3655c4STom N Harris                if ($length)
99955efc227SAndreas Gohr                    $this->_markers[$count]['data'] =& fread($this->_fp, $length);
1000*ed3655c4STom N Harris                else
1001*ed3655c4STom N Harris                    $this->_markers[$count]['data'] = "";
100255efc227SAndreas Gohr            }
100355efc227SAndreas Gohr            elseif (!$done) {
100455efc227SAndreas Gohr                $result = @fseek($this->_fp, $length, SEEK_CUR);
100555efc227SAndreas Gohr              // fseek doesn't seem to like HTTP 'files', but fgetc has no problem
100655efc227SAndreas Gohr                if (!($result === 0)) {
100755efc227SAndreas Gohr                    for ($i = 0; $i < $length; $i++) {
100855efc227SAndreas Gohr                        fgetc($this->_fp);
100955efc227SAndreas Gohr                    }
101055efc227SAndreas Gohr                }
101155efc227SAndreas Gohr            }
101255efc227SAndreas Gohr            $count++;
101355efc227SAndreas Gohr        }
101455efc227SAndreas Gohr
101555efc227SAndreas Gohr        if ($this->_fp) {
101655efc227SAndreas Gohr            fclose($this->_fp);
101755efc227SAndreas Gohr            $this->_fp = null;
101855efc227SAndreas Gohr        }
101955efc227SAndreas Gohr
102055efc227SAndreas Gohr        return $ok;
102155efc227SAndreas Gohr    }
102255efc227SAndreas Gohr
102355efc227SAndreas Gohr    /*************************************************************/
102455efc227SAndreas Gohr    function _parseAll()
102555efc227SAndreas Gohr    {
10261017ae2eSAndreas Gohr        if (!isset($this->_info['file'])) {
10271017ae2eSAndreas Gohr            $this->_parseFileInfo();
10281017ae2eSAndreas Gohr        }
102955efc227SAndreas Gohr        if (!isset($this->_markers)) {
103055efc227SAndreas Gohr            $this->_readJPEG();
103155efc227SAndreas Gohr        }
103255efc227SAndreas Gohr
103355efc227SAndreas Gohr        if ($this->_markers == null) {
103455efc227SAndreas Gohr            return false;
103555efc227SAndreas Gohr        }
103655efc227SAndreas Gohr
103755efc227SAndreas Gohr        if (!isset($this->_info['jfif'])) {
103855efc227SAndreas Gohr            $this->_parseMarkerJFIF();
103955efc227SAndreas Gohr        }
104055efc227SAndreas Gohr        if (!isset($this->_info['jpeg'])) {
104155efc227SAndreas Gohr            $this->_parseMarkerSOF();
104255efc227SAndreas Gohr        }
104355efc227SAndreas Gohr        if (!isset($this->_info['exif'])) {
104455efc227SAndreas Gohr            $this->_parseMarkerExif();
104555efc227SAndreas Gohr        }
1046431c7fc8Shakan.sandell        if (!isset($this->_info['xmp'])) {
1047431c7fc8Shakan.sandell            $this->_parseMarkerXmp();
1048431c7fc8Shakan.sandell        }
104955efc227SAndreas Gohr        if (!isset($this->_info['adobe'])) {
105055efc227SAndreas Gohr            $this->_parseMarkerAdobe();
105155efc227SAndreas Gohr        }
105255efc227SAndreas Gohr    }
105355efc227SAndreas Gohr
105455efc227SAndreas Gohr    /*************************************************************/
105555efc227SAndreas Gohr    function _writeJPEG($outputName)
105655efc227SAndreas Gohr    {
105755efc227SAndreas Gohr        $this->_parseAll();
105855efc227SAndreas Gohr
105955efc227SAndreas Gohr        $wroteEXIF = false;
106055efc227SAndreas Gohr        $wroteAdobe = false;
106155efc227SAndreas Gohr
106255efc227SAndreas Gohr        $this->_fp = @fopen($this->_fileName, 'r');
106355efc227SAndreas Gohr        if ($this->_fp) {
106455efc227SAndreas Gohr            if (file_exists($this->_fileName)) {
106555efc227SAndreas Gohr                $this->_type = 'file';
106655efc227SAndreas Gohr            }
106755efc227SAndreas Gohr            else {
106855efc227SAndreas Gohr                $this->_type = 'url';
106955efc227SAndreas Gohr            }
107055efc227SAndreas Gohr        }
107155efc227SAndreas Gohr        else {
107255efc227SAndreas Gohr            $this->_fp = null;
107355efc227SAndreas Gohr            return false;  // ERROR: Can't open file
107455efc227SAndreas Gohr        }
107555efc227SAndreas Gohr
107655efc227SAndreas Gohr        $this->_fpout = fopen($outputName, 'wb');
107755efc227SAndreas Gohr        if ($this->_fpout) {
107855efc227SAndreas Gohr        }
107955efc227SAndreas Gohr        else {
108055efc227SAndreas Gohr            $this->_fpout = null;
108155efc227SAndreas Gohr            fclose($this->_fp);
108255efc227SAndreas Gohr            $this->_fp = null;
108355efc227SAndreas Gohr            return false;  // ERROR: Can't open output file
108455efc227SAndreas Gohr        }
108555efc227SAndreas Gohr
108655efc227SAndreas Gohr        // Check for the JPEG signature
108755efc227SAndreas Gohr        $c1 = ord(fgetc($this->_fp));
108855efc227SAndreas Gohr        $c2 = ord(fgetc($this->_fp));
108955efc227SAndreas Gohr
109055efc227SAndreas Gohr        if ($c1 != 0xFF || $c2 != 0xD8) {   // (0xFF + SOI)
109155efc227SAndreas Gohr            return false;  // ERROR: File is not a JPEG
109255efc227SAndreas Gohr        }
109355efc227SAndreas Gohr
109455efc227SAndreas Gohr        fputs($this->_fpout, chr(0xFF), 1);
109555efc227SAndreas Gohr        fputs($this->_fpout, chr(0xD8), 1); // (0xFF + SOI)
109655efc227SAndreas Gohr
109755efc227SAndreas Gohr        $count = 0;
109855efc227SAndreas Gohr
109955efc227SAndreas Gohr        $done = false;
110055efc227SAndreas Gohr        $ok = true;
110155efc227SAndreas Gohr
110255efc227SAndreas Gohr        while (!$done) {
110355efc227SAndreas Gohr          // First, skip any non 0xFF bytes
110455efc227SAndreas Gohr            $discarded = 0;
110555efc227SAndreas Gohr            $c = ord(fgetc($this->_fp));
110655efc227SAndreas Gohr            while (!feof($this->_fp) && ($c != 0xFF)) {
110755efc227SAndreas Gohr                $discarded++;
110855efc227SAndreas Gohr                $c = ord(fgetc($this->_fp));
110955efc227SAndreas Gohr            }
111055efc227SAndreas Gohr          // Then skip all 0xFF until the marker byte
111155efc227SAndreas Gohr            do {
111255efc227SAndreas Gohr                $marker = ord(fgetc($this->_fp));
111355efc227SAndreas Gohr            } while (!feof($this->_fp) && ($marker == 0xFF));
111455efc227SAndreas Gohr
111555efc227SAndreas Gohr            if (feof($this->_fp)) {
111655efc227SAndreas Gohr                $ok = false;
111755efc227SAndreas Gohr                break; // ERROR: Unexpected EOF
111855efc227SAndreas Gohr            }
111955efc227SAndreas Gohr            if ($discarded != 0) {
112055efc227SAndreas Gohr                $ok = false;
112155efc227SAndreas Gohr                break; // ERROR: Extraneous data
112255efc227SAndreas Gohr            }
112355efc227SAndreas Gohr
112455efc227SAndreas Gohr            $length = ord(fgetc($this->_fp)) * 256 + ord(fgetc($this->_fp));
112555efc227SAndreas Gohr            if (feof($this->_fp)) {
112655efc227SAndreas Gohr                $ok = false;
112755efc227SAndreas Gohr                break; // ERROR: Unexpected EOF
112855efc227SAndreas Gohr            }
112955efc227SAndreas Gohr            if ($length < 2) {
113055efc227SAndreas Gohr                $ok = false;
113155efc227SAndreas Gohr                break; // ERROR: Extraneous data
113255efc227SAndreas Gohr            }
113355efc227SAndreas Gohr            $length = $length - 2; // The length we got counts itself
113455efc227SAndreas Gohr
113555efc227SAndreas Gohr            unset($data);
113655efc227SAndreas Gohr            if ($marker == 0xE1) { // APP1: EXIF data
113755efc227SAndreas Gohr                $data =& $this->_createMarkerEXIF();
113855efc227SAndreas Gohr                $wroteEXIF = true;
113955efc227SAndreas Gohr            }
114055efc227SAndreas Gohr            elseif ($marker == 0xED) { // APP13: IPTC / Photoshop data
114155efc227SAndreas Gohr                $data =& $this->_createMarkerAdobe();
114255efc227SAndreas Gohr                $wroteAdobe = true;
114355efc227SAndreas Gohr            }
114455efc227SAndreas Gohr            elseif ($marker == 0xDA) { // SOS: Start of scan... the image itself and the last block on the file
114555efc227SAndreas Gohr                $done = true;
114655efc227SAndreas Gohr            }
114755efc227SAndreas Gohr
114855efc227SAndreas Gohr            if (!$wroteEXIF && (($marker < 0xE0) || ($marker > 0xEF))) {
114955efc227SAndreas Gohr                if (isset($this->_info['exif']) && is_array($this->_info['exif'])) {
115055efc227SAndreas Gohr                    $exif =& $this->_createMarkerEXIF();
115155efc227SAndreas Gohr                    $this->_writeJPEGMarker(0xE1, strlen($exif), $exif, 0);
115255efc227SAndreas Gohr                    unset($exif);
115355efc227SAndreas Gohr                }
115455efc227SAndreas Gohr                $wroteEXIF = true;
115555efc227SAndreas Gohr            }
115655efc227SAndreas Gohr
115755efc227SAndreas Gohr            if (!$wroteAdobe && (($marker < 0xE0) || ($marker > 0xEF))) {
115855efc227SAndreas Gohr                if ((isset($this->_info['adobe']) && is_array($this->_info['adobe']))
115955efc227SAndreas Gohr                || (isset($this->_info['iptc']) && is_array($this->_info['iptc']))) {
116055efc227SAndreas Gohr                    $adobe =& $this->_createMarkerAdobe();
116155efc227SAndreas Gohr                    $this->_writeJPEGMarker(0xED, strlen($adobe), $adobe, 0);
116255efc227SAndreas Gohr                    unset($adobe);
116355efc227SAndreas Gohr                }
116455efc227SAndreas Gohr                $wroteAdobe = true;
116555efc227SAndreas Gohr            }
116655efc227SAndreas Gohr
116755efc227SAndreas Gohr            $origLength = $length;
116855efc227SAndreas Gohr            if (isset($data)) {
116955efc227SAndreas Gohr                $length = strlen($data);
117055efc227SAndreas Gohr            }
117155efc227SAndreas Gohr
117255efc227SAndreas Gohr            if ($marker != -1) {
117355efc227SAndreas Gohr                $this->_writeJPEGMarker($marker, $length, $data, $origLength);
117455efc227SAndreas Gohr            }
117555efc227SAndreas Gohr        }
117655efc227SAndreas Gohr
117755efc227SAndreas Gohr        if ($this->_fp) {
117855efc227SAndreas Gohr            fclose($this->_fp);
117955efc227SAndreas Gohr            $this->_fp = null;
118055efc227SAndreas Gohr        }
118155efc227SAndreas Gohr
118255efc227SAndreas Gohr        if ($this->_fpout) {
118355efc227SAndreas Gohr            fclose($this->_fpout);
118455efc227SAndreas Gohr            $this->_fpout = null;
118555efc227SAndreas Gohr        }
118655efc227SAndreas Gohr
118755efc227SAndreas Gohr        return $ok;
118855efc227SAndreas Gohr    }
118955efc227SAndreas Gohr
119055efc227SAndreas Gohr    /*************************************************************/
119155efc227SAndreas Gohr    function _writeJPEGMarker($marker, $length, &$data, $origLength)
119255efc227SAndreas Gohr    {
119355efc227SAndreas Gohr        if ($length <= 0) {
119455efc227SAndreas Gohr            return false;
119555efc227SAndreas Gohr        }
119655efc227SAndreas Gohr
119755efc227SAndreas Gohr        fputs($this->_fpout, chr(0xFF), 1);
119855efc227SAndreas Gohr        fputs($this->_fpout, chr($marker), 1);
119955efc227SAndreas Gohr        fputs($this->_fpout, chr((($length + 2) & 0x0000FF00) >> 8), 1);
120055efc227SAndreas Gohr        fputs($this->_fpout, chr((($length + 2) & 0x000000FF) >> 0), 1);
120155efc227SAndreas Gohr
120255efc227SAndreas Gohr        if (isset($data)) {
120355efc227SAndreas Gohr            // Copy the generated data
120455efc227SAndreas Gohr            fputs($this->_fpout, $data, $length);
120555efc227SAndreas Gohr
120655efc227SAndreas Gohr            if ($origLength > 0) {   // Skip the original data
120755efc227SAndreas Gohr                $result = @fseek($this->_fp, $origLength, SEEK_CUR);
120855efc227SAndreas Gohr                // fseek doesn't seem to like HTTP 'files', but fgetc has no problem
120955efc227SAndreas Gohr                if ($result != 0) {
121055efc227SAndreas Gohr                    for ($i = 0; $i < $origLength; $i++) {
121155efc227SAndreas Gohr                        fgetc($this->_fp);
121255efc227SAndreas Gohr                    }
121355efc227SAndreas Gohr                }
121455efc227SAndreas Gohr            }
121555efc227SAndreas Gohr        }
121655efc227SAndreas Gohr        else {
121755efc227SAndreas Gohr            if ($marker == 0xDA) {  // Copy until EOF
121855efc227SAndreas Gohr                while (!feof($this->_fp)) {
1219*ed3655c4STom N Harris                    $data = fread($this->_fp, 1024 * 16);
122055efc227SAndreas Gohr                    fputs($this->_fpout, $data, strlen($data));
122155efc227SAndreas Gohr                }
122255efc227SAndreas Gohr            }
122355efc227SAndreas Gohr            else { // Copy only $length bytes
1224*ed3655c4STom N Harris                $data = @fread($this->_fp, $length);
122555efc227SAndreas Gohr                fputs($this->_fpout, $data, $length);
122655efc227SAndreas Gohr            }
122755efc227SAndreas Gohr        }
122855efc227SAndreas Gohr
122955efc227SAndreas Gohr        return true;
123055efc227SAndreas Gohr    }
123155efc227SAndreas Gohr
123223a34783SAndreas Gohr    /**
123323a34783SAndreas Gohr     * Gets basic info from the file - should work with non-JPEGs
123423a34783SAndreas Gohr     *
123523a34783SAndreas Gohr     * @author  Sebastian Delmont <sdelmont@zonageek.com>
123623a34783SAndreas Gohr     * @author  Andreas Gohr <andi@splitbrain.org>
123723a34783SAndreas Gohr     */
123855efc227SAndreas Gohr    function _parseFileInfo()
123955efc227SAndreas Gohr    {
124055efc227SAndreas Gohr        if (file_exists($this->_fileName)) {
124155efc227SAndreas Gohr            $this->_info['file'] = array();
124255efc227SAndreas Gohr            $this->_info['file']['Name'] = basename($this->_fileName);
124300976812SAndreas Gohr            $this->_info['file']['Path'] = fullpath($this->_fileName);
124455efc227SAndreas Gohr            $this->_info['file']['Size'] = filesize($this->_fileName);
124555efc227SAndreas Gohr            if ($this->_info['file']['Size'] < 1024) {
124655efc227SAndreas Gohr                $this->_info['file']['NiceSize'] = $this->_info['file']['Size'] . 'B';
124755efc227SAndreas Gohr            }
124855efc227SAndreas Gohr            elseif ($this->_info['file']['Size'] < (1024 * 1024)) {
124955efc227SAndreas Gohr                $this->_info['file']['NiceSize'] = round($this->_info['file']['Size'] / 1024) . 'KB';
125055efc227SAndreas Gohr            }
125155efc227SAndreas Gohr            elseif ($this->_info['file']['Size'] < (1024 * 1024 * 1024)) {
1252fe00a666SAndreas Gohr                $this->_info['file']['NiceSize'] = round($this->_info['file']['Size'] / (1024*1024)) . 'MB';
125355efc227SAndreas Gohr            }
125455efc227SAndreas Gohr            else {
125555efc227SAndreas Gohr                $this->_info['file']['NiceSize'] = $this->_info['file']['Size'] . 'B';
125655efc227SAndreas Gohr            }
125755efc227SAndreas Gohr            $this->_info['file']['UnixTime'] = filemtime($this->_fileName);
125855efc227SAndreas Gohr
125955efc227SAndreas Gohr            // get image size directly from file
126055efc227SAndreas Gohr            $size = getimagesize($this->_fileName);
126155efc227SAndreas Gohr            $this->_info['file']['Width']  = $size[0];
126255efc227SAndreas Gohr            $this->_info['file']['Height'] = $size[1];
126355efc227SAndreas Gohr            // set mime types and formats
126455efc227SAndreas Gohr            // http://www.php.net/manual/en/function.getimagesize.php
126555efc227SAndreas Gohr            // http://www.php.net/manual/en/function.image-type-to-mime-type.php
126655efc227SAndreas Gohr            switch ($size[2]){
126755efc227SAndreas Gohr                case 1:
126855efc227SAndreas Gohr                    $this->_info['file']['Mime']   = 'image/gif';
126955efc227SAndreas Gohr                    $this->_info['file']['Format'] = 'GIF';
127055efc227SAndreas Gohr                    break;
127155efc227SAndreas Gohr                case 2:
127255efc227SAndreas Gohr                    $this->_info['file']['Mime']   = 'image/jpeg';
127355efc227SAndreas Gohr                    $this->_info['file']['Format'] = 'JPEG';
127455efc227SAndreas Gohr                    break;
127555efc227SAndreas Gohr                case 3:
127655efc227SAndreas Gohr                    $this->_info['file']['Mime']   = 'image/png';
127755efc227SAndreas Gohr                    $this->_info['file']['Format'] = 'PNG';
127855efc227SAndreas Gohr                    break;
127955efc227SAndreas Gohr                case 4:
128055efc227SAndreas Gohr                    $this->_info['file']['Mime']   = 'application/x-shockwave-flash';
128155efc227SAndreas Gohr                    $this->_info['file']['Format'] = 'SWF';
128255efc227SAndreas Gohr                    break;
128355efc227SAndreas Gohr                case 5:
128455efc227SAndreas Gohr                    $this->_info['file']['Mime']   = 'image/psd';
128555efc227SAndreas Gohr                    $this->_info['file']['Format'] = 'PSD';
128655efc227SAndreas Gohr                    break;
128755efc227SAndreas Gohr                case 6:
128855efc227SAndreas Gohr                    $this->_info['file']['Mime']   = 'image/bmp';
128955efc227SAndreas Gohr                    $this->_info['file']['Format'] = 'BMP';
129055efc227SAndreas Gohr                    break;
129155efc227SAndreas Gohr                case 7:
129255efc227SAndreas Gohr                    $this->_info['file']['Mime']   = 'image/tiff';
129355efc227SAndreas Gohr                    $this->_info['file']['Format'] = 'TIFF (Intel)';
129455efc227SAndreas Gohr                    break;
129555efc227SAndreas Gohr                case 8:
129655efc227SAndreas Gohr                    $this->_info['file']['Mime']   = 'image/tiff';
129755efc227SAndreas Gohr                    $this->_info['file']['Format'] = 'TIFF (Motorola)';
129855efc227SAndreas Gohr                    break;
129955efc227SAndreas Gohr                case 9:
130055efc227SAndreas Gohr                    $this->_info['file']['Mime']   = 'application/octet-stream';
130155efc227SAndreas Gohr                    $this->_info['file']['Format'] = 'JPC';
130255efc227SAndreas Gohr                    break;
130355efc227SAndreas Gohr                case 10:
130455efc227SAndreas Gohr                    $this->_info['file']['Mime']   = 'image/jp2';
130555efc227SAndreas Gohr                    $this->_info['file']['Format'] = 'JP2';
130655efc227SAndreas Gohr                    break;
130755efc227SAndreas Gohr                case 11:
130855efc227SAndreas Gohr                    $this->_info['file']['Mime']   = 'application/octet-stream';
130955efc227SAndreas Gohr                    $this->_info['file']['Format'] = 'JPX';
131055efc227SAndreas Gohr                    break;
131155efc227SAndreas Gohr                case 12:
131255efc227SAndreas Gohr                    $this->_info['file']['Mime']   = 'application/octet-stream';
131355efc227SAndreas Gohr                    $this->_info['file']['Format'] = 'JB2';
131455efc227SAndreas Gohr                    break;
131555efc227SAndreas Gohr                case 13:
131655efc227SAndreas Gohr                    $this->_info['file']['Mime']   = 'application/x-shockwave-flash';
131755efc227SAndreas Gohr                    $this->_info['file']['Format'] = 'SWC';
131855efc227SAndreas Gohr                    break;
131955efc227SAndreas Gohr                case 14:
132055efc227SAndreas Gohr                    $this->_info['file']['Mime']   = 'image/iff';
132155efc227SAndreas Gohr                    $this->_info['file']['Format'] = 'IFF';
132255efc227SAndreas Gohr                    break;
132355efc227SAndreas Gohr                case 15:
132455efc227SAndreas Gohr                    $this->_info['file']['Mime']   = 'image/vnd.wap.wbmp';
132555efc227SAndreas Gohr                    $this->_info['file']['Format'] = 'WBMP';
132655efc227SAndreas Gohr                    break;
132755efc227SAndreas Gohr                case 16:
132855efc227SAndreas Gohr                    $this->_info['file']['Mime']   = 'image/xbm';
132955efc227SAndreas Gohr                    $this->_info['file']['Format'] = 'XBM';
133055efc227SAndreas Gohr                    break;
133155efc227SAndreas Gohr                default:
133255efc227SAndreas Gohr                    $this->_info['file']['Mime']   = 'image/unknown';
133355efc227SAndreas Gohr            }
133455efc227SAndreas Gohr        }
133555efc227SAndreas Gohr        else {
133655efc227SAndreas Gohr            $this->_info['file'] = array();
133755efc227SAndreas Gohr            $this->_info['file']['Name'] = basename($this->_fileName);
133855efc227SAndreas Gohr            $this->_info['file']['Url'] = $this->_fileName;
133955efc227SAndreas Gohr        }
134055efc227SAndreas Gohr
134155efc227SAndreas Gohr        return true;
134255efc227SAndreas Gohr    }
134355efc227SAndreas Gohr
134455efc227SAndreas Gohr    /*************************************************************/
134555efc227SAndreas Gohr    function _parseMarkerJFIF()
134655efc227SAndreas Gohr    {
134755efc227SAndreas Gohr        if (!isset($this->_markers)) {
134855efc227SAndreas Gohr            $this->_readJPEG();
134955efc227SAndreas Gohr        }
135055efc227SAndreas Gohr
135155efc227SAndreas Gohr        if ($this->_markers == null) {
135255efc227SAndreas Gohr            return false;
135355efc227SAndreas Gohr        }
135455efc227SAndreas Gohr
135555efc227SAndreas Gohr        $data = null;
135655efc227SAndreas Gohr        $count = count($this->_markers);
135755efc227SAndreas Gohr        for ($i = 0; $i < $count; $i++) {
135855efc227SAndreas Gohr            if ($this->_markers[$i]['marker'] == 0xE0) {
135955efc227SAndreas Gohr                $signature = $this->_getFixedString($this->_markers[$i]['data'], 0, 4);
136055efc227SAndreas Gohr                if ($signature == 'JFIF') {
136155efc227SAndreas Gohr                    $data =& $this->_markers[$i]['data'];
136255efc227SAndreas Gohr                    break;
136355efc227SAndreas Gohr                }
136455efc227SAndreas Gohr            }
136555efc227SAndreas Gohr        }
136655efc227SAndreas Gohr
136755efc227SAndreas Gohr        if ($data == null) {
136855efc227SAndreas Gohr            $this->_info['jfif'] = false;
136955efc227SAndreas Gohr            return false;
137055efc227SAndreas Gohr        }
137155efc227SAndreas Gohr
137255efc227SAndreas Gohr        $pos = 0;
137355efc227SAndreas Gohr        $this->_info['jfif'] = array();
137455efc227SAndreas Gohr
137555efc227SAndreas Gohr
137655efc227SAndreas Gohr        $vmaj = $this->_getByte($data, 5);
137755efc227SAndreas Gohr        $vmin = $this->_getByte($data, 6);
137855efc227SAndreas Gohr
137955efc227SAndreas Gohr        $this->_info['jfif']['Version'] = sprintf('%d.%02d', $vmaj, $vmin);
138055efc227SAndreas Gohr
138155efc227SAndreas Gohr        $units = $this->_getByte($data, 7);
138255efc227SAndreas Gohr        switch ($units) {
138355efc227SAndreas Gohr        case 0:
138455efc227SAndreas Gohr            $this->_info['jfif']['Units'] = 'pixels';
138555efc227SAndreas Gohr            break;
138655efc227SAndreas Gohr        case 1:
138755efc227SAndreas Gohr            $this->_info['jfif']['Units'] = 'dpi';
138855efc227SAndreas Gohr            break;
138955efc227SAndreas Gohr        case 2:
139055efc227SAndreas Gohr            $this->_info['jfif']['Units'] = 'dpcm';
139155efc227SAndreas Gohr            break;
139255efc227SAndreas Gohr        default:
139355efc227SAndreas Gohr            $this->_info['jfif']['Units'] = 'unknown';
139455efc227SAndreas Gohr            break;
139555efc227SAndreas Gohr        }
139655efc227SAndreas Gohr
139755efc227SAndreas Gohr        $xdens = $this->_getShort($data, 8);
139855efc227SAndreas Gohr        $ydens = $this->_getShort($data, 10);
139955efc227SAndreas Gohr
140055efc227SAndreas Gohr        $this->_info['jfif']['XDensity'] = $xdens;
140155efc227SAndreas Gohr        $this->_info['jfif']['YDensity'] = $ydens;
140255efc227SAndreas Gohr
140355efc227SAndreas Gohr        $thumbx = $this->_getByte($data, 12);
140455efc227SAndreas Gohr        $thumby = $this->_getByte($data, 13);
140555efc227SAndreas Gohr
140655efc227SAndreas Gohr        $this->_info['jfif']['ThumbnailWidth'] = $thumbx;
140755efc227SAndreas Gohr        $this->_info['jfif']['ThumbnailHeight'] = $thumby;
140855efc227SAndreas Gohr
140955efc227SAndreas Gohr        return true;
141055efc227SAndreas Gohr    }
141155efc227SAndreas Gohr
141255efc227SAndreas Gohr    /*************************************************************/
141355efc227SAndreas Gohr    function _parseMarkerSOF()
141455efc227SAndreas Gohr    {
141555efc227SAndreas Gohr        if (!isset($this->_markers)) {
141655efc227SAndreas Gohr            $this->_readJPEG();
141755efc227SAndreas Gohr        }
141855efc227SAndreas Gohr
141955efc227SAndreas Gohr        if ($this->_markers == null) {
142055efc227SAndreas Gohr            return false;
142155efc227SAndreas Gohr        }
142255efc227SAndreas Gohr
142355efc227SAndreas Gohr        $data = null;
142455efc227SAndreas Gohr        $count = count($this->_markers);
142555efc227SAndreas Gohr        for ($i = 0; $i < $count; $i++) {
142655efc227SAndreas Gohr            switch ($this->_markers[$i]['marker']) {
142755efc227SAndreas Gohr            case 0xC0: // SOF0
142855efc227SAndreas Gohr            case 0xC1: // SOF1
142955efc227SAndreas Gohr            case 0xC2: // SOF2
143055efc227SAndreas Gohr            case 0xC9: // SOF9
143155efc227SAndreas Gohr                $data =& $this->_markers[$i]['data'];
143255efc227SAndreas Gohr                $marker = $this->_markers[$i]['marker'];
143355efc227SAndreas Gohr                break;
143455efc227SAndreas Gohr            }
143555efc227SAndreas Gohr        }
143655efc227SAndreas Gohr
143755efc227SAndreas Gohr        if ($data == null) {
143855efc227SAndreas Gohr            $this->_info['sof'] = false;
143955efc227SAndreas Gohr            return false;
144055efc227SAndreas Gohr        }
144155efc227SAndreas Gohr
144255efc227SAndreas Gohr        $pos = 0;
144355efc227SAndreas Gohr        $this->_info['sof'] = array();
144455efc227SAndreas Gohr
144555efc227SAndreas Gohr
144655efc227SAndreas Gohr        switch ($marker) {
144755efc227SAndreas Gohr        case 0xC0: // SOF0
144855efc227SAndreas Gohr            $format = 'Baseline';
144955efc227SAndreas Gohr            break;
145055efc227SAndreas Gohr        case 0xC1: // SOF1
145155efc227SAndreas Gohr            $format = 'Progessive';
145255efc227SAndreas Gohr            break;
145355efc227SAndreas Gohr        case 0xC2: // SOF2
145455efc227SAndreas Gohr            $format = 'Non-baseline';
145555efc227SAndreas Gohr            break;
145655efc227SAndreas Gohr        case 0xC9: // SOF9
145755efc227SAndreas Gohr            $format = 'Arithmetic';
145855efc227SAndreas Gohr            break;
145955efc227SAndreas Gohr        default:
146055efc227SAndreas Gohr            return false;
146155efc227SAndreas Gohr            break;
146255efc227SAndreas Gohr        }
146355efc227SAndreas Gohr
146455efc227SAndreas Gohr
146555efc227SAndreas Gohr        $this->_info['sof']['Format'] = $format;
146655efc227SAndreas Gohr
146755efc227SAndreas Gohr        $this->_info['sof']['SamplePrecision'] = $this->_getByte($data, $pos + 0);
146855efc227SAndreas Gohr        $this->_info['sof']['ImageHeight'] = $this->_getShort($data, $pos + 1);
146955efc227SAndreas Gohr        $this->_info['sof']['ImageWidth'] = $this->_getShort($data, $pos + 3);
147055efc227SAndreas Gohr        $this->_info['sof']['ColorChannels'] = $this->_getByte($data, $pos + 5);
147155efc227SAndreas Gohr
147255efc227SAndreas Gohr        return true;
147355efc227SAndreas Gohr    }
147455efc227SAndreas Gohr
1475431c7fc8Shakan.sandell    /**
1476431c7fc8Shakan.sandell     * Parses the XMP data
1477431c7fc8Shakan.sandell     *
1478431c7fc8Shakan.sandell     * @author  Hakan Sandell <hakan.sandell@mydata.se>
1479431c7fc8Shakan.sandell     */
1480431c7fc8Shakan.sandell    function _parseMarkerXmp()
1481431c7fc8Shakan.sandell    {
1482431c7fc8Shakan.sandell        if (!isset($this->_markers)) {
1483431c7fc8Shakan.sandell            $this->_readJPEG();
1484431c7fc8Shakan.sandell        }
1485431c7fc8Shakan.sandell
1486431c7fc8Shakan.sandell        if ($this->_markers == null) {
1487431c7fc8Shakan.sandell            return false;
1488431c7fc8Shakan.sandell        }
1489431c7fc8Shakan.sandell
1490431c7fc8Shakan.sandell        $data = null;
1491431c7fc8Shakan.sandell        $count = count($this->_markers);
1492431c7fc8Shakan.sandell        for ($i = 0; $i < $count; $i++) {
1493431c7fc8Shakan.sandell            if ($this->_markers[$i]['marker'] == 0xE1) {
1494431c7fc8Shakan.sandell                $signature = $this->_getFixedString($this->_markers[$i]['data'], 0, 29);
1495431c7fc8Shakan.sandell                if ($signature == "http://ns.adobe.com/xap/1.0/\0") {
1496431c7fc8Shakan.sandell                    $data =& substr($this->_markers[$i]['data'], 29);
1497431c7fc8Shakan.sandell                    break;
1498431c7fc8Shakan.sandell                }
1499431c7fc8Shakan.sandell            }
1500431c7fc8Shakan.sandell        }
1501431c7fc8Shakan.sandell
1502431c7fc8Shakan.sandell        if ($data == null) {
1503431c7fc8Shakan.sandell            $this->_info['xmp'] = false;
1504431c7fc8Shakan.sandell            return false;
1505431c7fc8Shakan.sandell        }
1506431c7fc8Shakan.sandell
1507431c7fc8Shakan.sandell        $parser = xml_parser_create();
1508431c7fc8Shakan.sandell        xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
1509431c7fc8Shakan.sandell        xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
1510431c7fc8Shakan.sandell        xml_parse_into_struct($parser, $data, $values, $tags);
1511431c7fc8Shakan.sandell        xml_parser_free($parser);
1512431c7fc8Shakan.sandell
1513431c7fc8Shakan.sandell        $this->_info['xmp'] = array();
1514431c7fc8Shakan.sandell        $count = count($values);
1515431c7fc8Shakan.sandell        for ($i = 0; $i < $count; $i++) {
1516431c7fc8Shakan.sandell            if ($values[$i][tag] == 'rdf:Description' && $values[$i][type] == 'open') {
1517431c7fc8Shakan.sandell
1518431c7fc8Shakan.sandell                while ($values[++$i][tag] != 'rdf:Description') {
1519431c7fc8Shakan.sandell                    $this->_parseXmpNode($values, $i, $this->_info['xmp'][$values[$i][tag]]);
1520431c7fc8Shakan.sandell                }
1521431c7fc8Shakan.sandell            }
1522431c7fc8Shakan.sandell        }
1523431c7fc8Shakan.sandell        return true;
1524431c7fc8Shakan.sandell    }
1525431c7fc8Shakan.sandell
1526431c7fc8Shakan.sandell    /**
1527431c7fc8Shakan.sandell     * Parses XMP nodes by recursion
1528431c7fc8Shakan.sandell     *
1529431c7fc8Shakan.sandell     * @author  Hakan Sandell <hakan.sandell@mydata.se>
1530431c7fc8Shakan.sandell     */
1531431c7fc8Shakan.sandell    function _parseXmpNode($values, &$i, &$meta)
1532431c7fc8Shakan.sandell    {
1533431c7fc8Shakan.sandell        if ($values[$i][type] == 'complete') {
1534431c7fc8Shakan.sandell            // Simple Type property
1535431c7fc8Shakan.sandell            $meta = $values[$i][value];
1536431c7fc8Shakan.sandell            return;
1537431c7fc8Shakan.sandell        }
1538431c7fc8Shakan.sandell
1539431c7fc8Shakan.sandell        $i++;
1540431c7fc8Shakan.sandell        if ($values[$i][tag] == 'rdf:Bag' || $values[$i][tag] == 'rdf:Seq') {
1541431c7fc8Shakan.sandell            // Array property
1542431c7fc8Shakan.sandell            $meta = array();
1543431c7fc8Shakan.sandell            while ($values[++$i][tag] == 'rdf:li') {
1544431c7fc8Shakan.sandell                $this->_parseXmpNode($values, $i, $meta[]);
1545431c7fc8Shakan.sandell            }
1546431c7fc8Shakan.sandell            $i++; // skip closing tag
1547431c7fc8Shakan.sandell
1548431c7fc8Shakan.sandell        } elseif ($values[$i][tag] == 'rdf:Alt') {
1549431c7fc8Shakan.sandell            // Language Alternative property, only the first (default) value is used
1550431c7fc8Shakan.sandell            $i++;
1551431c7fc8Shakan.sandell            $this->_parseXmpNode($values, $i, $meta);
1552431c7fc8Shakan.sandell            while ($values[++$i][tag] != 'rdf:Alt');
1553431c7fc8Shakan.sandell            $i++; // skip closing tag
1554431c7fc8Shakan.sandell
1555431c7fc8Shakan.sandell        } else {
1556431c7fc8Shakan.sandell            // Structure property
1557431c7fc8Shakan.sandell            $meta = array();
1558431c7fc8Shakan.sandell            $startTag = $values[$i-1][tag];
1559431c7fc8Shakan.sandell            do {
1560431c7fc8Shakan.sandell                $this->_parseXmpNode($values, $i, $meta[$values[$i][tag]]);
1561431c7fc8Shakan.sandell            } while ($values[++$i][tag] != $startTag);
1562431c7fc8Shakan.sandell        }
1563431c7fc8Shakan.sandell    }
1564431c7fc8Shakan.sandell
156555efc227SAndreas Gohr    /*************************************************************/
156655efc227SAndreas Gohr    function _parseMarkerExif()
156755efc227SAndreas Gohr    {
156855efc227SAndreas Gohr        if (!isset($this->_markers)) {
156955efc227SAndreas Gohr            $this->_readJPEG();
157055efc227SAndreas Gohr        }
157155efc227SAndreas Gohr
157255efc227SAndreas Gohr        if ($this->_markers == null) {
157355efc227SAndreas Gohr            return false;
157455efc227SAndreas Gohr        }
157555efc227SAndreas Gohr
157655efc227SAndreas Gohr        $data = null;
157755efc227SAndreas Gohr        $count = count($this->_markers);
157855efc227SAndreas Gohr        for ($i = 0; $i < $count; $i++) {
157955efc227SAndreas Gohr            if ($this->_markers[$i]['marker'] == 0xE1) {
158055efc227SAndreas Gohr                $signature = $this->_getFixedString($this->_markers[$i]['data'], 0, 6);
158155efc227SAndreas Gohr                if ($signature == "Exif\0\0") {
158255efc227SAndreas Gohr                    $data =& $this->_markers[$i]['data'];
158355efc227SAndreas Gohr                    break;
158455efc227SAndreas Gohr                }
158555efc227SAndreas Gohr            }
158655efc227SAndreas Gohr        }
158755efc227SAndreas Gohr
158855efc227SAndreas Gohr        if ($data == null) {
158955efc227SAndreas Gohr            $this->_info['exif'] = false;
159055efc227SAndreas Gohr            return false;
159155efc227SAndreas Gohr        }
159255efc227SAndreas Gohr        $pos = 6;
159355efc227SAndreas Gohr        $this->_info['exif'] = array();
159455efc227SAndreas Gohr
159555efc227SAndreas Gohr        // We don't increment $pos after this because Exif uses offsets relative to this point
159655efc227SAndreas Gohr
159755efc227SAndreas Gohr        $byteAlign = $this->_getShort($data, $pos + 0);
159855efc227SAndreas Gohr
159955efc227SAndreas Gohr        if ($byteAlign == 0x4949) { // "II"
160055efc227SAndreas Gohr            $isBigEndian = false;
160155efc227SAndreas Gohr        }
160255efc227SAndreas Gohr        elseif ($byteAlign == 0x4D4D) { // "MM"
160355efc227SAndreas Gohr            $isBigEndian = true;
160455efc227SAndreas Gohr        }
160555efc227SAndreas Gohr        else {
160655efc227SAndreas Gohr            return false; // Unexpected data
160755efc227SAndreas Gohr        }
160855efc227SAndreas Gohr
160955efc227SAndreas Gohr        $alignCheck = $this->_getShort($data, $pos + 2, $isBigEndian);
161055efc227SAndreas Gohr        if ($alignCheck != 0x002A) // That's the expected value
161155efc227SAndreas Gohr            return false; // Unexpected data
161255efc227SAndreas Gohr
161355efc227SAndreas Gohr        if ($isBigEndian) {
161455efc227SAndreas Gohr            $this->_info['exif']['ByteAlign'] = "Big Endian";
161555efc227SAndreas Gohr        }
161655efc227SAndreas Gohr        else {
161755efc227SAndreas Gohr            $this->_info['exif']['ByteAlign'] = "Little Endian";
161855efc227SAndreas Gohr        }
161955efc227SAndreas Gohr
162055efc227SAndreas Gohr        $offsetIFD0 = $this->_getLong($data, $pos + 4, $isBigEndian);
162155efc227SAndreas Gohr        if ($offsetIFD0 < 8)
162255efc227SAndreas Gohr            return false; // Unexpected data
162355efc227SAndreas Gohr
162455efc227SAndreas Gohr        $offsetIFD1 = $this->_readIFD($data, $pos, $offsetIFD0, $isBigEndian, 'ifd0');
162555efc227SAndreas Gohr        if ($offsetIFD1 != 0)
162655efc227SAndreas Gohr            $this->_readIFD($data, $pos, $offsetIFD1, $isBigEndian, 'ifd1');
162755efc227SAndreas Gohr
162855efc227SAndreas Gohr        return true;
162955efc227SAndreas Gohr    }
163055efc227SAndreas Gohr
163155efc227SAndreas Gohr    /*************************************************************/
163255efc227SAndreas Gohr    function _readIFD($data, $base, $offset, $isBigEndian, $mode)
163355efc227SAndreas Gohr    {
163455efc227SAndreas Gohr        $EXIFTags = $this->_exifTagNames($mode);
163555efc227SAndreas Gohr
163655efc227SAndreas Gohr        $numEntries = $this->_getShort($data, $base + $offset, $isBigEndian);
163755efc227SAndreas Gohr        $offset += 2;
163855efc227SAndreas Gohr
163955efc227SAndreas Gohr        $exifTIFFOffset = 0;
164055efc227SAndreas Gohr        $exifTIFFLength = 0;
164155efc227SAndreas Gohr        $exifThumbnailOffset = 0;
164255efc227SAndreas Gohr        $exifThumbnailLength = 0;
164355efc227SAndreas Gohr
164455efc227SAndreas Gohr        for ($i = 0; $i < $numEntries; $i++) {
164555efc227SAndreas Gohr            $tag = $this->_getShort($data, $base + $offset, $isBigEndian);
164655efc227SAndreas Gohr            $offset += 2;
164755efc227SAndreas Gohr            $type = $this->_getShort($data, $base + $offset, $isBigEndian);
164855efc227SAndreas Gohr            $offset += 2;
164955efc227SAndreas Gohr            $count = $this->_getLong($data, $base + $offset, $isBigEndian);
165055efc227SAndreas Gohr            $offset += 4;
165155efc227SAndreas Gohr
165255efc227SAndreas Gohr            if (($type < 1) || ($type > 12))
165355efc227SAndreas Gohr                return false; // Unexpected Type
165455efc227SAndreas Gohr
165555efc227SAndreas Gohr            $typeLengths = array( -1, 1, 1, 2, 4, 8, 1, 1, 2, 4, 8, 4, 8 );
165655efc227SAndreas Gohr
165755efc227SAndreas Gohr            $dataLength = $typeLengths[$type] * $count;
165855efc227SAndreas Gohr            if ($dataLength > 4) {
165955efc227SAndreas Gohr                $dataOffset = $this->_getLong($data, $base + $offset, $isBigEndian);
166055efc227SAndreas Gohr                $rawValue = $this->_getFixedString($data, $base + $dataOffset, $dataLength);
166155efc227SAndreas Gohr            }
166255efc227SAndreas Gohr            else {
166355efc227SAndreas Gohr                $rawValue = $this->_getFixedString($data, $base + $offset, $dataLength);
166455efc227SAndreas Gohr            }
166555efc227SAndreas Gohr            $offset += 4;
166655efc227SAndreas Gohr
166755efc227SAndreas Gohr            switch ($type) {
166855efc227SAndreas Gohr            case 1:    // UBYTE
166955efc227SAndreas Gohr                if ($count == 1) {
167055efc227SAndreas Gohr                    $value = $this->_getByte($rawValue, 0);
167155efc227SAndreas Gohr                }
167255efc227SAndreas Gohr                else {
167355efc227SAndreas Gohr                    $value = array();
167455efc227SAndreas Gohr                    for ($j = 0; $j < $count; $j++)
167555efc227SAndreas Gohr                        $value[$j] = $this->_getByte($rawValue, $j);
167655efc227SAndreas Gohr                }
167755efc227SAndreas Gohr                break;
167855efc227SAndreas Gohr            case 2:    // ASCII
167955efc227SAndreas Gohr                $value = $rawValue;
168055efc227SAndreas Gohr                break;
168155efc227SAndreas Gohr            case 3:    // USHORT
168255efc227SAndreas Gohr                if ($count == 1) {
168355efc227SAndreas Gohr                    $value = $this->_getShort($rawValue, 0, $isBigEndian);
168455efc227SAndreas Gohr                }
168555efc227SAndreas Gohr                else {
168655efc227SAndreas Gohr                    $value = array();
168755efc227SAndreas Gohr                    for ($j = 0; $j < $count; $j++)
168855efc227SAndreas Gohr                        $value[$j] = $this->_getShort($rawValue, $j * 2, $isBigEndian);
168955efc227SAndreas Gohr                }
169055efc227SAndreas Gohr                break;
169155efc227SAndreas Gohr            case 4:    // ULONG
169255efc227SAndreas Gohr                if ($count == 1) {
169355efc227SAndreas Gohr                    $value = $this->_getLong($rawValue, 0, $isBigEndian);
169455efc227SAndreas Gohr                }
169555efc227SAndreas Gohr                else {
169655efc227SAndreas Gohr                    $value = array();
169755efc227SAndreas Gohr                    for ($j = 0; $j < $count; $j++)
169855efc227SAndreas Gohr                        $value[$j] = $this->_getLong($rawValue, $j * 4, $isBigEndian);
169955efc227SAndreas Gohr                }
170055efc227SAndreas Gohr                break;
170155efc227SAndreas Gohr            case 5:    // URATIONAL
170255efc227SAndreas Gohr                if ($count == 1) {
170355efc227SAndreas Gohr                    $a = $this->_getLong($rawValue, 0, $isBigEndian);
170455efc227SAndreas Gohr                    $b = $this->_getLong($rawValue, 4, $isBigEndian);
170555efc227SAndreas Gohr                    $value = array();
170655efc227SAndreas Gohr                    $value['val'] = 0;
170755efc227SAndreas Gohr                    $value['num'] = $a;
170855efc227SAndreas Gohr                    $value['den'] = $b;
170955efc227SAndreas Gohr                    if (($a != 0) && ($b != 0)) {
171055efc227SAndreas Gohr                        $value['val'] = $a / $b;
171155efc227SAndreas Gohr                    }
171255efc227SAndreas Gohr                }
171355efc227SAndreas Gohr                else {
171455efc227SAndreas Gohr                    $value = array();
171555efc227SAndreas Gohr                    for ($j = 0; $j < $count; $j++) {
171655efc227SAndreas Gohr                        $a = $this->_getLong($rawValue, $j * 8, $isBigEndian);
171755efc227SAndreas Gohr                        $b = $this->_getLong($rawValue, ($j * 8) + 4, $isBigEndian);
171855efc227SAndreas Gohr                        $value = array();
171955efc227SAndreas Gohr                        $value[$j]['val'] = 0;
172055efc227SAndreas Gohr                        $value[$j]['num'] = $a;
172155efc227SAndreas Gohr                        $value[$j]['den'] = $b;
172255efc227SAndreas Gohr                        if (($a != 0) && ($b != 0))
172355efc227SAndreas Gohr                            $value[$j]['val'] = $a / $b;
172455efc227SAndreas Gohr                    }
172555efc227SAndreas Gohr                }
172655efc227SAndreas Gohr                break;
172755efc227SAndreas Gohr            case 6:    // SBYTE
172855efc227SAndreas Gohr                if ($count == 1) {
172955efc227SAndreas Gohr                    $value = $this->_getByte($rawValue, 0);
173055efc227SAndreas Gohr                }
173155efc227SAndreas Gohr                else {
173255efc227SAndreas Gohr                    $value = array();
173355efc227SAndreas Gohr                    for ($j = 0; $j < $count; $j++)
173455efc227SAndreas Gohr                        $value[$j] = $this->_getByte($rawValue, $j);
173555efc227SAndreas Gohr                }
173655efc227SAndreas Gohr                break;
173755efc227SAndreas Gohr            case 7:    // UNDEFINED
173855efc227SAndreas Gohr                $value = $rawValue;
173955efc227SAndreas Gohr                break;
174055efc227SAndreas Gohr            case 8:    // SSHORT
174155efc227SAndreas Gohr                if ($count == 1) {
174255efc227SAndreas Gohr                    $value = $this->_getShort($rawValue, 0, $isBigEndian);
174355efc227SAndreas Gohr                }
174455efc227SAndreas Gohr                else {
174555efc227SAndreas Gohr                    $value = array();
174655efc227SAndreas Gohr                    for ($j = 0; $j < $count; $j++)
174755efc227SAndreas Gohr                        $value[$j] = $this->_getShort($rawValue, $j * 2, $isBigEndian);
174855efc227SAndreas Gohr                }
174955efc227SAndreas Gohr                break;
175055efc227SAndreas Gohr            case 9:    // SLONG
175155efc227SAndreas Gohr                if ($count == 1) {
175255efc227SAndreas Gohr                    $value = $this->_getLong($rawValue, 0, $isBigEndian);
175355efc227SAndreas Gohr                }
175455efc227SAndreas Gohr                else {
175555efc227SAndreas Gohr                    $value = array();
175655efc227SAndreas Gohr                    for ($j = 0; $j < $count; $j++)
175755efc227SAndreas Gohr                        $value[$j] = $this->_getLong($rawValue, $j * 4, $isBigEndian);
175855efc227SAndreas Gohr                }
175955efc227SAndreas Gohr                break;
176055efc227SAndreas Gohr            case 10:   // SRATIONAL
176155efc227SAndreas Gohr                if ($count == 1) {
176255efc227SAndreas Gohr                    $a = $this->_getLong($rawValue, 0, $isBigEndian);
176355efc227SAndreas Gohr                    $b = $this->_getLong($rawValue, 4, $isBigEndian);
176455efc227SAndreas Gohr                    $value = array();
176555efc227SAndreas Gohr                    $value['val'] = 0;
176655efc227SAndreas Gohr                    $value['num'] = $a;
176755efc227SAndreas Gohr                    $value['den'] = $b;
176855efc227SAndreas Gohr                    if (($a != 0) && ($b != 0))
176955efc227SAndreas Gohr                        $value['val'] = $a / $b;
177055efc227SAndreas Gohr                }
177155efc227SAndreas Gohr                else {
177255efc227SAndreas Gohr                    $value = array();
177355efc227SAndreas Gohr                    for ($j = 0; $j < $count; $j++) {
177455efc227SAndreas Gohr                        $a = $this->_getLong($rawValue, $j * 8, $isBigEndian);
177555efc227SAndreas Gohr                        $b = $this->_getLong($rawValue, ($j * 8) + 4, $isBigEndian);
177655efc227SAndreas Gohr                        $value = array();
177755efc227SAndreas Gohr                        $value[$j]['val'] = 0;
177855efc227SAndreas Gohr                        $value[$j]['num'] = $a;
177955efc227SAndreas Gohr                        $value[$j]['den'] = $b;
178055efc227SAndreas Gohr                        if (($a != 0) && ($b != 0))
178155efc227SAndreas Gohr                            $value[$j]['val'] = $a / $b;
178255efc227SAndreas Gohr                    }
178355efc227SAndreas Gohr                }
178455efc227SAndreas Gohr                break;
178555efc227SAndreas Gohr            case 11:   // FLOAT
178655efc227SAndreas Gohr                $value = $rawValue;
178755efc227SAndreas Gohr                break;
178855efc227SAndreas Gohr
178955efc227SAndreas Gohr            case 12:   // DFLOAT
179055efc227SAndreas Gohr                $value = $rawValue;
179155efc227SAndreas Gohr                break;
179255efc227SAndreas Gohr            default:
179355efc227SAndreas Gohr                return false; // Unexpected Type
179455efc227SAndreas Gohr            }
179555efc227SAndreas Gohr
179655efc227SAndreas Gohr            $tagName = '';
179755efc227SAndreas Gohr            if (($mode == 'ifd0') && ($tag == 0x8769)) {  // ExifIFDOffset
179855efc227SAndreas Gohr                $this->_readIFD($data, $base, $value, $isBigEndian, 'exif');
179955efc227SAndreas Gohr            }
180055efc227SAndreas Gohr            elseif (($mode == 'ifd0') && ($tag == 0x8825)) {  // GPSIFDOffset
180155efc227SAndreas Gohr                $this->_readIFD($data, $base, $value, $isBigEndian, 'gps');
180255efc227SAndreas Gohr            }
180355efc227SAndreas Gohr            elseif (($mode == 'ifd1') && ($tag == 0x0111)) {  // TIFFStripOffsets
180455efc227SAndreas Gohr                $exifTIFFOffset = $value;
180555efc227SAndreas Gohr            }
180655efc227SAndreas Gohr            elseif (($mode == 'ifd1') && ($tag == 0x0117)) {  // TIFFStripByteCounts
180755efc227SAndreas Gohr                $exifTIFFLength = $value;
180855efc227SAndreas Gohr            }
180955efc227SAndreas Gohr            elseif (($mode == 'ifd1') && ($tag == 0x0201)) {  // TIFFJFIFOffset
181055efc227SAndreas Gohr                $exifThumbnailOffset = $value;
181155efc227SAndreas Gohr            }
181255efc227SAndreas Gohr            elseif (($mode == 'ifd1') && ($tag == 0x0202)) {  // TIFFJFIFLength
181355efc227SAndreas Gohr                $exifThumbnailLength = $value;
181455efc227SAndreas Gohr            }
181555efc227SAndreas Gohr            elseif (($mode == 'exif') && ($tag == 0xA005)) {  // InteropIFDOffset
181655efc227SAndreas Gohr                $this->_readIFD($data, $base, $value, $isBigEndian, 'interop');
181755efc227SAndreas Gohr            }
181855efc227SAndreas Gohr            // elseif (($mode == 'exif') && ($tag == 0x927C)) {  // MakerNote
181955efc227SAndreas Gohr            // }
182055efc227SAndreas Gohr            else {
182155efc227SAndreas Gohr                if (isset($EXIFTags[$tag])) {
182255efc227SAndreas Gohr                    $tagName = $EXIFTags[$tag];
182355efc227SAndreas Gohr                    if (isset($this->_info['exif'][$tagName])) {
182455efc227SAndreas Gohr                        if (!is_array($this->_info['exif'][$tagName])) {
182555efc227SAndreas Gohr                            $aux = array();
182655efc227SAndreas Gohr                            $aux[0] = $this->_info['exif'][$tagName];
182755efc227SAndreas Gohr                            $this->_info['exif'][$tagName] = $aux;
182855efc227SAndreas Gohr                        }
182955efc227SAndreas Gohr
183055efc227SAndreas Gohr                        $this->_info['exif'][$tagName][count($this->_info['exif'][$tagName])] = $value;
183155efc227SAndreas Gohr                    }
183255efc227SAndreas Gohr                    else {
183355efc227SAndreas Gohr                        $this->_info['exif'][$tagName] = $value;
183455efc227SAndreas Gohr                    }
183555efc227SAndreas Gohr                }
183655efc227SAndreas Gohr                else {
183755efc227SAndreas Gohr#echo sprintf("<h1>Unknown tag %02x (t: %d l: %d) %s in %s</h1>", $tag, $type, $count, $mode, $this->_fileName);
183855efc227SAndreas Gohr                    // Unknown Tags will be ignored!!!
183955efc227SAndreas Gohr                    // That's because the tag might be a pointer (like the Exif tag)
184055efc227SAndreas Gohr                    // and saving it without saving the data it points to might
184155efc227SAndreas Gohr                    // create an invalid file.
184255efc227SAndreas Gohr                }
184355efc227SAndreas Gohr            }
184455efc227SAndreas Gohr        }
184555efc227SAndreas Gohr
184655efc227SAndreas Gohr        if (($exifThumbnailOffset > 0) && ($exifThumbnailLength > 0)) {
184755efc227SAndreas Gohr            $this->_info['exif']['JFIFThumbnail'] = $this->_getFixedString($data, $base + $exifThumbnailOffset, $exifThumbnailLength);
184855efc227SAndreas Gohr        }
184955efc227SAndreas Gohr
185055efc227SAndreas Gohr        if (($exifTIFFOffset > 0) && ($exifTIFFLength > 0)) {
185155efc227SAndreas Gohr            $this->_info['exif']['TIFFStrips'] = $this->_getFixedString($data, $base + $exifTIFFOffset, $exifTIFFLength);
185255efc227SAndreas Gohr        }
185355efc227SAndreas Gohr
185455efc227SAndreas Gohr        $nextOffset = $this->_getLong($data, $base + $offset, $isBigEndian);
185555efc227SAndreas Gohr        return $nextOffset;
185655efc227SAndreas Gohr    }
185755efc227SAndreas Gohr
185855efc227SAndreas Gohr    /*************************************************************/
185955efc227SAndreas Gohr    function & _createMarkerExif()
186055efc227SAndreas Gohr    {
186155efc227SAndreas Gohr        $data = null;
186255efc227SAndreas Gohr        $count = count($this->_markers);
186355efc227SAndreas Gohr        for ($i = 0; $i < $count; $i++) {
186455efc227SAndreas Gohr            if ($this->_markers[$i]['marker'] == 0xE1) {
186555efc227SAndreas Gohr                $signature = $this->_getFixedString($this->_markers[$i]['data'], 0, 6);
186655efc227SAndreas Gohr                if ($signature == "Exif\0\0") {
186755efc227SAndreas Gohr                    $data =& $this->_markers[$i]['data'];
186855efc227SAndreas Gohr                    break;
186955efc227SAndreas Gohr                }
187055efc227SAndreas Gohr            }
187155efc227SAndreas Gohr        }
187255efc227SAndreas Gohr
187355efc227SAndreas Gohr        if (!isset($this->_info['exif'])) {
187455efc227SAndreas Gohr            return false;
187555efc227SAndreas Gohr        }
187655efc227SAndreas Gohr
187755efc227SAndreas Gohr        $data = "Exif\0\0";
187855efc227SAndreas Gohr        $pos = 6;
187955efc227SAndreas Gohr        $offsetBase = 6;
188055efc227SAndreas Gohr
188155efc227SAndreas Gohr        if (isset($this->_info['exif']['ByteAlign']) && ($this->_info['exif']['ByteAlign'] == "Big Endian")) {
188255efc227SAndreas Gohr            $isBigEndian = true;
188355efc227SAndreas Gohr            $aux = "MM";
188455efc227SAndreas Gohr            $pos = $this->_putString($data, $pos, $aux);
188555efc227SAndreas Gohr        }
188655efc227SAndreas Gohr        else {
188755efc227SAndreas Gohr            $isBigEndian = false;
188855efc227SAndreas Gohr            $aux = "II";
188955efc227SAndreas Gohr            $pos = $this->_putString($data, $pos, $aux);
189055efc227SAndreas Gohr        }
189155efc227SAndreas Gohr        $pos = $this->_putShort($data, $pos, 0x002A, $isBigEndian);
189255efc227SAndreas Gohr        $pos = $this->_putLong($data, $pos, 0x00000008, $isBigEndian); // IFD0 Offset is always 8
189355efc227SAndreas Gohr
189455efc227SAndreas Gohr        $ifd0 =& $this->_getIFDEntries($isBigEndian, 'ifd0');
189555efc227SAndreas Gohr        $ifd1 =& $this->_getIFDEntries($isBigEndian, 'ifd1');
189655efc227SAndreas Gohr
189755efc227SAndreas Gohr        $pos = $this->_writeIFD($data, $pos, $offsetBase, $ifd0, $isBigEndian, true);
189855efc227SAndreas Gohr        $pos = $this->_writeIFD($data, $pos, $offsetBase, $ifd1, $isBigEndian, false);
189955efc227SAndreas Gohr
190055efc227SAndreas Gohr        return $data;
190155efc227SAndreas Gohr    }
190255efc227SAndreas Gohr
190355efc227SAndreas Gohr    /*************************************************************/
190455efc227SAndreas Gohr    function _writeIFD(&$data, $pos, $offsetBase, &$entries, $isBigEndian, $hasNext)
190555efc227SAndreas Gohr    {
190655efc227SAndreas Gohr        $tiffData = null;
190755efc227SAndreas Gohr        $tiffDataOffsetPos = -1;
190855efc227SAndreas Gohr
190955efc227SAndreas Gohr        $entryCount = count($entries);
191055efc227SAndreas Gohr
191155efc227SAndreas Gohr        $dataPos = $pos + 2 + ($entryCount * 12) + 4;
191255efc227SAndreas Gohr        $pos = $this->_putShort($data, $pos, $entryCount, $isBigEndian);
191355efc227SAndreas Gohr
191455efc227SAndreas Gohr        for ($i = 0; $i < $entryCount; $i++) {
191555efc227SAndreas Gohr            $tag = $entries[$i]['tag'];
191655efc227SAndreas Gohr            $type = $entries[$i]['type'];
191755efc227SAndreas Gohr
191855efc227SAndreas Gohr            if ($type == -99) { // SubIFD
191955efc227SAndreas Gohr                $pos = $this->_putShort($data, $pos, $tag, $isBigEndian);
192055efc227SAndreas Gohr                $pos = $this->_putShort($data, $pos, 0x04, $isBigEndian); // LONG
192155efc227SAndreas Gohr                $pos = $this->_putLong($data, $pos, 0x01, $isBigEndian); // Count = 1
192255efc227SAndreas Gohr                $pos = $this->_putLong($data, $pos, $dataPos - $offsetBase, $isBigEndian);
192355efc227SAndreas Gohr
192455efc227SAndreas Gohr                $dataPos = $this->_writeIFD($data, $dataPos, $offsetBase, $entries[$i]['value'], $isBigEndian, false);
192555efc227SAndreas Gohr            }
192655efc227SAndreas Gohr            elseif ($type == -98) { // TIFF Data
192755efc227SAndreas Gohr                $pos = $this->_putShort($data, $pos, $tag, $isBigEndian);
192855efc227SAndreas Gohr                $pos = $this->_putShort($data, $pos, 0x04, $isBigEndian); // LONG
192955efc227SAndreas Gohr                $pos = $this->_putLong($data, $pos, 0x01, $isBigEndian); // Count = 1
193055efc227SAndreas Gohr                $tiffDataOffsetPos = $pos;
193155efc227SAndreas Gohr                $pos = $this->_putLong($data, $pos, 0x00, $isBigEndian); // For Now
193255efc227SAndreas Gohr                $tiffData =& $entries[$i]['value'] ;
193355efc227SAndreas Gohr            }
193455efc227SAndreas Gohr            else { // Regular Entry
193555efc227SAndreas Gohr                $pos = $this->_putShort($data, $pos, $tag, $isBigEndian);
193655efc227SAndreas Gohr                $pos = $this->_putShort($data, $pos, $type, $isBigEndian);
193755efc227SAndreas Gohr                $pos = $this->_putLong($data, $pos, $entries[$i]['count'], $isBigEndian);
193855efc227SAndreas Gohr                if (strlen($entries[$i]['value']) > 4) {
193955efc227SAndreas Gohr                    $pos = $this->_putLong($data, $pos, $dataPos - $offsetBase, $isBigEndian);
194055efc227SAndreas Gohr                    $dataPos = $this->_putString($data, $dataPos, $entries[$i]['value']);
194155efc227SAndreas Gohr                }
194255efc227SAndreas Gohr                else {
194355efc227SAndreas Gohr                    $val = str_pad($entries[$i]['value'], 4, "\0");
194455efc227SAndreas Gohr                    $pos = $this->_putString($data, $pos, $val);
194555efc227SAndreas Gohr                }
194655efc227SAndreas Gohr            }
194755efc227SAndreas Gohr        }
194855efc227SAndreas Gohr
194955efc227SAndreas Gohr        if ($tiffData != null) {
195055efc227SAndreas Gohr            $this->_putLong($data, $tiffDataOffsetPos, $dataPos - $offsetBase, $isBigEndian);
195155efc227SAndreas Gohr            $dataPos = $this->_putString($data, $dataPos, $tiffData);
195255efc227SAndreas Gohr        }
195355efc227SAndreas Gohr
195455efc227SAndreas Gohr        if ($hasNext) {
195555efc227SAndreas Gohr            $pos = $this->_putLong($data, $pos, $dataPos - $offsetBase, $isBigEndian);
195655efc227SAndreas Gohr        }
195755efc227SAndreas Gohr        else {
195855efc227SAndreas Gohr            $pos = $this->_putLong($data, $pos, 0, $isBigEndian);
195955efc227SAndreas Gohr        }
196055efc227SAndreas Gohr
196155efc227SAndreas Gohr        return $dataPos;
196255efc227SAndreas Gohr    }
196355efc227SAndreas Gohr
196455efc227SAndreas Gohr    /*************************************************************/
196555efc227SAndreas Gohr    function & _getIFDEntries($isBigEndian, $mode)
196655efc227SAndreas Gohr    {
196755efc227SAndreas Gohr        $EXIFNames = $this->_exifTagNames($mode);
196855efc227SAndreas Gohr        $EXIFTags = $this->_exifNameTags($mode);
196955efc227SAndreas Gohr        $EXIFTypeInfo = $this->_exifTagTypes($mode);
197055efc227SAndreas Gohr
197155efc227SAndreas Gohr        $ifdEntries = array();
197255efc227SAndreas Gohr        $entryCount = 0;
197355efc227SAndreas Gohr
197455efc227SAndreas Gohr        reset($EXIFNames);
197555efc227SAndreas Gohr        while (list($tag, $name) = each($EXIFNames)) {
197655efc227SAndreas Gohr            $type = $EXIFTypeInfo[$tag][0];
197755efc227SAndreas Gohr            $count = $EXIFTypeInfo[$tag][1];
197855efc227SAndreas Gohr            $value = null;
197955efc227SAndreas Gohr
198055efc227SAndreas Gohr            if (($mode == 'ifd0') && ($tag == 0x8769)) {  // ExifIFDOffset
198155efc227SAndreas Gohr                if (isset($this->_info['exif']['EXIFVersion'])) {
198255efc227SAndreas Gohr                    $value =& $this->_getIFDEntries($isBigEndian, "exif");
198355efc227SAndreas Gohr                    $type = -99;
198455efc227SAndreas Gohr                }
198555efc227SAndreas Gohr                else {
198655efc227SAndreas Gohr                    $value = null;
198755efc227SAndreas Gohr                }
198855efc227SAndreas Gohr            }
198955efc227SAndreas Gohr            elseif (($mode == 'ifd0') && ($tag == 0x8825)) {  // GPSIFDOffset
199055efc227SAndreas Gohr                if (isset($this->_info['exif']['GPSVersionID'])) {
199155efc227SAndreas Gohr                    $value =& $this->_getIFDEntries($isBigEndian, "gps");
199255efc227SAndreas Gohr                    $type = -99;
199355efc227SAndreas Gohr                }
199455efc227SAndreas Gohr                else {
199555efc227SAndreas Gohr                    $value = null;
199655efc227SAndreas Gohr                }
199755efc227SAndreas Gohr            }
199855efc227SAndreas Gohr            elseif (($mode == 'ifd1') && ($tag == 0x0111)) {  // TIFFStripOffsets
199955efc227SAndreas Gohr                if (isset($this->_info['exif']['TIFFStrips'])) {
200055efc227SAndreas Gohr                    $value =& $this->_info['exif']['TIFFStrips'];
200155efc227SAndreas Gohr                    $type = -98;
200255efc227SAndreas Gohr                }
200355efc227SAndreas Gohr                else {
200455efc227SAndreas Gohr                    $value = null;
200555efc227SAndreas Gohr                }
200655efc227SAndreas Gohr            }
200755efc227SAndreas Gohr            elseif (($mode == 'ifd1') && ($tag == 0x0117)) {  // TIFFStripByteCounts
200855efc227SAndreas Gohr                if (isset($this->_info['exif']['TIFFStrips'])) {
200955efc227SAndreas Gohr                    $value = strlen($this->_info['exif']['TIFFStrips']);
201055efc227SAndreas Gohr                }
201155efc227SAndreas Gohr                else {
201255efc227SAndreas Gohr                    $value = null;
201355efc227SAndreas Gohr                }
201455efc227SAndreas Gohr            }
201555efc227SAndreas Gohr            elseif (($mode == 'ifd1') && ($tag == 0x0201)) {  // TIFFJFIFOffset
201655efc227SAndreas Gohr                if (isset($this->_info['exif']['JFIFThumbnail'])) {
201755efc227SAndreas Gohr                    $value =& $this->_info['exif']['JFIFThumbnail'];
201855efc227SAndreas Gohr                    $type = -98;
201955efc227SAndreas Gohr                }
202055efc227SAndreas Gohr                else {
202155efc227SAndreas Gohr                    $value = null;
202255efc227SAndreas Gohr                }
202355efc227SAndreas Gohr            }
202455efc227SAndreas Gohr            elseif (($mode == 'ifd1') && ($tag == 0x0202)) {  // TIFFJFIFLength
202555efc227SAndreas Gohr                if (isset($this->_info['exif']['JFIFThumbnail'])) {
202655efc227SAndreas Gohr                    $value = strlen($this->_info['exif']['JFIFThumbnail']);
202755efc227SAndreas Gohr                }
202855efc227SAndreas Gohr                else {
202955efc227SAndreas Gohr                    $value = null;
203055efc227SAndreas Gohr                }
203155efc227SAndreas Gohr            }
203255efc227SAndreas Gohr            elseif (($mode == 'exif') && ($tag == 0xA005)) {  // InteropIFDOffset
203355efc227SAndreas Gohr                if (isset($this->_info['exif']['InteroperabilityIndex'])) {
203455efc227SAndreas Gohr                    $value =& $this->_getIFDEntries($isBigEndian, "interop");
203555efc227SAndreas Gohr                    $type = -99;
203655efc227SAndreas Gohr                }
203755efc227SAndreas Gohr                else {
203855efc227SAndreas Gohr                    $value = null;
203955efc227SAndreas Gohr                }
204055efc227SAndreas Gohr            }
204155efc227SAndreas Gohr            elseif (isset($this->_info['exif'][$name])) {
204255efc227SAndreas Gohr                $origValue =& $this->_info['exif'][$name];
204355efc227SAndreas Gohr
204455efc227SAndreas Gohr                // This makes it easier to process variable size elements
204555efc227SAndreas Gohr                if (!is_array($origValue) || isset($origValue['val'])) {
204655efc227SAndreas Gohr                    unset($origValue); // Break the reference
204755efc227SAndreas Gohr                    $origValue = array($this->_info['exif'][$name]);
204855efc227SAndreas Gohr                }
204955efc227SAndreas Gohr                $origCount = count($origValue);
205055efc227SAndreas Gohr
205155efc227SAndreas Gohr                if ($origCount == 0 ) {
205255efc227SAndreas Gohr                    $type = -1;  // To ignore this field
205355efc227SAndreas Gohr                }
205455efc227SAndreas Gohr
205555efc227SAndreas Gohr                $value = " ";
205655efc227SAndreas Gohr
205755efc227SAndreas Gohr                switch ($type) {
205855efc227SAndreas Gohr                case 1:    // UBYTE
205955efc227SAndreas Gohr                    if ($count == 0) {
206055efc227SAndreas Gohr                        $count = $origCount;
206155efc227SAndreas Gohr                    }
206255efc227SAndreas Gohr
206355efc227SAndreas Gohr                    $j = 0;
206455efc227SAndreas Gohr                    while (($j < $count) && ($j < $origCount)) {
206555efc227SAndreas Gohr
206655efc227SAndreas Gohr                        $this->_putByte($value, $j, $origValue[$j]);
206755efc227SAndreas Gohr                        $j++;
206855efc227SAndreas Gohr                    }
206955efc227SAndreas Gohr
207055efc227SAndreas Gohr                    while ($j < $count) {
207155efc227SAndreas Gohr                        $this->_putByte($value, $j, 0);
207255efc227SAndreas Gohr                        $j++;
207355efc227SAndreas Gohr                    }
207455efc227SAndreas Gohr                    break;
207555efc227SAndreas Gohr                case 2:    // ASCII
207655efc227SAndreas Gohr                    $v = strval($origValue[0]);
207755efc227SAndreas Gohr                    if (($count != 0) && (strlen($v) > $count)) {
207855efc227SAndreas Gohr                        $v = substr($v, 0, $count);
207955efc227SAndreas Gohr                    }
208055efc227SAndreas Gohr                    elseif (($count > 0) && (strlen($v) < $count)) {
208155efc227SAndreas Gohr                        $v = str_pad($v, $count, "\0");
208255efc227SAndreas Gohr                    }
208355efc227SAndreas Gohr
208455efc227SAndreas Gohr                    $count = strlen($v);
208555efc227SAndreas Gohr
208655efc227SAndreas Gohr                    $this->_putString($value, 0, $v);
208755efc227SAndreas Gohr                    break;
208855efc227SAndreas Gohr                case 3:    // USHORT
208955efc227SAndreas Gohr                    if ($count == 0) {
209055efc227SAndreas Gohr                        $count = $origCount;
209155efc227SAndreas Gohr                    }
209255efc227SAndreas Gohr
209355efc227SAndreas Gohr                    $j = 0;
209455efc227SAndreas Gohr                    while (($j < $count) && ($j < $origCount)) {
209555efc227SAndreas Gohr                        $this->_putShort($value, $j * 2, $origValue[$j], $isBigEndian);
209655efc227SAndreas Gohr                        $j++;
209755efc227SAndreas Gohr                    }
209855efc227SAndreas Gohr
209955efc227SAndreas Gohr                    while ($j < $count) {
210055efc227SAndreas Gohr                        $this->_putShort($value, $j * 2, 0, $isBigEndian);
210155efc227SAndreas Gohr                        $j++;
210255efc227SAndreas Gohr                    }
210355efc227SAndreas Gohr                    break;
210455efc227SAndreas Gohr                case 4:    // ULONG
210555efc227SAndreas Gohr                    if ($count == 0) {
210655efc227SAndreas Gohr                        $count = $origCount;
210755efc227SAndreas Gohr                    }
210855efc227SAndreas Gohr
210955efc227SAndreas Gohr                    $j = 0;
211055efc227SAndreas Gohr                    while (($j < $count) && ($j < $origCount)) {
211155efc227SAndreas Gohr                        $this->_putLong($value, $j * 4, $origValue[$j], $isBigEndian);
211255efc227SAndreas Gohr                        $j++;
211355efc227SAndreas Gohr                    }
211455efc227SAndreas Gohr
211555efc227SAndreas Gohr                    while ($j < $count) {
211655efc227SAndreas Gohr                        $this->_putLong($value, $j * 4, 0, $isBigEndian);
211755efc227SAndreas Gohr                        $j++;
211855efc227SAndreas Gohr                    }
211955efc227SAndreas Gohr                    break;
212055efc227SAndreas Gohr                case 5:    // URATIONAL
212155efc227SAndreas Gohr                    if ($count == 0) {
212255efc227SAndreas Gohr                        $count = $origCount;
212355efc227SAndreas Gohr                    }
212455efc227SAndreas Gohr
212555efc227SAndreas Gohr                    $j = 0;
212655efc227SAndreas Gohr                    while (($j < $count) && ($j < $origCount)) {
212755efc227SAndreas Gohr                        $v = $origValue[$j];
212855efc227SAndreas Gohr                        if (is_array($v)) {
212955efc227SAndreas Gohr                            $a = $v['num'];
213055efc227SAndreas Gohr                            $b = $v['den'];
213155efc227SAndreas Gohr                        }
213255efc227SAndreas Gohr                        else {
213355efc227SAndreas Gohr                            $a = 0;
213455efc227SAndreas Gohr                            $b = 0;
213555efc227SAndreas Gohr                            // TODO: Allow other types and convert them
213655efc227SAndreas Gohr                        }
213755efc227SAndreas Gohr                        $this->_putLong($value, $j * 8, $a, $isBigEndian);
213855efc227SAndreas Gohr                        $this->_putLong($value, ($j * 8) + 4, $b, $isBigEndian);
213955efc227SAndreas Gohr                        $j++;
214055efc227SAndreas Gohr                    }
214155efc227SAndreas Gohr
214255efc227SAndreas Gohr                    while ($j < $count) {
214355efc227SAndreas Gohr                        $this->_putLong($value, $j * 8, 0, $isBigEndian);
214455efc227SAndreas Gohr                        $this->_putLong($value, ($j * 8) + 4, 0, $isBigEndian);
214555efc227SAndreas Gohr                        $j++;
214655efc227SAndreas Gohr                    }
214755efc227SAndreas Gohr                    break;
214855efc227SAndreas Gohr                case 6:    // SBYTE
214955efc227SAndreas Gohr                    if ($count == 0) {
215055efc227SAndreas Gohr                        $count = $origCount;
215155efc227SAndreas Gohr                    }
215255efc227SAndreas Gohr
215355efc227SAndreas Gohr                    $j = 0;
215455efc227SAndreas Gohr                    while (($j < $count) && ($j < $origCount)) {
215555efc227SAndreas Gohr                        $this->_putByte($value, $j, $origValue[$j]);
215655efc227SAndreas Gohr                        $j++;
215755efc227SAndreas Gohr                    }
215855efc227SAndreas Gohr
215955efc227SAndreas Gohr                    while ($j < $count) {
216055efc227SAndreas Gohr                        $this->_putByte($value, $j, 0);
216155efc227SAndreas Gohr                        $j++;
216255efc227SAndreas Gohr                    }
216355efc227SAndreas Gohr                    break;
216455efc227SAndreas Gohr                case 7:    // UNDEFINED
216555efc227SAndreas Gohr                    $v = strval($origValue[0]);
216655efc227SAndreas Gohr                    if (($count != 0) && (strlen($v) > $count)) {
216755efc227SAndreas Gohr                        $v = substr($v, 0, $count);
216855efc227SAndreas Gohr                    }
216955efc227SAndreas Gohr                    elseif (($count > 0) && (strlen($v) < $count)) {
217055efc227SAndreas Gohr                        $v = str_pad($v, $count, "\0");
217155efc227SAndreas Gohr                    }
217255efc227SAndreas Gohr
217355efc227SAndreas Gohr                    $count = strlen($v);
217455efc227SAndreas Gohr
217555efc227SAndreas Gohr                    $this->_putString($value, 0, $v);
217655efc227SAndreas Gohr                    break;
217755efc227SAndreas Gohr                case 8:    // SSHORT
217855efc227SAndreas Gohr                    if ($count == 0) {
217955efc227SAndreas Gohr                        $count = $origCount;
218055efc227SAndreas Gohr                    }
218155efc227SAndreas Gohr
218255efc227SAndreas Gohr                    $j = 0;
218355efc227SAndreas Gohr                    while (($j < $count) && ($j < $origCount)) {
218455efc227SAndreas Gohr                        $this->_putShort($value, $j * 2, $origValue[$j], $isBigEndian);
218555efc227SAndreas Gohr                        $j++;
218655efc227SAndreas Gohr                    }
218755efc227SAndreas Gohr
218855efc227SAndreas Gohr                    while ($j < $count) {
218955efc227SAndreas Gohr                        $this->_putShort($value, $j * 2, 0, $isBigEndian);
219055efc227SAndreas Gohr                        $j++;
219155efc227SAndreas Gohr                    }
219255efc227SAndreas Gohr                    break;
219355efc227SAndreas Gohr                case 9:    // SLONG
219455efc227SAndreas Gohr                    if ($count == 0) {
219555efc227SAndreas Gohr                        $count = $origCount;
219655efc227SAndreas Gohr                    }
219755efc227SAndreas Gohr
219855efc227SAndreas Gohr                    $j = 0;
219955efc227SAndreas Gohr                    while (($j < $count) && ($j < $origCount)) {
220055efc227SAndreas Gohr                        $this->_putLong($value, $j * 4, $origValue[$j], $isBigEndian);
220155efc227SAndreas Gohr                        $j++;
220255efc227SAndreas Gohr                    }
220355efc227SAndreas Gohr
220455efc227SAndreas Gohr                    while ($j < $count) {
220555efc227SAndreas Gohr                        $this->_putLong($value, $j * 4, 0, $isBigEndian);
220655efc227SAndreas Gohr                        $j++;
220755efc227SAndreas Gohr                    }
220855efc227SAndreas Gohr                    break;
220955efc227SAndreas Gohr                case 10:   // SRATIONAL
221055efc227SAndreas Gohr                    if ($count == 0) {
221155efc227SAndreas Gohr                        $count = $origCount;
221255efc227SAndreas Gohr                    }
221355efc227SAndreas Gohr
221455efc227SAndreas Gohr                    $j = 0;
221555efc227SAndreas Gohr                    while (($j < $count) && ($j < $origCount)) {
221655efc227SAndreas Gohr                        $v = $origValue[$j];
221755efc227SAndreas Gohr                        if (is_array($v)) {
221855efc227SAndreas Gohr                            $a = $v['num'];
221955efc227SAndreas Gohr                            $b = $v['den'];
222055efc227SAndreas Gohr                        }
222155efc227SAndreas Gohr                        else {
222255efc227SAndreas Gohr                            $a = 0;
222355efc227SAndreas Gohr                            $b = 0;
222455efc227SAndreas Gohr                            // TODO: Allow other types and convert them
222555efc227SAndreas Gohr                        }
222655efc227SAndreas Gohr
222755efc227SAndreas Gohr                        $this->_putLong($value, $j * 8, $a, $isBigEndian);
222855efc227SAndreas Gohr                        $this->_putLong($value, ($j * 8) + 4, $b, $isBigEndian);
222955efc227SAndreas Gohr                        $j++;
223055efc227SAndreas Gohr                    }
223155efc227SAndreas Gohr
223255efc227SAndreas Gohr                    while ($j < $count) {
223355efc227SAndreas Gohr                        $this->_putLong($value, $j * 8, 0, $isBigEndian);
223455efc227SAndreas Gohr                        $this->_putLong($value, ($j * 8) + 4, 0, $isBigEndian);
223555efc227SAndreas Gohr                        $j++;
223655efc227SAndreas Gohr                    }
223755efc227SAndreas Gohr                    break;
223855efc227SAndreas Gohr                case 11:   // FLOAT
223955efc227SAndreas Gohr                    if ($count == 0) {
224055efc227SAndreas Gohr                        $count = $origCount;
224155efc227SAndreas Gohr                    }
224255efc227SAndreas Gohr
224355efc227SAndreas Gohr                    $j = 0;
224455efc227SAndreas Gohr                    while (($j < $count) && ($j < $origCount)) {
224555efc227SAndreas Gohr                        $v = strval($origValue[$j]);
224655efc227SAndreas Gohr                        if (strlen($v) > 4) {
224755efc227SAndreas Gohr                            $v = substr($v, 0, 4);
224855efc227SAndreas Gohr                        }
224955efc227SAndreas Gohr                        elseif (strlen($v) < 4) {
225055efc227SAndreas Gohr                            $v = str_pad($v, 4, "\0");
225155efc227SAndreas Gohr                        }
225255efc227SAndreas Gohr                        $this->_putString($value, $j * 4, $v);
225355efc227SAndreas Gohr                        $j++;
225455efc227SAndreas Gohr                    }
225555efc227SAndreas Gohr
225655efc227SAndreas Gohr                    while ($j < $count) {
225755efc227SAndreas Gohr                        $this->_putString($value, $j * 4, "\0\0\0\0");
225855efc227SAndreas Gohr                        $j++;
225955efc227SAndreas Gohr                    }
226055efc227SAndreas Gohr                    break;
226155efc227SAndreas Gohr                case 12:   // DFLOAT
226255efc227SAndreas Gohr                    if ($count == 0) {
226355efc227SAndreas Gohr                        $count = $origCount;
226455efc227SAndreas Gohr                    }
226555efc227SAndreas Gohr
226655efc227SAndreas Gohr                    $j = 0;
226755efc227SAndreas Gohr                    while (($j < $count) && ($j < $origCount)) {
226855efc227SAndreas Gohr                        $v = strval($origValue[$j]);
226955efc227SAndreas Gohr                        if (strlen($v) > 8) {
227055efc227SAndreas Gohr                            $v = substr($v, 0, 8);
227155efc227SAndreas Gohr                        }
227255efc227SAndreas Gohr                        elseif (strlen($v) < 8) {
227355efc227SAndreas Gohr                            $v = str_pad($v, 8, "\0");
227455efc227SAndreas Gohr                        }
227555efc227SAndreas Gohr                        $this->_putString($value, $j * 8, $v);
227655efc227SAndreas Gohr                        $j++;
227755efc227SAndreas Gohr                    }
227855efc227SAndreas Gohr
227955efc227SAndreas Gohr                    while ($j < $count) {
228055efc227SAndreas Gohr                        $this->_putString($value, $j * 8, "\0\0\0\0\0\0\0\0");
228155efc227SAndreas Gohr                        $j++;
228255efc227SAndreas Gohr                    }
228355efc227SAndreas Gohr                    break;
228455efc227SAndreas Gohr                default:
228555efc227SAndreas Gohr                    $value = null;
228655efc227SAndreas Gohr                    break;
228755efc227SAndreas Gohr                }
228855efc227SAndreas Gohr            }
228955efc227SAndreas Gohr
229055efc227SAndreas Gohr            if ($value != null) {
229155efc227SAndreas Gohr                $ifdEntries[$entryCount] = array();
229255efc227SAndreas Gohr                $ifdEntries[$entryCount]['tag'] = $tag;
229355efc227SAndreas Gohr                $ifdEntries[$entryCount]['type'] = $type;
229455efc227SAndreas Gohr                $ifdEntries[$entryCount]['count'] = $count;
229555efc227SAndreas Gohr                $ifdEntries[$entryCount]['value'] = $value;
229655efc227SAndreas Gohr
229755efc227SAndreas Gohr                $entryCount++;
229855efc227SAndreas Gohr            }
229955efc227SAndreas Gohr        }
230055efc227SAndreas Gohr
230155efc227SAndreas Gohr        return $ifdEntries;
230255efc227SAndreas Gohr    }
230355efc227SAndreas Gohr
230455efc227SAndreas Gohr    /*************************************************************/
230555efc227SAndreas Gohr    function _parseMarkerAdobe()
230655efc227SAndreas Gohr    {
230755efc227SAndreas Gohr        if (!isset($this->_markers)) {
230855efc227SAndreas Gohr            $this->_readJPEG();
230955efc227SAndreas Gohr        }
231055efc227SAndreas Gohr
231155efc227SAndreas Gohr        if ($this->_markers == null) {
231255efc227SAndreas Gohr            return false;
231355efc227SAndreas Gohr        }
231455efc227SAndreas Gohr
231555efc227SAndreas Gohr        $data = null;
231655efc227SAndreas Gohr        $count = count($this->_markers);
231755efc227SAndreas Gohr        for ($i = 0; $i < $count; $i++) {
231855efc227SAndreas Gohr            if ($this->_markers[$i]['marker'] == 0xED) {
231955efc227SAndreas Gohr                $signature = $this->_getFixedString($this->_markers[$i]['data'], 0, 14);
232055efc227SAndreas Gohr                if ($signature == "Photoshop 3.0\0") {
232155efc227SAndreas Gohr                    $data =& $this->_markers[$i]['data'];
232255efc227SAndreas Gohr                    break;
232355efc227SAndreas Gohr                }
232455efc227SAndreas Gohr            }
232555efc227SAndreas Gohr        }
232655efc227SAndreas Gohr
232755efc227SAndreas Gohr        if ($data == null) {
232855efc227SAndreas Gohr            $this->_info['adobe'] = false;
232955efc227SAndreas Gohr            $this->_info['iptc'] = false;
233055efc227SAndreas Gohr            return false;
233155efc227SAndreas Gohr        }
233255efc227SAndreas Gohr        $pos = 14;
233355efc227SAndreas Gohr        $this->_info['adobe'] = array();
233455efc227SAndreas Gohr        $this->_info['adobe']['raw'] = array();
233555efc227SAndreas Gohr        $this->_info['iptc'] = array();
233655efc227SAndreas Gohr
233755efc227SAndreas Gohr        $datasize = strlen($data);
233855efc227SAndreas Gohr
233955efc227SAndreas Gohr        while ($pos < $datasize) {
234055efc227SAndreas Gohr            $signature = $this->_getFixedString($data, $pos, 4);
234155efc227SAndreas Gohr            if ($signature != '8BIM')
234255efc227SAndreas Gohr                return false;
234355efc227SAndreas Gohr            $pos += 4;
234455efc227SAndreas Gohr
234555efc227SAndreas Gohr            $type = $this->_getShort($data, $pos);
234655efc227SAndreas Gohr            $pos += 2;
234755efc227SAndreas Gohr
234855efc227SAndreas Gohr            $strlen = $this->_getByte($data, $pos);
234955efc227SAndreas Gohr            $pos += 1;
235055efc227SAndreas Gohr            $header = '';
235155efc227SAndreas Gohr            for ($i = 0; $i < $strlen; $i++) {
235255efc227SAndreas Gohr                $header .= $data{$pos + $i};
235355efc227SAndreas Gohr            }
235455efc227SAndreas Gohr            $pos += $strlen + 1 - ($strlen % 2);  // The string is padded to even length, counting the length byte itself
235555efc227SAndreas Gohr
235655efc227SAndreas Gohr            $length = $this->_getLong($data, $pos);
235755efc227SAndreas Gohr            $pos += 4;
235855efc227SAndreas Gohr
235955efc227SAndreas Gohr            $basePos = $pos;
236055efc227SAndreas Gohr
236155efc227SAndreas Gohr            switch ($type) {
236255efc227SAndreas Gohr            case 0x0404: // Caption (IPTC Data)
236355efc227SAndreas Gohr                $pos = $this->_readIPTC($data, $pos);
236455efc227SAndreas Gohr                if ($pos == false)
236555efc227SAndreas Gohr                    return false;
236655efc227SAndreas Gohr                break;
236755efc227SAndreas Gohr            case 0x040A: // CopyrightFlag
236855efc227SAndreas Gohr                $this->_info['adobe']['CopyrightFlag'] = $this->_getByte($data, $pos);
236955efc227SAndreas Gohr                $pos += $length;
237055efc227SAndreas Gohr                break;
237155efc227SAndreas Gohr            case 0x040B: // ImageURL
237255efc227SAndreas Gohr                $this->_info['adobe']['ImageURL'] = $this->_getFixedString($data, $pos, $length);
237355efc227SAndreas Gohr                $pos += $length;
237455efc227SAndreas Gohr                break;
237555efc227SAndreas Gohr            case 0x040C: // Thumbnail
237655efc227SAndreas Gohr                $aux = $this->_getLong($data, $pos);
237755efc227SAndreas Gohr                $pos += 4;
237855efc227SAndreas Gohr                if ($aux == 1) {
237955efc227SAndreas Gohr                    $this->_info['adobe']['ThumbnailWidth'] = $this->_getLong($data, $pos);
238055efc227SAndreas Gohr                    $pos += 4;
238155efc227SAndreas Gohr                    $this->_info['adobe']['ThumbnailHeight'] = $this->_getLong($data, $pos);
238255efc227SAndreas Gohr                    $pos += 4;
238355efc227SAndreas Gohr
238455efc227SAndreas Gohr                    $pos += 16; // Skip some data
238555efc227SAndreas Gohr
238655efc227SAndreas Gohr                    $this->_info['adobe']['ThumbnailData'] = $this->_getFixedString($data, $pos, $length - 28);
238755efc227SAndreas Gohr                    $pos += $length - 28;
238855efc227SAndreas Gohr                }
238955efc227SAndreas Gohr                break;
239055efc227SAndreas Gohr            default:
239155efc227SAndreas Gohr                break;
239255efc227SAndreas Gohr            }
239355efc227SAndreas Gohr
239455efc227SAndreas Gohr            // We save all blocks, even those we recognized
239555efc227SAndreas Gohr            $label = sprintf('8BIM_0x%04x', $type);
239655efc227SAndreas Gohr            $this->_info['adobe']['raw'][$label] = array();
239755efc227SAndreas Gohr            $this->_info['adobe']['raw'][$label]['type'] = $type;
239855efc227SAndreas Gohr            $this->_info['adobe']['raw'][$label]['header'] = $header;
239955efc227SAndreas Gohr            $this->_info['adobe']['raw'][$label]['data'] =& $this->_getFixedString($data, $basePos, $length);
240055efc227SAndreas Gohr
240155efc227SAndreas Gohr            $pos = $basePos + $length + ($length % 2); // Even padding
240255efc227SAndreas Gohr        }
240355efc227SAndreas Gohr
240455efc227SAndreas Gohr    }
240555efc227SAndreas Gohr
240655efc227SAndreas Gohr    /*************************************************************/
240755efc227SAndreas Gohr    function _readIPTC(&$data, $pos = 0)
240855efc227SAndreas Gohr    {
240955efc227SAndreas Gohr        $totalLength = strlen($data);
241055efc227SAndreas Gohr
241155efc227SAndreas Gohr        $IPTCTags =& $this->_iptcTagNames();
241255efc227SAndreas Gohr
241355efc227SAndreas Gohr        while ($pos < ($totalLength - 5)) {
241455efc227SAndreas Gohr            $signature = $this->_getShort($data, $pos);
241555efc227SAndreas Gohr            if ($signature != 0x1C02)
241655efc227SAndreas Gohr                return $pos;
241755efc227SAndreas Gohr            $pos += 2;
241855efc227SAndreas Gohr
241955efc227SAndreas Gohr            $type = $this->_getByte($data, $pos);
242055efc227SAndreas Gohr            $pos += 1;
242155efc227SAndreas Gohr            $length = $this->_getShort($data, $pos);
242255efc227SAndreas Gohr            $pos += 2;
242355efc227SAndreas Gohr
242455efc227SAndreas Gohr            $basePos = $pos;
242555efc227SAndreas Gohr            $label = '';
242655efc227SAndreas Gohr
242755efc227SAndreas Gohr            if (isset($IPTCTags[$type])) {
242855efc227SAndreas Gohr                $label = $IPTCTags[$type];
242955efc227SAndreas Gohr            }
243055efc227SAndreas Gohr            else {
243155efc227SAndreas Gohr                $label = sprintf('IPTC_0x%02x', $type);
243255efc227SAndreas Gohr            }
243355efc227SAndreas Gohr
243455efc227SAndreas Gohr            if ($label != '') {
243555efc227SAndreas Gohr                if (isset($this->_info['iptc'][$label])) {
243655efc227SAndreas Gohr                    if (!is_array($this->_info['iptc'][$label])) {
243755efc227SAndreas Gohr                        $aux = array();
243855efc227SAndreas Gohr                        $aux[0] = $this->_info['iptc'][$label];
243955efc227SAndreas Gohr                        $this->_info['iptc'][$label] = $aux;
244055efc227SAndreas Gohr                    }
244155efc227SAndreas Gohr                    $this->_info['iptc'][$label][ count($this->_info['iptc'][$label]) ] = $this->_getFixedString($data, $pos, $length);
244255efc227SAndreas Gohr                }
244355efc227SAndreas Gohr                else {
244455efc227SAndreas Gohr                    $this->_info['iptc'][$label] = $this->_getFixedString($data, $pos, $length);
244555efc227SAndreas Gohr                }
244655efc227SAndreas Gohr            }
244755efc227SAndreas Gohr
244855efc227SAndreas Gohr            $pos = $basePos + $length; // No padding
244955efc227SAndreas Gohr        }
245055efc227SAndreas Gohr        return $pos;
245155efc227SAndreas Gohr    }
245255efc227SAndreas Gohr
245355efc227SAndreas Gohr    /*************************************************************/
245455efc227SAndreas Gohr    function & _createMarkerAdobe()
245555efc227SAndreas Gohr    {
245655efc227SAndreas Gohr        if (isset($this->_info['iptc'])) {
245755efc227SAndreas Gohr            if (!isset($this->_info['adobe'])) {
245855efc227SAndreas Gohr                $this->_info['adobe'] = array();
245955efc227SAndreas Gohr            }
246055efc227SAndreas Gohr            if (!isset($this->_info['adobe']['raw'])) {
246155efc227SAndreas Gohr                $this->_info['adobe']['raw'] = array();
246255efc227SAndreas Gohr            }
246355efc227SAndreas Gohr            if (!isset($this->_info['adobe']['raw']['8BIM_0x0404'])) {
246455efc227SAndreas Gohr                $this->_info['adobe']['raw']['8BIM_0x0404'] = array();
246555efc227SAndreas Gohr            }
246655efc227SAndreas Gohr            $this->_info['adobe']['raw']['8BIM_0x0404']['type'] = 0x0404;
246755efc227SAndreas Gohr            $this->_info['adobe']['raw']['8BIM_0x0404']['header'] = "Caption";
246855efc227SAndreas Gohr            $this->_info['adobe']['raw']['8BIM_0x0404']['data'] =& $this->_writeIPTC();
246955efc227SAndreas Gohr        }
247055efc227SAndreas Gohr
247155efc227SAndreas Gohr        if (isset($this->_info['adobe']['raw']) && (count($this->_info['adobe']['raw']) > 0)) {
247255efc227SAndreas Gohr            $data = "Photoshop 3.0\0";
247355efc227SAndreas Gohr            $pos = 14;
247455efc227SAndreas Gohr
247555efc227SAndreas Gohr            reset($this->_info['adobe']['raw']);
247655efc227SAndreas Gohr            while (list($key) = each($this->_info['adobe']['raw'])) {
247755efc227SAndreas Gohr                $pos = $this->_write8BIM(
247855efc227SAndreas Gohr                            $data,
247955efc227SAndreas Gohr                            $pos,
248055efc227SAndreas Gohr                            $this->_info['adobe']['raw'][$key]['type'],
248155efc227SAndreas Gohr                            $this->_info['adobe']['raw'][$key]['header'],
248255efc227SAndreas Gohr                            $this->_info['adobe']['raw'][$key]['data'] );
248355efc227SAndreas Gohr            }
248455efc227SAndreas Gohr        }
248555efc227SAndreas Gohr
248655efc227SAndreas Gohr        return $data;
248755efc227SAndreas Gohr    }
248855efc227SAndreas Gohr
248955efc227SAndreas Gohr    /*************************************************************/
249055efc227SAndreas Gohr    function _write8BIM(&$data, $pos, $type, $header, &$value)
249155efc227SAndreas Gohr    {
249255efc227SAndreas Gohr        $signature = "8BIM";
249355efc227SAndreas Gohr
249455efc227SAndreas Gohr        $pos = $this->_putString($data, $pos, $signature);
249555efc227SAndreas Gohr        $pos = $this->_putShort($data, $pos, $type);
249655efc227SAndreas Gohr
249755efc227SAndreas Gohr        $len = strlen($header);
249855efc227SAndreas Gohr
249955efc227SAndreas Gohr        $pos = $this->_putByte($data, $pos, $len);
250055efc227SAndreas Gohr        $pos = $this->_putString($data, $pos, $header);
250155efc227SAndreas Gohr        if (($len % 2) == 0) {  // Even padding, including the length byte
250255efc227SAndreas Gohr            $pos = $this->_putByte($data, $pos, 0);
250355efc227SAndreas Gohr        }
250455efc227SAndreas Gohr
250555efc227SAndreas Gohr        $len = strlen($value);
250655efc227SAndreas Gohr        $pos = $this->_putLong($data, $pos, $len);
250755efc227SAndreas Gohr        $pos = $this->_putString($data, $pos, $value);
250855efc227SAndreas Gohr        if (($len % 2) != 0) {  // Even padding
250955efc227SAndreas Gohr            $pos = $this->_putByte($data, $pos, 0);
251055efc227SAndreas Gohr        }
251155efc227SAndreas Gohr        return $pos;
251255efc227SAndreas Gohr    }
251355efc227SAndreas Gohr
251455efc227SAndreas Gohr    /*************************************************************/
251555efc227SAndreas Gohr    function & _writeIPTC()
251655efc227SAndreas Gohr    {
251755efc227SAndreas Gohr        $data = " ";
251855efc227SAndreas Gohr        $pos = 0;
251955efc227SAndreas Gohr
252055efc227SAndreas Gohr        $IPTCNames =& $this->_iptcNameTags();
252155efc227SAndreas Gohr
252255efc227SAndreas Gohr        reset($this->_info['iptc']);
252355efc227SAndreas Gohr
252455efc227SAndreas Gohr
252555efc227SAndreas Gohr        while (list($label) = each($this->_info['iptc'])) {
252655efc227SAndreas Gohr            $value =& $this->_info['iptc'][$label];
252755efc227SAndreas Gohr            $type = -1;
252855efc227SAndreas Gohr
252955efc227SAndreas Gohr            if (isset($IPTCNames[$label])) {
253055efc227SAndreas Gohr                $type = $IPTCNames[$label];
253155efc227SAndreas Gohr            }
253255efc227SAndreas Gohr            elseif (substr($label, 0, 7) == "IPTC_0x") {
253355efc227SAndreas Gohr                $type = hexdec(substr($label, 7, 2));
253455efc227SAndreas Gohr            }
253555efc227SAndreas Gohr
253655efc227SAndreas Gohr            if ($type != -1) {
253755efc227SAndreas Gohr                if (is_array($value)) {
253855efc227SAndreas Gohr                    for ($i = 0; $i < count($value); $i++) {
253955efc227SAndreas Gohr                        $pos = $this->_writeIPTCEntry($data, $pos, $type, $value[$i]);
254055efc227SAndreas Gohr                    }
254155efc227SAndreas Gohr                }
254255efc227SAndreas Gohr                else {
254355efc227SAndreas Gohr                    $pos = $this->_writeIPTCEntry($data, $pos, $type, $value);
254455efc227SAndreas Gohr                }
254555efc227SAndreas Gohr            }
254655efc227SAndreas Gohr        }
254755efc227SAndreas Gohr
254855efc227SAndreas Gohr        return $data;
254955efc227SAndreas Gohr    }
255055efc227SAndreas Gohr
255155efc227SAndreas Gohr    /*************************************************************/
255255efc227SAndreas Gohr    function _writeIPTCEntry(&$data, $pos, $type, &$value)
255355efc227SAndreas Gohr    {
255455efc227SAndreas Gohr        $pos = $this->_putShort($data, $pos, 0x1C02);
255555efc227SAndreas Gohr        $pos = $this->_putByte($data, $pos, $type);
255655efc227SAndreas Gohr        $pos = $this->_putShort($data, $pos, strlen($value));
255755efc227SAndreas Gohr        $pos = $this->_putString($data, $pos, $value);
255855efc227SAndreas Gohr
255955efc227SAndreas Gohr        return $pos;
256055efc227SAndreas Gohr    }
256155efc227SAndreas Gohr
256255efc227SAndreas Gohr    /*************************************************************/
256355efc227SAndreas Gohr    function _exifTagNames($mode)
256455efc227SAndreas Gohr    {
256555efc227SAndreas Gohr        $tags = array();
256655efc227SAndreas Gohr
256755efc227SAndreas Gohr        if ($mode == 'ifd0') {
256855efc227SAndreas Gohr            $tags[0x010E] = 'ImageDescription';
256955efc227SAndreas Gohr            $tags[0x010F] = 'Make';
257055efc227SAndreas Gohr            $tags[0x0110] = 'Model';
257155efc227SAndreas Gohr            $tags[0x0112] = 'Orientation';
257255efc227SAndreas Gohr            $tags[0x011A] = 'XResolution';
257355efc227SAndreas Gohr            $tags[0x011B] = 'YResolution';
257455efc227SAndreas Gohr            $tags[0x0128] = 'ResolutionUnit';
257555efc227SAndreas Gohr            $tags[0x0131] = 'Software';
257655efc227SAndreas Gohr            $tags[0x0132] = 'DateTime';
257755efc227SAndreas Gohr            $tags[0x013B] = 'Artist';
257855efc227SAndreas Gohr            $tags[0x013E] = 'WhitePoint';
257955efc227SAndreas Gohr            $tags[0x013F] = 'PrimaryChromaticities';
258055efc227SAndreas Gohr            $tags[0x0211] = 'YCbCrCoefficients';
258155efc227SAndreas Gohr            $tags[0x0212] = 'YCbCrSubSampling';
258255efc227SAndreas Gohr            $tags[0x0213] = 'YCbCrPositioning';
258355efc227SAndreas Gohr            $tags[0x0214] = 'ReferenceBlackWhite';
258455efc227SAndreas Gohr            $tags[0x8298] = 'Copyright';
258555efc227SAndreas Gohr            $tags[0x8769] = 'ExifIFDOffset';
258655efc227SAndreas Gohr            $tags[0x8825] = 'GPSIFDOffset';
258755efc227SAndreas Gohr        }
258855efc227SAndreas Gohr        if ($mode == 'ifd1') {
258955efc227SAndreas Gohr            $tags[0x00FE] = 'TIFFNewSubfileType';
259055efc227SAndreas Gohr            $tags[0x00FF] = 'TIFFSubfileType';
259155efc227SAndreas Gohr            $tags[0x0100] = 'TIFFImageWidth';
259255efc227SAndreas Gohr            $tags[0x0101] = 'TIFFImageHeight';
259355efc227SAndreas Gohr            $tags[0x0102] = 'TIFFBitsPerSample';
259455efc227SAndreas Gohr            $tags[0x0103] = 'TIFFCompression';
259555efc227SAndreas Gohr            $tags[0x0106] = 'TIFFPhotometricInterpretation';
259655efc227SAndreas Gohr            $tags[0x0107] = 'TIFFThreshholding';
259755efc227SAndreas Gohr            $tags[0x0108] = 'TIFFCellWidth';
259855efc227SAndreas Gohr            $tags[0x0109] = 'TIFFCellLength';
259955efc227SAndreas Gohr            $tags[0x010A] = 'TIFFFillOrder';
260055efc227SAndreas Gohr            $tags[0x010E] = 'TIFFImageDescription';
260155efc227SAndreas Gohr            $tags[0x010F] = 'TIFFMake';
260255efc227SAndreas Gohr            $tags[0x0110] = 'TIFFModel';
260355efc227SAndreas Gohr            $tags[0x0111] = 'TIFFStripOffsets';
260455efc227SAndreas Gohr            $tags[0x0112] = 'TIFFOrientation';
260555efc227SAndreas Gohr            $tags[0x0115] = 'TIFFSamplesPerPixel';
260655efc227SAndreas Gohr            $tags[0x0116] = 'TIFFRowsPerStrip';
260755efc227SAndreas Gohr            $tags[0x0117] = 'TIFFStripByteCounts';
260855efc227SAndreas Gohr            $tags[0x0118] = 'TIFFMinSampleValue';
260955efc227SAndreas Gohr            $tags[0x0119] = 'TIFFMaxSampleValue';
261055efc227SAndreas Gohr            $tags[0x011A] = 'TIFFXResolution';
261155efc227SAndreas Gohr            $tags[0x011B] = 'TIFFYResolution';
261255efc227SAndreas Gohr            $tags[0x011C] = 'TIFFPlanarConfiguration';
261355efc227SAndreas Gohr            $tags[0x0122] = 'TIFFGrayResponseUnit';
261455efc227SAndreas Gohr            $tags[0x0123] = 'TIFFGrayResponseCurve';
261555efc227SAndreas Gohr            $tags[0x0128] = 'TIFFResolutionUnit';
261655efc227SAndreas Gohr            $tags[0x0131] = 'TIFFSoftware';
261755efc227SAndreas Gohr            $tags[0x0132] = 'TIFFDateTime';
261855efc227SAndreas Gohr            $tags[0x013B] = 'TIFFArtist';
261955efc227SAndreas Gohr            $tags[0x013C] = 'TIFFHostComputer';
262055efc227SAndreas Gohr            $tags[0x0140] = 'TIFFColorMap';
262155efc227SAndreas Gohr            $tags[0x0152] = 'TIFFExtraSamples';
262255efc227SAndreas Gohr            $tags[0x0201] = 'TIFFJFIFOffset';
262355efc227SAndreas Gohr            $tags[0x0202] = 'TIFFJFIFLength';
262455efc227SAndreas Gohr            $tags[0x0211] = 'TIFFYCbCrCoefficients';
262555efc227SAndreas Gohr            $tags[0x0212] = 'TIFFYCbCrSubSampling';
262655efc227SAndreas Gohr            $tags[0x0213] = 'TIFFYCbCrPositioning';
262755efc227SAndreas Gohr            $tags[0x0214] = 'TIFFReferenceBlackWhite';
262855efc227SAndreas Gohr            $tags[0x8298] = 'TIFFCopyright';
262955efc227SAndreas Gohr            $tags[0x9286] = 'TIFFUserComment';
263055efc227SAndreas Gohr        }
263155efc227SAndreas Gohr        elseif ($mode == 'exif') {
263255efc227SAndreas Gohr            $tags[0x829A] = 'ExposureTime';
263355efc227SAndreas Gohr            $tags[0x829D] = 'FNumber';
263455efc227SAndreas Gohr            $tags[0x8822] = 'ExposureProgram';
263555efc227SAndreas Gohr            $tags[0x8824] = 'SpectralSensitivity';
263655efc227SAndreas Gohr            $tags[0x8827] = 'ISOSpeedRatings';
263755efc227SAndreas Gohr            $tags[0x8828] = 'OECF';
263855efc227SAndreas Gohr            $tags[0x9000] = 'EXIFVersion';
263955efc227SAndreas Gohr            $tags[0x9003] = 'DatetimeOriginal';
264055efc227SAndreas Gohr            $tags[0x9004] = 'DatetimeDigitized';
264155efc227SAndreas Gohr            $tags[0x9101] = 'ComponentsConfiguration';
264255efc227SAndreas Gohr            $tags[0x9102] = 'CompressedBitsPerPixel';
264355efc227SAndreas Gohr            $tags[0x9201] = 'ShutterSpeedValue';
264455efc227SAndreas Gohr            $tags[0x9202] = 'ApertureValue';
264555efc227SAndreas Gohr            $tags[0x9203] = 'BrightnessValue';
264655efc227SAndreas Gohr            $tags[0x9204] = 'ExposureBiasValue';
264755efc227SAndreas Gohr            $tags[0x9205] = 'MaxApertureValue';
264855efc227SAndreas Gohr            $tags[0x9206] = 'SubjectDistance';
264955efc227SAndreas Gohr            $tags[0x9207] = 'MeteringMode';
265055efc227SAndreas Gohr            $tags[0x9208] = 'LightSource';
265155efc227SAndreas Gohr            $tags[0x9209] = 'Flash';
265255efc227SAndreas Gohr            $tags[0x920A] = 'FocalLength';
265355efc227SAndreas Gohr            $tags[0x927C] = 'MakerNote';
265455efc227SAndreas Gohr            $tags[0x9286] = 'UserComment';
265555efc227SAndreas Gohr            $tags[0x9290] = 'SubSecTime';
265655efc227SAndreas Gohr            $tags[0x9291] = 'SubSecTimeOriginal';
265755efc227SAndreas Gohr            $tags[0x9292] = 'SubSecTimeDigitized';
265855efc227SAndreas Gohr            $tags[0xA000] = 'FlashPixVersion';
265955efc227SAndreas Gohr            $tags[0xA001] = 'ColorSpace';
266055efc227SAndreas Gohr            $tags[0xA002] = 'PixelXDimension';
266155efc227SAndreas Gohr            $tags[0xA003] = 'PixelYDimension';
266255efc227SAndreas Gohr            $tags[0xA004] = 'RelatedSoundFile';
266355efc227SAndreas Gohr            $tags[0xA005] = 'InteropIFDOffset';
266455efc227SAndreas Gohr            $tags[0xA20B] = 'FlashEnergy';
266555efc227SAndreas Gohr            $tags[0xA20C] = 'SpatialFrequencyResponse';
266655efc227SAndreas Gohr            $tags[0xA20E] = 'FocalPlaneXResolution';
266755efc227SAndreas Gohr            $tags[0xA20F] = 'FocalPlaneYResolution';
266855efc227SAndreas Gohr            $tags[0xA210] = 'FocalPlaneResolutionUnit';
266955efc227SAndreas Gohr            $tags[0xA214] = 'SubjectLocation';
267055efc227SAndreas Gohr            $tags[0xA215] = 'ExposureIndex';
267155efc227SAndreas Gohr            $tags[0xA217] = 'SensingMethod';
267255efc227SAndreas Gohr            $tags[0xA300] = 'FileSource';
267355efc227SAndreas Gohr            $tags[0xA301] = 'SceneType';
267455efc227SAndreas Gohr            $tags[0xA302] = 'CFAPattern';
267555efc227SAndreas Gohr        }
267655efc227SAndreas Gohr        elseif ($mode == 'interop') {
267755efc227SAndreas Gohr            $tags[0x0001] = 'InteroperabilityIndex';
267855efc227SAndreas Gohr            $tags[0x0002] = 'InteroperabilityVersion';
267955efc227SAndreas Gohr            $tags[0x1000] = 'RelatedImageFileFormat';
268055efc227SAndreas Gohr            $tags[0x1001] = 'RelatedImageWidth';
268155efc227SAndreas Gohr            $tags[0x1002] = 'RelatedImageLength';
268255efc227SAndreas Gohr        }
268355efc227SAndreas Gohr        elseif ($mode == 'gps') {
268455efc227SAndreas Gohr            $tags[0x0000] = 'GPSVersionID';
268555efc227SAndreas Gohr            $tags[0x0001] = 'GPSLatitudeRef';
268655efc227SAndreas Gohr            $tags[0x0002] = 'GPSLatitude';
268755efc227SAndreas Gohr            $tags[0x0003] = 'GPSLongitudeRef';
268855efc227SAndreas Gohr            $tags[0x0004] = 'GPSLongitude';
268955efc227SAndreas Gohr            $tags[0x0005] = 'GPSAltitudeRef';
269055efc227SAndreas Gohr            $tags[0x0006] = 'GPSAltitude';
269155efc227SAndreas Gohr            $tags[0x0007] = 'GPSTimeStamp';
269255efc227SAndreas Gohr            $tags[0x0008] = 'GPSSatellites';
269355efc227SAndreas Gohr            $tags[0x0009] = 'GPSStatus';
269455efc227SAndreas Gohr            $tags[0x000A] = 'GPSMeasureMode';
269555efc227SAndreas Gohr            $tags[0x000B] = 'GPSDOP';
269655efc227SAndreas Gohr            $tags[0x000C] = 'GPSSpeedRef';
269755efc227SAndreas Gohr            $tags[0x000D] = 'GPSSpeed';
269855efc227SAndreas Gohr            $tags[0x000E] = 'GPSTrackRef';
269955efc227SAndreas Gohr            $tags[0x000F] = 'GPSTrack';
270055efc227SAndreas Gohr            $tags[0x0010] = 'GPSImgDirectionRef';
270155efc227SAndreas Gohr            $tags[0x0011] = 'GPSImgDirection';
270255efc227SAndreas Gohr            $tags[0x0012] = 'GPSMapDatum';
270355efc227SAndreas Gohr            $tags[0x0013] = 'GPSDestLatitudeRef';
270455efc227SAndreas Gohr            $tags[0x0014] = 'GPSDestLatitude';
270555efc227SAndreas Gohr            $tags[0x0015] = 'GPSDestLongitudeRef';
270655efc227SAndreas Gohr            $tags[0x0016] = 'GPSDestLongitude';
270755efc227SAndreas Gohr            $tags[0x0017] = 'GPSDestBearingRef';
270855efc227SAndreas Gohr            $tags[0x0018] = 'GPSDestBearing';
270955efc227SAndreas Gohr            $tags[0x0019] = 'GPSDestDistanceRef';
271055efc227SAndreas Gohr            $tags[0x001A] = 'GPSDestDistance';
271155efc227SAndreas Gohr        }
271255efc227SAndreas Gohr
271355efc227SAndreas Gohr        return $tags;
271455efc227SAndreas Gohr    }
271555efc227SAndreas Gohr
271655efc227SAndreas Gohr    /*************************************************************/
271755efc227SAndreas Gohr    function _exifTagTypes($mode)
271855efc227SAndreas Gohr    {
271955efc227SAndreas Gohr        $tags = array();
272055efc227SAndreas Gohr
272155efc227SAndreas Gohr        if ($mode == 'ifd0') {
272255efc227SAndreas Gohr            $tags[0x010E] = array(2, 0); // ImageDescription -> ASCII, Any
272355efc227SAndreas Gohr            $tags[0x010F] = array(2, 0); // Make -> ASCII, Any
272455efc227SAndreas Gohr            $tags[0x0110] = array(2, 0); // Model -> ASCII, Any
272555efc227SAndreas Gohr            $tags[0x0112] = array(3, 1); // Orientation -> SHORT, 1
272655efc227SAndreas Gohr            $tags[0x011A] = array(5, 1); // XResolution -> RATIONAL, 1
272755efc227SAndreas Gohr            $tags[0x011B] = array(5, 1); // YResolution -> RATIONAL, 1
272855efc227SAndreas Gohr            $tags[0x0128] = array(3, 1); // ResolutionUnit -> SHORT
272955efc227SAndreas Gohr            $tags[0x0131] = array(2, 0); // Software -> ASCII, Any
273055efc227SAndreas Gohr            $tags[0x0132] = array(2, 20); // DateTime -> ASCII, 20
273155efc227SAndreas Gohr            $tags[0x013B] = array(2, 0); // Artist -> ASCII, Any
273255efc227SAndreas Gohr            $tags[0x013E] = array(5, 2); // WhitePoint -> RATIONAL, 2
273355efc227SAndreas Gohr            $tags[0x013F] = array(5, 6); // PrimaryChromaticities -> RATIONAL, 6
273455efc227SAndreas Gohr            $tags[0x0211] = array(5, 3); // YCbCrCoefficients -> RATIONAL, 3
273555efc227SAndreas Gohr            $tags[0x0212] = array(3, 2); // YCbCrSubSampling -> SHORT, 2
273655efc227SAndreas Gohr            $tags[0x0213] = array(3, 1); // YCbCrPositioning -> SHORT, 1
273755efc227SAndreas Gohr            $tags[0x0214] = array(5, 6); // ReferenceBlackWhite -> RATIONAL, 6
273855efc227SAndreas Gohr            $tags[0x8298] = array(2, 0); // Copyright -> ASCII, Any
273955efc227SAndreas Gohr            $tags[0x8769] = array(4, 1); // ExifIFDOffset -> LONG, 1
274055efc227SAndreas Gohr            $tags[0x8825] = array(4, 1); // GPSIFDOffset -> LONG, 1
274155efc227SAndreas Gohr        }
274255efc227SAndreas Gohr        if ($mode == 'ifd1') {
274355efc227SAndreas Gohr            $tags[0x00FE] = array(4, 1); // TIFFNewSubfileType -> LONG, 1
274455efc227SAndreas Gohr            $tags[0x00FF] = array(3, 1); // TIFFSubfileType -> SHORT, 1
274555efc227SAndreas Gohr            $tags[0x0100] = array(4, 1); // TIFFImageWidth -> LONG (or SHORT), 1
274655efc227SAndreas Gohr            $tags[0x0101] = array(4, 1); // TIFFImageHeight -> LONG (or SHORT), 1
274755efc227SAndreas Gohr            $tags[0x0102] = array(3, 3); // TIFFBitsPerSample -> SHORT, 3
274855efc227SAndreas Gohr            $tags[0x0103] = array(3, 1); // TIFFCompression -> SHORT, 1
274955efc227SAndreas Gohr            $tags[0x0106] = array(3, 1); // TIFFPhotometricInterpretation -> SHORT, 1
275055efc227SAndreas Gohr            $tags[0x0107] = array(3, 1); // TIFFThreshholding -> SHORT, 1
275155efc227SAndreas Gohr            $tags[0x0108] = array(3, 1); // TIFFCellWidth -> SHORT, 1
275255efc227SAndreas Gohr            $tags[0x0109] = array(3, 1); // TIFFCellLength -> SHORT, 1
275355efc227SAndreas Gohr            $tags[0x010A] = array(3, 1); // TIFFFillOrder -> SHORT, 1
275455efc227SAndreas Gohr            $tags[0x010E] = array(2, 0); // TIFFImageDescription -> ASCII, Any
275555efc227SAndreas Gohr            $tags[0x010F] = array(2, 0); // TIFFMake -> ASCII, Any
275655efc227SAndreas Gohr            $tags[0x0110] = array(2, 0); // TIFFModel -> ASCII, Any
275755efc227SAndreas Gohr            $tags[0x0111] = array(4, 0); // TIFFStripOffsets -> LONG (or SHORT), Any (one per strip)
275855efc227SAndreas Gohr            $tags[0x0112] = array(3, 1); // TIFFOrientation -> SHORT, 1
275955efc227SAndreas Gohr            $tags[0x0115] = array(3, 1); // TIFFSamplesPerPixel -> SHORT, 1
276055efc227SAndreas Gohr            $tags[0x0116] = array(4, 1); // TIFFRowsPerStrip -> LONG (or SHORT), 1
276155efc227SAndreas Gohr            $tags[0x0117] = array(4, 0); // TIFFStripByteCounts -> LONG (or SHORT), Any (one per strip)
276255efc227SAndreas Gohr            $tags[0x0118] = array(3, 0); // TIFFMinSampleValue -> SHORT, Any (SamplesPerPixel)
276355efc227SAndreas Gohr            $tags[0x0119] = array(3, 0); // TIFFMaxSampleValue -> SHORT, Any (SamplesPerPixel)
276455efc227SAndreas Gohr            $tags[0x011A] = array(5, 1); // TIFFXResolution -> RATIONAL, 1
276555efc227SAndreas Gohr            $tags[0x011B] = array(5, 1); // TIFFYResolution -> RATIONAL, 1
276655efc227SAndreas Gohr            $tags[0x011C] = array(3, 1); // TIFFPlanarConfiguration -> SHORT, 1
276755efc227SAndreas Gohr            $tags[0x0122] = array(3, 1); // TIFFGrayResponseUnit -> SHORT, 1
276855efc227SAndreas Gohr            $tags[0x0123] = array(3, 0); // TIFFGrayResponseCurve -> SHORT, Any (2^BitsPerSample)
276955efc227SAndreas Gohr            $tags[0x0128] = array(3, 1); // TIFFResolutionUnit -> SHORT, 1
277055efc227SAndreas Gohr            $tags[0x0131] = array(2, 0); // TIFFSoftware -> ASCII, Any
277155efc227SAndreas Gohr            $tags[0x0132] = array(2, 20); // TIFFDateTime -> ASCII, 20
277255efc227SAndreas Gohr            $tags[0x013B] = array(2, 0); // TIFFArtist -> ASCII, Any
277355efc227SAndreas Gohr            $tags[0x013C] = array(2, 0); // TIFFHostComputer -> ASCII, Any
277455efc227SAndreas Gohr            $tags[0x0140] = array(3, 0); // TIFFColorMap -> SHORT, Any (3 * 2^BitsPerSample)
277555efc227SAndreas Gohr            $tags[0x0152] = array(3, 0); // TIFFExtraSamples -> SHORT, Any (SamplesPerPixel - 3)
277655efc227SAndreas Gohr            $tags[0x0201] = array(4, 1); // TIFFJFIFOffset -> LONG, 1
277755efc227SAndreas Gohr            $tags[0x0202] = array(4, 1); // TIFFJFIFLength -> LONG, 1
277855efc227SAndreas Gohr            $tags[0x0211] = array(5, 3); // TIFFYCbCrCoefficients -> RATIONAL, 3
277955efc227SAndreas Gohr            $tags[0x0212] = array(3, 2); // TIFFYCbCrSubSampling -> SHORT, 2
278055efc227SAndreas Gohr            $tags[0x0213] = array(3, 1); // TIFFYCbCrPositioning -> SHORT, 1
278155efc227SAndreas Gohr            $tags[0x0214] = array(5, 6); // TIFFReferenceBlackWhite -> RATIONAL, 6
278255efc227SAndreas Gohr            $tags[0x8298] = array(2, 0); // TIFFCopyright -> ASCII, Any
278355efc227SAndreas Gohr            $tags[0x9286] = array(2, 0); // TIFFUserComment -> ASCII, Any
278455efc227SAndreas Gohr        }
278555efc227SAndreas Gohr        elseif ($mode == 'exif') {
278655efc227SAndreas Gohr            $tags[0x829A] = array(5, 1); // ExposureTime -> RATIONAL, 1
278755efc227SAndreas Gohr            $tags[0x829D] = array(5, 1); // FNumber -> RATIONAL, 1
278855efc227SAndreas Gohr            $tags[0x8822] = array(3, 1); // ExposureProgram -> SHORT, 1
278955efc227SAndreas Gohr            $tags[0x8824] = array(2, 0); // SpectralSensitivity -> ASCII, Any
279055efc227SAndreas Gohr            $tags[0x8827] = array(3, 0); // ISOSpeedRatings -> SHORT, Any
279155efc227SAndreas Gohr            $tags[0x8828] = array(7, 0); // OECF -> UNDEFINED, Any
279255efc227SAndreas Gohr            $tags[0x9000] = array(7, 4); // EXIFVersion -> UNDEFINED, 4
279355efc227SAndreas Gohr            $tags[0x9003] = array(2, 20); // DatetimeOriginal -> ASCII, 20
279455efc227SAndreas Gohr            $tags[0x9004] = array(2, 20); // DatetimeDigitized -> ASCII, 20
279555efc227SAndreas Gohr            $tags[0x9101] = array(7, 4); // ComponentsConfiguration -> UNDEFINED, 4
279655efc227SAndreas Gohr            $tags[0x9102] = array(5, 1); // CompressedBitsPerPixel -> RATIONAL, 1
279755efc227SAndreas Gohr            $tags[0x9201] = array(10, 1); // ShutterSpeedValue -> SRATIONAL, 1
279855efc227SAndreas Gohr            $tags[0x9202] = array(5, 1); // ApertureValue -> RATIONAL, 1
279955efc227SAndreas Gohr            $tags[0x9203] = array(10, 1); // BrightnessValue -> SRATIONAL, 1
280055efc227SAndreas Gohr            $tags[0x9204] = array(10, 1); // ExposureBiasValue -> SRATIONAL, 1
280155efc227SAndreas Gohr            $tags[0x9205] = array(5, 1); // MaxApertureValue -> RATIONAL, 1
280255efc227SAndreas Gohr            $tags[0x9206] = array(5, 1); // SubjectDistance -> RATIONAL, 1
280355efc227SAndreas Gohr            $tags[0x9207] = array(3, 1); // MeteringMode -> SHORT, 1
280455efc227SAndreas Gohr            $tags[0x9208] = array(3, 1); // LightSource -> SHORT, 1
280555efc227SAndreas Gohr            $tags[0x9209] = array(3, 1); // Flash -> SHORT, 1
280655efc227SAndreas Gohr            $tags[0x920A] = array(5, 1); // FocalLength -> RATIONAL, 1
280755efc227SAndreas Gohr            $tags[0x927C] = array(7, 0); // MakerNote -> UNDEFINED, Any
280855efc227SAndreas Gohr            $tags[0x9286] = array(7, 0); // UserComment -> UNDEFINED, Any
280955efc227SAndreas Gohr            $tags[0x9290] = array(2, 0); // SubSecTime -> ASCII, Any
281055efc227SAndreas Gohr            $tags[0x9291] = array(2, 0); // SubSecTimeOriginal -> ASCII, Any
281155efc227SAndreas Gohr            $tags[0x9292] = array(2, 0); // SubSecTimeDigitized -> ASCII, Any
281255efc227SAndreas Gohr            $tags[0xA000] = array(7, 4); // FlashPixVersion -> UNDEFINED, 4
281355efc227SAndreas Gohr            $tags[0xA001] = array(3, 1); // ColorSpace -> SHORT, 1
281455efc227SAndreas Gohr            $tags[0xA002] = array(4, 1); // PixelXDimension -> LONG (or SHORT), 1
281555efc227SAndreas Gohr            $tags[0xA003] = array(4, 1); // PixelYDimension -> LONG (or SHORT), 1
281655efc227SAndreas Gohr            $tags[0xA004] = array(2, 13); // RelatedSoundFile -> ASCII, 13
281755efc227SAndreas Gohr            $tags[0xA005] = array(4, 1); // InteropIFDOffset -> LONG, 1
281855efc227SAndreas Gohr            $tags[0xA20B] = array(5, 1); // FlashEnergy -> RATIONAL, 1
281955efc227SAndreas Gohr            $tags[0xA20C] = array(7, 0); // SpatialFrequencyResponse -> UNDEFINED, Any
282055efc227SAndreas Gohr            $tags[0xA20E] = array(5, 1); // FocalPlaneXResolution -> RATIONAL, 1
282155efc227SAndreas Gohr            $tags[0xA20F] = array(5, 1); // FocalPlaneYResolution -> RATIONAL, 1
282255efc227SAndreas Gohr            $tags[0xA210] = array(3, 1); // FocalPlaneResolutionUnit -> SHORT, 1
282355efc227SAndreas Gohr            $tags[0xA214] = array(3, 2); // SubjectLocation -> SHORT, 2
282455efc227SAndreas Gohr            $tags[0xA215] = array(5, 1); // ExposureIndex -> RATIONAL, 1
282555efc227SAndreas Gohr            $tags[0xA217] = array(3, 1); // SensingMethod -> SHORT, 1
282655efc227SAndreas Gohr            $tags[0xA300] = array(7, 1); // FileSource -> UNDEFINED, 1
282755efc227SAndreas Gohr            $tags[0xA301] = array(7, 1); // SceneType -> UNDEFINED, 1
282855efc227SAndreas Gohr            $tags[0xA302] = array(7, 0); // CFAPattern -> UNDEFINED, Any
282955efc227SAndreas Gohr        }
283055efc227SAndreas Gohr        elseif ($mode == 'interop') {
283155efc227SAndreas Gohr            $tags[0x0001] = array(2, 0); // InteroperabilityIndex -> ASCII, Any
283255efc227SAndreas Gohr            $tags[0x0002] = array(7, 4); // InteroperabilityVersion -> UNKNOWN, 4
283355efc227SAndreas Gohr            $tags[0x1000] = array(2, 0); // RelatedImageFileFormat -> ASCII, Any
283455efc227SAndreas Gohr            $tags[0x1001] = array(4, 1); // RelatedImageWidth -> LONG (or SHORT), 1
283555efc227SAndreas Gohr            $tags[0x1002] = array(4, 1); // RelatedImageLength -> LONG (or SHORT), 1
283655efc227SAndreas Gohr        }
283755efc227SAndreas Gohr        elseif ($mode == 'gps') {
283855efc227SAndreas Gohr            $tags[0x0000] = array(1, 4); // GPSVersionID -> BYTE, 4
283955efc227SAndreas Gohr            $tags[0x0001] = array(2, 2); // GPSLatitudeRef -> ASCII, 2
284055efc227SAndreas Gohr            $tags[0x0002] = array(5, 3); // GPSLatitude -> RATIONAL, 3
284155efc227SAndreas Gohr            $tags[0x0003] = array(2, 2); // GPSLongitudeRef -> ASCII, 2
284255efc227SAndreas Gohr            $tags[0x0004] = array(5, 3); // GPSLongitude -> RATIONAL, 3
284355efc227SAndreas Gohr            $tags[0x0005] = array(2, 2); // GPSAltitudeRef -> ASCII, 2
284455efc227SAndreas Gohr            $tags[0x0006] = array(5, 1); // GPSAltitude -> RATIONAL, 1
284555efc227SAndreas Gohr            $tags[0x0007] = array(5, 3); // GPSTimeStamp -> RATIONAL, 3
284655efc227SAndreas Gohr            $tags[0x0008] = array(2, 0); // GPSSatellites -> ASCII, Any
284755efc227SAndreas Gohr            $tags[0x0009] = array(2, 2); // GPSStatus -> ASCII, 2
284855efc227SAndreas Gohr            $tags[0x000A] = array(2, 2); // GPSMeasureMode -> ASCII, 2
284955efc227SAndreas Gohr            $tags[0x000B] = array(5, 1); // GPSDOP -> RATIONAL, 1
285055efc227SAndreas Gohr            $tags[0x000C] = array(2, 2); // GPSSpeedRef -> ASCII, 2
285155efc227SAndreas Gohr            $tags[0x000D] = array(5, 1); // GPSSpeed -> RATIONAL, 1
285255efc227SAndreas Gohr            $tags[0x000E] = array(2, 2); // GPSTrackRef -> ASCII, 2
285355efc227SAndreas Gohr            $tags[0x000F] = array(5, 1); // GPSTrack -> RATIONAL, 1
285455efc227SAndreas Gohr            $tags[0x0010] = array(2, 2); // GPSImgDirectionRef -> ASCII, 2
285555efc227SAndreas Gohr            $tags[0x0011] = array(5, 1); // GPSImgDirection -> RATIONAL, 1
285655efc227SAndreas Gohr            $tags[0x0012] = array(2, 0); // GPSMapDatum -> ASCII, Any
285755efc227SAndreas Gohr            $tags[0x0013] = array(2, 2); // GPSDestLatitudeRef -> ASCII, 2
285855efc227SAndreas Gohr            $tags[0x0014] = array(5, 3); // GPSDestLatitude -> RATIONAL, 3
285955efc227SAndreas Gohr            $tags[0x0015] = array(2, 2); // GPSDestLongitudeRef -> ASCII, 2
286055efc227SAndreas Gohr            $tags[0x0016] = array(5, 3); // GPSDestLongitude -> RATIONAL, 3
286155efc227SAndreas Gohr            $tags[0x0017] = array(2, 2); // GPSDestBearingRef -> ASCII, 2
286255efc227SAndreas Gohr            $tags[0x0018] = array(5, 1); // GPSDestBearing -> RATIONAL, 1
286355efc227SAndreas Gohr            $tags[0x0019] = array(2, 2); // GPSDestDistanceRef -> ASCII, 2
286455efc227SAndreas Gohr            $tags[0x001A] = array(5, 1); // GPSDestDistance -> RATIONAL, 1
286555efc227SAndreas Gohr        }
286655efc227SAndreas Gohr
286755efc227SAndreas Gohr        return $tags;
286855efc227SAndreas Gohr    }
286955efc227SAndreas Gohr
287055efc227SAndreas Gohr    /*************************************************************/
287155efc227SAndreas Gohr    function _exifNameTags($mode)
287255efc227SAndreas Gohr    {
287355efc227SAndreas Gohr        $tags = $this->_exifTagNames($mode);
287455efc227SAndreas Gohr        return $this->_names2Tags($tags);
287555efc227SAndreas Gohr    }
287655efc227SAndreas Gohr
287755efc227SAndreas Gohr    /*************************************************************/
287855efc227SAndreas Gohr    function _iptcTagNames()
287955efc227SAndreas Gohr    {
288055efc227SAndreas Gohr        $tags = array();
288155efc227SAndreas Gohr        $tags[0x14] = 'SuplementalCategories';
288255efc227SAndreas Gohr        $tags[0x19] = 'Keywords';
288355efc227SAndreas Gohr        $tags[0x78] = 'Caption';
288455efc227SAndreas Gohr        $tags[0x7A] = 'CaptionWriter';
288555efc227SAndreas Gohr        $tags[0x69] = 'Headline';
288655efc227SAndreas Gohr        $tags[0x28] = 'SpecialInstructions';
288755efc227SAndreas Gohr        $tags[0x0F] = 'Category';
288855efc227SAndreas Gohr        $tags[0x50] = 'Byline';
288955efc227SAndreas Gohr        $tags[0x55] = 'BylineTitle';
289055efc227SAndreas Gohr        $tags[0x6E] = 'Credit';
289155efc227SAndreas Gohr        $tags[0x73] = 'Source';
289255efc227SAndreas Gohr        $tags[0x74] = 'CopyrightNotice';
289355efc227SAndreas Gohr        $tags[0x05] = 'ObjectName';
289455efc227SAndreas Gohr        $tags[0x5A] = 'City';
2895493531a8SJoe Lapp        $tags[0x5C] = 'Sublocation';
289655efc227SAndreas Gohr        $tags[0x5F] = 'ProvinceState';
289755efc227SAndreas Gohr        $tags[0x65] = 'CountryName';
289855efc227SAndreas Gohr        $tags[0x67] = 'OriginalTransmissionReference';
289955efc227SAndreas Gohr        $tags[0x37] = 'DateCreated';
290055efc227SAndreas Gohr        $tags[0x0A] = 'CopyrightFlag';
290155efc227SAndreas Gohr
290255efc227SAndreas Gohr        return $tags;
290355efc227SAndreas Gohr    }
290455efc227SAndreas Gohr
290555efc227SAndreas Gohr    /*************************************************************/
290655efc227SAndreas Gohr    function & _iptcNameTags()
290755efc227SAndreas Gohr    {
290855efc227SAndreas Gohr        $tags = $this->_iptcTagNames();
290955efc227SAndreas Gohr        return $this->_names2Tags($tags);
291055efc227SAndreas Gohr    }
291155efc227SAndreas Gohr
291255efc227SAndreas Gohr    /*************************************************************/
291355efc227SAndreas Gohr    function _names2Tags($tags2Names)
291455efc227SAndreas Gohr    {
291555efc227SAndreas Gohr        $names2Tags = array();
291655efc227SAndreas Gohr        reset($tags2Names);
291755efc227SAndreas Gohr        while (list($tag, $name) = each($tags2Names)) {
291855efc227SAndreas Gohr            $names2Tags[$name] = $tag;
291955efc227SAndreas Gohr        }
292055efc227SAndreas Gohr
292155efc227SAndreas Gohr        return $names2Tags;
292255efc227SAndreas Gohr    }
292355efc227SAndreas Gohr
292455efc227SAndreas Gohr    /*************************************************************/
292555efc227SAndreas Gohr    function _getByte(&$data, $pos)
292655efc227SAndreas Gohr    {
292755efc227SAndreas Gohr        return ord($data{$pos});
292855efc227SAndreas Gohr    }
292955efc227SAndreas Gohr
293055efc227SAndreas Gohr    /*************************************************************/
293155efc227SAndreas Gohr    function _putByte(&$data, $pos, $val)
293255efc227SAndreas Gohr    {
293355efc227SAndreas Gohr        $val = intval($val);
293455efc227SAndreas Gohr
293555efc227SAndreas Gohr        $data{$pos} = chr($val);
293655efc227SAndreas Gohr
293755efc227SAndreas Gohr        return $pos + 1;
293855efc227SAndreas Gohr    }
293955efc227SAndreas Gohr
294055efc227SAndreas Gohr    /*************************************************************/
294155efc227SAndreas Gohr    function _getShort(&$data, $pos, $bigEndian = true)
294255efc227SAndreas Gohr    {
294355efc227SAndreas Gohr        if ($bigEndian) {
294455efc227SAndreas Gohr            return (ord($data{$pos}) << 8)
294555efc227SAndreas Gohr                   + ord($data{$pos + 1});
294655efc227SAndreas Gohr        }
294755efc227SAndreas Gohr        else {
294855efc227SAndreas Gohr            return ord($data{$pos})
294955efc227SAndreas Gohr                   + (ord($data{$pos + 1}) << 8);
295055efc227SAndreas Gohr        }
295155efc227SAndreas Gohr    }
295255efc227SAndreas Gohr
295355efc227SAndreas Gohr    /*************************************************************/
295455efc227SAndreas Gohr    function _putShort(&$data, $pos = 0, $val, $bigEndian = true)
295555efc227SAndreas Gohr    {
295655efc227SAndreas Gohr        $val = intval($val);
295755efc227SAndreas Gohr
295855efc227SAndreas Gohr        if ($bigEndian) {
295955efc227SAndreas Gohr            $data{$pos + 0} = chr(($val & 0x0000FF00) >> 8);
296055efc227SAndreas Gohr            $data{$pos + 1} = chr(($val & 0x000000FF) >> 0);
296155efc227SAndreas Gohr        }
296255efc227SAndreas Gohr        else {
296355efc227SAndreas Gohr            $data{$pos + 0} = chr(($val & 0x00FF) >> 0);
296455efc227SAndreas Gohr            $data{$pos + 1} = chr(($val & 0xFF00) >> 8);
296555efc227SAndreas Gohr        }
296655efc227SAndreas Gohr
296755efc227SAndreas Gohr        return $pos + 2;
296855efc227SAndreas Gohr    }
296955efc227SAndreas Gohr
297055efc227SAndreas Gohr    /*************************************************************/
297155efc227SAndreas Gohr    function _getLong(&$data, $pos, $bigEndian = true)
297255efc227SAndreas Gohr    {
297355efc227SAndreas Gohr        if ($bigEndian) {
297455efc227SAndreas Gohr            return (ord($data{$pos}) << 24)
297555efc227SAndreas Gohr                   + (ord($data{$pos + 1}) << 16)
297655efc227SAndreas Gohr                   + (ord($data{$pos + 2}) << 8)
297755efc227SAndreas Gohr                   + ord($data{$pos + 3});
297855efc227SAndreas Gohr        }
297955efc227SAndreas Gohr        else {
298055efc227SAndreas Gohr            return ord($data{$pos})
298155efc227SAndreas Gohr                   + (ord($data{$pos + 1}) << 8)
298255efc227SAndreas Gohr                   + (ord($data{$pos + 2}) << 16)
298355efc227SAndreas Gohr                   + (ord($data{$pos + 3}) << 24);
298455efc227SAndreas Gohr        }
298555efc227SAndreas Gohr    }
298655efc227SAndreas Gohr
298755efc227SAndreas Gohr    /*************************************************************/
298855efc227SAndreas Gohr    function _putLong(&$data, $pos, $val, $bigEndian = true)
298955efc227SAndreas Gohr    {
299055efc227SAndreas Gohr        $val = intval($val);
299155efc227SAndreas Gohr
299255efc227SAndreas Gohr        if ($bigEndian) {
299355efc227SAndreas Gohr            $data{$pos + 0} = chr(($val & 0xFF000000) >> 24);
299455efc227SAndreas Gohr            $data{$pos + 1} = chr(($val & 0x00FF0000) >> 16);
299555efc227SAndreas Gohr            $data{$pos + 2} = chr(($val & 0x0000FF00) >> 8);
299655efc227SAndreas Gohr            $data{$pos + 3} = chr(($val & 0x000000FF) >> 0);
299755efc227SAndreas Gohr        }
299855efc227SAndreas Gohr        else {
299955efc227SAndreas Gohr            $data{$pos + 0} = chr(($val & 0x000000FF) >> 0);
300055efc227SAndreas Gohr            $data{$pos + 1} = chr(($val & 0x0000FF00) >> 8);
300155efc227SAndreas Gohr            $data{$pos + 2} = chr(($val & 0x00FF0000) >> 16);
300255efc227SAndreas Gohr            $data{$pos + 3} = chr(($val & 0xFF000000) >> 24);
300355efc227SAndreas Gohr        }
300455efc227SAndreas Gohr
300555efc227SAndreas Gohr        return $pos + 4;
300655efc227SAndreas Gohr    }
300755efc227SAndreas Gohr
300855efc227SAndreas Gohr    /*************************************************************/
300955efc227SAndreas Gohr    function & _getNullString(&$data, $pos)
301055efc227SAndreas Gohr    {
301155efc227SAndreas Gohr        $str = '';
301255efc227SAndreas Gohr        $max = strlen($data);
301355efc227SAndreas Gohr
301455efc227SAndreas Gohr        while ($pos < $max) {
301555efc227SAndreas Gohr            if (ord($data{$pos}) == 0) {
301655efc227SAndreas Gohr                return $str;
301755efc227SAndreas Gohr            }
301855efc227SAndreas Gohr            else {
301955efc227SAndreas Gohr                $str .= $data{$pos};
302055efc227SAndreas Gohr            }
302155efc227SAndreas Gohr            $pos++;
302255efc227SAndreas Gohr        }
302355efc227SAndreas Gohr
302455efc227SAndreas Gohr        return $str;
302555efc227SAndreas Gohr    }
302655efc227SAndreas Gohr
302755efc227SAndreas Gohr    /*************************************************************/
302855efc227SAndreas Gohr    function & _getFixedString(&$data, $pos, $length = -1)
302955efc227SAndreas Gohr    {
303055efc227SAndreas Gohr        if ($length == -1) {
303155efc227SAndreas Gohr            $length = strlen($data) - $pos;
303255efc227SAndreas Gohr        }
303355efc227SAndreas Gohr
303455efc227SAndreas Gohr        return substr($data, $pos, $length);
303555efc227SAndreas Gohr    }
303655efc227SAndreas Gohr
303755efc227SAndreas Gohr    /*************************************************************/
303855efc227SAndreas Gohr    function _putString(&$data, $pos, &$str)
303955efc227SAndreas Gohr    {
304055efc227SAndreas Gohr        $len = strlen($str);
304155efc227SAndreas Gohr        for ($i = 0; $i < $len; $i++) {
304255efc227SAndreas Gohr          $data{$pos + $i} = $str{$i};
304355efc227SAndreas Gohr        }
304455efc227SAndreas Gohr
304555efc227SAndreas Gohr        return $pos + $len;
304655efc227SAndreas Gohr    }
304755efc227SAndreas Gohr
304855efc227SAndreas Gohr    /*************************************************************/
304955efc227SAndreas Gohr    function _hexDump(&$data, $start = 0, $length = -1)
305055efc227SAndreas Gohr    {
305155efc227SAndreas Gohr        if (($length == -1) || (($length + $start) > strlen($data))) {
305255efc227SAndreas Gohr            $end = strlen($data);
305355efc227SAndreas Gohr        }
305455efc227SAndreas Gohr        else {
305555efc227SAndreas Gohr            $end = $start + $length;
305655efc227SAndreas Gohr        }
305755efc227SAndreas Gohr
305855efc227SAndreas Gohr        $ascii = '';
305955efc227SAndreas Gohr        $count = 0;
306055efc227SAndreas Gohr
306155efc227SAndreas Gohr        echo "<tt>\n";
306255efc227SAndreas Gohr
306355efc227SAndreas Gohr        while ($start < $end) {
306455efc227SAndreas Gohr            if (($count % 16) == 0) {
306555efc227SAndreas Gohr                echo sprintf('%04d', $count) . ': ';
306655efc227SAndreas Gohr            }
306755efc227SAndreas Gohr
306855efc227SAndreas Gohr            $c = ord($data{$start});
306955efc227SAndreas Gohr            $count++;
307055efc227SAndreas Gohr            $start++;
307155efc227SAndreas Gohr
307255efc227SAndreas Gohr            $aux = dechex($c);
307355efc227SAndreas Gohr            if (strlen($aux) == 1)
307455efc227SAndreas Gohr                echo '0';
307555efc227SAndreas Gohr            echo $aux . ' ';
307655efc227SAndreas Gohr
307755efc227SAndreas Gohr            if ($c == 60)
307855efc227SAndreas Gohr                $ascii .= '&lt;';
307955efc227SAndreas Gohr            elseif ($c == 62)
308055efc227SAndreas Gohr                $ascii .= '&gt;';
308155efc227SAndreas Gohr            elseif ($c == 32)
308255efc227SAndreas Gohr                $ascii .= '&nbsp;';
308355efc227SAndreas Gohr            elseif ($c > 32)
308455efc227SAndreas Gohr                $ascii .= chr($c);
308555efc227SAndreas Gohr            else
308655efc227SAndreas Gohr                $ascii .= '.';
308755efc227SAndreas Gohr
308855efc227SAndreas Gohr            if (($count % 4) == 0) {
308955efc227SAndreas Gohr                echo ' - ';
309055efc227SAndreas Gohr            }
309155efc227SAndreas Gohr
309255efc227SAndreas Gohr            if (($count % 16) == 0) {
309355efc227SAndreas Gohr                echo ': ' . $ascii . "<br>\n";
309455efc227SAndreas Gohr                $ascii = '';
309555efc227SAndreas Gohr            }
309655efc227SAndreas Gohr        }
309755efc227SAndreas Gohr
309855efc227SAndreas Gohr        if ($ascii != '') {
309955efc227SAndreas Gohr            while (($count % 16) != 0) {
310055efc227SAndreas Gohr                echo '-- ';
310155efc227SAndreas Gohr                $count++;
310255efc227SAndreas Gohr                if (($count % 4) == 0) {
310355efc227SAndreas Gohr                    echo ' - ';
310455efc227SAndreas Gohr                }
310555efc227SAndreas Gohr            }
310655efc227SAndreas Gohr            echo ': ' . $ascii . "<br>\n";
310755efc227SAndreas Gohr        }
310855efc227SAndreas Gohr
310955efc227SAndreas Gohr        echo "</tt>\n";
311055efc227SAndreas Gohr    }
311155efc227SAndreas Gohr
311255efc227SAndreas Gohr/*****************************************************************/
311355efc227SAndreas Gohr}
311455efc227SAndreas Gohr
311555efc227SAndreas Gohr/* vim: set expandtab tabstop=4 shiftwidth=4: */
311655efc227SAndreas Gohr
3117