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