3 * JPEG metadata reader/writer
5 * @license BSD <http://www.opensource.org/licenses/bsd-license.php>
6 * @link http://github.com/sd/jpeg-php
7 * @author Sebastian Delmont <sdelmont@zonageek.com>
8 * @author Andreas Gohr <andi@splitbrain.org>
9 * @author Hakan Sandell <hakan.sandell@mydata.se>
10 * @todo Add support for Maker Notes, Extend for GIF and PNG metadata
13 // Original copyright notice:
15 // Copyright (c) 2003 Sebastian Delmont <sdelmont@zonageek.com>
16 // All rights reserved.
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions
21 // 1. Redistributions of source code must retain the above copyright
22 // notice, this list of conditions and the following disclaimer.
23 // 2. Redistributions in binary form must reproduce the above copyright
24 // notice, this list of conditions and the following disclaimer in the
25 // documentation and/or other materials provided with the distribution.
26 // 3. Neither the name of the author nor the names of its contributors
27 // may be used to endorse or promote products derived from this software
28 // without specific prior written permission.
30 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
31 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
32 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
33 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
36 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
37 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
38 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
39 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
40 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
46 var $_type = 'unknown';
55 * @author Sebastian Delmont <sdelmont@zonageek.com>
59 function __construct($fileName) {
61 $this->_fileName
= $fileName;
64 $this->_type
= 'unknown';
67 unset($this->_markers
);
71 * Returns all gathered info as multidim array
73 * @author Sebastian Delmont <sdelmont@zonageek.com>
75 function & getRawInfo() {
78 if ($this->_markers
== null) {
86 * Returns basic image info
88 * @author Sebastian Delmont <sdelmont@zonageek.com>
90 function & getBasicInfo() {
95 if ($this->_markers
== null) {
99 $info['Name'] = $this->_info
['file']['Name'];
100 if (isset($this->_info
['file']['Url'])) {
101 $info['Url'] = $this->_info
['file']['Url'];
102 $info['NiceSize'] = "???KB";
104 $info['Size'] = $this->_info
['file']['Size'];
105 $info['NiceSize'] = $this->_info
['file']['NiceSize'];
108 if (@isset
($this->_info
['sof']['Format'])) {
109 $info['Format'] = $this->_info
['sof']['Format'] . " JPEG";
111 $info['Format'] = $this->_info
['sof']['Format'] . " JPEG";
114 if (@isset
($this->_info
['sof']['ColorChannels'])) {
115 $info['ColorMode'] = ($this->_info
['sof']['ColorChannels'] > 1) ?
"Color" : "B&W";
118 $info['Width'] = $this->getWidth();
119 $info['Height'] = $this->getHeight();
120 $info['DimStr'] = $this->getDimStr();
122 $dates = $this->getDates();
124 $info['DateTime'] = $dates['EarliestTime'];
125 $info['DateTimeStr'] = $dates['EarliestTimeStr'];
127 $info['HasThumbnail'] = $this->hasThumbnail();
134 * Convinience function to access nearly all available Data
135 * through one function
137 * @author Andreas Gohr <andi@splitbrain.org>
139 * @param array|string $fields field name or array with field names
140 * @return bool|string
142 function getField($fields) {
143 if(!is_array($fields)) $fields = array($fields);
145 foreach($fields as $field){
146 if(strtolower(substr($field,0,5)) == 'iptc.'){
147 $info = $this->getIPTCField(substr($field,5));
148 }elseif(strtolower(substr($field,0,5)) == 'exif.'){
149 $info = $this->getExifField(substr($field,5));
150 }elseif(strtolower(substr($field,0,4)) == 'xmp.'){
151 $info = $this->getXmpField(substr($field,4));
152 }elseif(strtolower(substr($field,0,5)) == 'file.'){
153 $info = $this->getFileField(substr($field,5));
154 }elseif(strtolower(substr($field,0,5)) == 'date.'){
155 $info = $this->getDateField(substr($field,5));
156 }elseif(strtolower($field) == 'simple.camera'){
157 $info = $this->getCamera();
158 }elseif(strtolower($field) == 'simple.raw'){
159 return $this->getRawInfo();
160 }elseif(strtolower($field) == 'simple.title'){
161 $info = $this->getTitle();
162 }elseif(strtolower($field) == 'simple.shutterspeed'){
163 $info = $this->getShutterSpeed();
165 $info = $this->getExifField($field);
167 if($info != false) break;
170 if($info === false) $info = '';
172 if(isset($info['val'])){
173 $info = $info['val'];
175 $info = join(', ',$info);
182 * Convinience function to set nearly all available Data
183 * through one function
185 * @author Andreas Gohr <andi@splitbrain.org>
187 * @param string $field field name
188 * @param string $value
189 * @return bool success or fail
191 function setField($field, $value) {
192 if(strtolower(substr($field,0,5)) == 'iptc.'){
193 return $this->setIPTCField(substr($field,5),$value);
194 }elseif(strtolower(substr($field,0,5)) == 'exif.'){
195 return $this->setExifField(substr($field,5),$value);
197 return $this->setExifField($field,$value);
202 * Convinience function to delete nearly all available Data
203 * through one function
205 * @author Andreas Gohr <andi@splitbrain.org>
207 * @param string $field field name
210 function deleteField($field) {
211 if(strtolower(substr($field,0,5)) == 'iptc.'){
212 return $this->deleteIPTCField(substr($field,5));
213 }elseif(strtolower(substr($field,0,5)) == 'exif.'){
214 return $this->deleteExifField(substr($field,5));
216 return $this->deleteExifField($field);
221 * Return a date field
223 * @author Andreas Gohr <andi@splitbrain.org>
225 * @param string $field
226 * @return false|string
228 function getDateField($field) {
229 if (!isset($this->_info
['dates'])) {
230 $this->_info
['dates'] = $this->getDates();
233 if (isset($this->_info
['dates'][$field])) {
234 return $this->_info
['dates'][$field];
241 * Return a file info field
243 * @author Andreas Gohr <andi@splitbrain.org>
245 * @param string $field field name
246 * @return false|string
248 function getFileField($field) {
249 if (!isset($this->_info
['file'])) {
250 $this->_parseFileInfo();
253 if (isset($this->_info
['file'][$field])) {
254 return $this->_info
['file'][$field];
261 * Return the camera info (Maker and Model)
263 * @author Andreas Gohr <andi@splitbrain.org>
264 * @todo handle makernotes
266 * @return false|string
268 function getCamera(){
269 $make = $this->getField(array('Exif.Make','Exif.TIFFMake'));
270 $model = $this->getField(array('Exif.Model','Exif.TIFFModel'));
271 $cam = trim("$make $model");
272 if(empty($cam)) return false;
277 * Return shutter speed as a ratio
279 * @author Joe Lapp <joe.lapp@pobox.com>
283 function getShutterSpeed() {
284 if (!isset($this->_info
['exif'])) {
285 $this->_parseMarkerExif();
287 if(!isset($this->_info
['exif']['ExposureTime'])){
291 $field = $this->_info
['exif']['ExposureTime'];
292 if($field['den'] == 1) return $field['num'];
293 return $field['num'].'/'.$field['den'];
297 * Return an EXIF field
299 * @author Sebastian Delmont <sdelmont@zonageek.com>
301 * @param string $field field name
302 * @return false|string
304 function getExifField($field) {
305 if (!isset($this->_info
['exif'])) {
306 $this->_parseMarkerExif();
309 if ($this->_markers
== null) {
313 if (isset($this->_info
['exif'][$field])) {
314 return $this->_info
['exif'][$field];
321 * Return an XMP field
323 * @author Hakan Sandell <hakan.sandell@mydata.se>
325 * @param string $field field name
326 * @return false|string
328 function getXmpField($field) {
329 if (!isset($this->_info
['xmp'])) {
330 $this->_parseMarkerXmp();
333 if ($this->_markers
== null) {
337 if (isset($this->_info
['xmp'][$field])) {
338 return $this->_info
['xmp'][$field];
345 * Return an Adobe Field
347 * @author Sebastian Delmont <sdelmont@zonageek.com>
349 * @param string $field field name
350 * @return false|string
352 function getAdobeField($field) {
353 if (!isset($this->_info
['adobe'])) {
354 $this->_parseMarkerAdobe();
357 if ($this->_markers
== null) {
361 if (isset($this->_info
['adobe'][$field])) {
362 return $this->_info
['adobe'][$field];
369 * Return an IPTC field
371 * @author Sebastian Delmont <sdelmont@zonageek.com>
373 * @param string $field field name
374 * @return false|string
376 function getIPTCField($field) {
377 if (!isset($this->_info
['iptc'])) {
378 $this->_parseMarkerAdobe();
381 if ($this->_markers
== null) {
385 if (isset($this->_info
['iptc'][$field])) {
386 return $this->_info
['iptc'][$field];
395 * @author Sebastian Delmont <sdelmont@zonageek.com>
396 * @author Joe Lapp <joe.lapp@pobox.com>
398 * @param string $field field name
399 * @param string $value
402 function setExifField($field, $value) {
403 if (!isset($this->_info
['exif'])) {
404 $this->_parseMarkerExif();
407 if ($this->_markers
== null) {
411 if ($this->_info
['exif'] == false) {
412 $this->_info
['exif'] = array();
415 // make sure datetimes are in correct format
416 if(strlen($field) >= 8 && strtolower(substr($field, 0, 8)) == 'datetime') {
417 if(strlen($value) < 8 ||
$value[4] != ':' ||
$value[7] != ':') {
418 $value = date('Y:m:d H:i:s', strtotime($value));
422 $this->_info
['exif'][$field] = $value;
430 * @author Sebastian Delmont <sdelmont@zonageek.com>
432 * @param string $field field name
433 * @param string $value
436 function setAdobeField($field, $value) {
437 if (!isset($this->_info
['adobe'])) {
438 $this->_parseMarkerAdobe();
441 if ($this->_markers
== null) {
445 if ($this->_info
['adobe'] == false) {
446 $this->_info
['adobe'] = array();
449 $this->_info
['adobe'][$field] = $value;
455 * Calculates the multiplier needed to resize the image to the given
458 * @author Andreas Gohr <andi@splitbrain.org>
460 * @param int $maxwidth
461 * @param int $maxheight
464 function getResizeRatio($maxwidth,$maxheight=0){
465 if(!$maxheight) $maxheight = $maxwidth;
467 $w = $this->getField('File.Width');
468 $h = $this->getField('File.Height');
473 $ratio = $maxwidth/$w;
474 }elseif($h > $maxheight){
475 $ratio = $maxheight/$h;
478 if($h >= $maxheight){
479 $ratio = $maxheight/$h;
480 }elseif($w > $maxwidth){
481 $ratio = $maxwidth/$w;
491 * @author Sebastian Delmont <sdelmont@zonageek.com>
493 * @param string $field field name
494 * @param string $value
497 function setIPTCField($field, $value) {
498 if (!isset($this->_info
['iptc'])) {
499 $this->_parseMarkerAdobe();
502 if ($this->_markers
== null) {
506 if ($this->_info
['iptc'] == false) {
507 $this->_info
['iptc'] = array();
510 $this->_info
['iptc'][$field] = $value;
516 * Delete an EXIF field
518 * @author Sebastian Delmont <sdelmont@zonageek.com>
520 * @param string $field field name
523 function deleteExifField($field) {
524 if (!isset($this->_info
['exif'])) {
525 $this->_parseMarkerAdobe();
528 if ($this->_markers
== null) {
532 if ($this->_info
['exif'] != false) {
533 unset($this->_info
['exif'][$field]);
540 * Delete an Adobe field
542 * @author Sebastian Delmont <sdelmont@zonageek.com>
544 * @param string $field field name
547 function deleteAdobeField($field) {
548 if (!isset($this->_info
['adobe'])) {
549 $this->_parseMarkerAdobe();
552 if ($this->_markers
== null) {
556 if ($this->_info
['adobe'] != false) {
557 unset($this->_info
['adobe'][$field]);
564 * Delete an IPTC field
566 * @author Sebastian Delmont <sdelmont@zonageek.com>
568 * @param string $field field name
571 function deleteIPTCField($field) {
572 if (!isset($this->_info
['iptc'])) {
573 $this->_parseMarkerAdobe();
576 if ($this->_markers
== null) {
580 if ($this->_info
['iptc'] != false) {
581 unset($this->_info
['iptc'][$field]);
588 * Get the image's title, tries various fields
590 * @param int $max maximum number chars (keeps words)
591 * @return false|string
593 * @author Andreas Gohr <andi@splitbrain.org>
595 function getTitle($max=80){
596 // try various fields
597 $cap = $this->getField(array('Iptc.Headline',
601 'Exif.TIFFUserComment',
602 'Exif.TIFFImageDescription',
604 if (empty($cap)) return false;
606 if(!$max) return $cap;
607 // Shorten to 80 chars (keeping words)
608 $new = preg_replace('/\n.+$/','',wordwrap($cap, $max));
609 if($new != $cap) $new .= '...';
615 * Gather various date fields
617 * @author Sebastian Delmont <sdelmont@zonageek.com>
621 function getDates() {
623 if ($this->_markers
== null) {
624 if (@isset
($this->_info
['file']['UnixTime'])) {
626 $dates['FileModified'] = $this->_info
['file']['UnixTime'];
627 $dates['Time'] = $this->_info
['file']['UnixTime'];
628 $dates['TimeSource'] = 'FileModified';
629 $dates['TimeStr'] = date("Y-m-d H:i:s", $this->_info
['file']['UnixTime']);
630 $dates['EarliestTime'] = $this->_info
['file']['UnixTime'];
631 $dates['EarliestTimeSource'] = 'FileModified';
632 $dates['EarliestTimeStr'] = date("Y-m-d H:i:s", $this->_info
['file']['UnixTime']);
633 $dates['LatestTime'] = $this->_info
['file']['UnixTime'];
634 $dates['LatestTimeSource'] = 'FileModified';
635 $dates['LatestTimeStr'] = date("Y-m-d H:i:s", $this->_info
['file']['UnixTime']);
644 $latestTimeSource = "";
645 $earliestTime = time();
646 $earliestTimeSource = "";
648 if (@isset
($this->_info
['exif']['DateTime'])) {
649 $dates['ExifDateTime'] = $this->_info
['exif']['DateTime'];
651 $aux = $this->_info
['exif']['DateTime'];
654 $t = strtotime($aux);
656 if ($t && $t > $latestTime) {
658 $latestTimeSource = "ExifDateTime";
661 if ($t && $t < $earliestTime) {
663 $earliestTimeSource = "ExifDateTime";
667 if (@isset
($this->_info
['exif']['DateTimeOriginal'])) {
668 $dates['ExifDateTimeOriginal'] = $this->_info
['exif']['DateTime'];
670 $aux = $this->_info
['exif']['DateTimeOriginal'];
673 $t = strtotime($aux);
675 if ($t && $t > $latestTime) {
677 $latestTimeSource = "ExifDateTimeOriginal";
680 if ($t && $t < $earliestTime) {
682 $earliestTimeSource = "ExifDateTimeOriginal";
686 if (@isset
($this->_info
['exif']['DateTimeDigitized'])) {
687 $dates['ExifDateTimeDigitized'] = $this->_info
['exif']['DateTime'];
689 $aux = $this->_info
['exif']['DateTimeDigitized'];
692 $t = strtotime($aux);
694 if ($t && $t > $latestTime) {
696 $latestTimeSource = "ExifDateTimeDigitized";
699 if ($t && $t < $earliestTime) {
701 $earliestTimeSource = "ExifDateTimeDigitized";
705 if (@isset
($this->_info
['iptc']['DateCreated'])) {
706 $dates['IPTCDateCreated'] = $this->_info
['iptc']['DateCreated'];
708 $aux = $this->_info
['iptc']['DateCreated'];
709 $aux = substr($aux, 0, 4) . "-" . substr($aux, 4, 2) . "-" . substr($aux, 6, 2);
710 $t = strtotime($aux);
712 if ($t && $t > $latestTime) {
714 $latestTimeSource = "IPTCDateCreated";
717 if ($t && $t < $earliestTime) {
719 $earliestTimeSource = "IPTCDateCreated";
723 if (@isset
($this->_info
['file']['UnixTime'])) {
724 $dates['FileModified'] = $this->_info
['file']['UnixTime'];
726 $t = $this->_info
['file']['UnixTime'];
728 if ($t && $t > $latestTime) {
730 $latestTimeSource = "FileModified";
733 if ($t && $t < $earliestTime) {
735 $earliestTimeSource = "FileModified";
739 $dates['Time'] = $earliestTime;
740 $dates['TimeSource'] = $earliestTimeSource;
741 $dates['TimeStr'] = date("Y-m-d H:i:s", $earliestTime);
742 $dates['EarliestTime'] = $earliestTime;
743 $dates['EarliestTimeSource'] = $earliestTimeSource;
744 $dates['EarliestTimeStr'] = date("Y-m-d H:i:s", $earliestTime);
745 $dates['LatestTime'] = $latestTime;
746 $dates['LatestTimeSource'] = $latestTimeSource;
747 $dates['LatestTimeStr'] = date("Y-m-d H:i:s", $latestTime);
753 * Get the image width, tries various fields
755 * @author Sebastian Delmont <sdelmont@zonageek.com>
757 * @return false|string
759 function getWidth() {
760 if (!isset($this->_info
['sof'])) {
761 $this->_parseMarkerSOF();
764 if ($this->_markers
== null) {
768 if (isset($this->_info
['sof']['ImageWidth'])) {
769 return $this->_info
['sof']['ImageWidth'];
772 if (!isset($this->_info
['exif'])) {
773 $this->_parseMarkerExif();
776 if (isset($this->_info
['exif']['PixelXDimension'])) {
777 return $this->_info
['exif']['PixelXDimension'];
784 * Get the image height, tries various fields
786 * @author Sebastian Delmont <sdelmont@zonageek.com>
788 * @return false|string
790 function getHeight() {
791 if (!isset($this->_info
['sof'])) {
792 $this->_parseMarkerSOF();
795 if ($this->_markers
== null) {
799 if (isset($this->_info
['sof']['ImageHeight'])) {
800 return $this->_info
['sof']['ImageHeight'];
803 if (!isset($this->_info
['exif'])) {
804 $this->_parseMarkerExif();
807 if (isset($this->_info
['exif']['PixelYDimension'])) {
808 return $this->_info
['exif']['PixelYDimension'];
815 * Get an dimension string for use in img tag
817 * @author Sebastian Delmont <sdelmont@zonageek.com>
819 * @return false|string
821 function getDimStr() {
822 if ($this->_markers
== null) {
826 $w = $this->getWidth();
827 $h = $this->getHeight();
829 return "width='" . $w . "' height='" . $h . "'";
833 * Checks for an embedded thumbnail
835 * @author Sebastian Delmont <sdelmont@zonageek.com>
837 * @param string $which possible values: 'any', 'exif' or 'adobe'
838 * @return false|string
840 function hasThumbnail($which = 'any') {
841 if (($which == 'any') ||
($which == 'exif')) {
842 if (!isset($this->_info
['exif'])) {
843 $this->_parseMarkerExif();
846 if ($this->_markers
== null) {
850 if (isset($this->_info
['exif']) && is_array($this->_info
['exif'])) {
851 if (isset($this->_info
['exif']['JFIFThumbnail'])) {
857 if ($which == 'adobe') {
858 if (!isset($this->_info
['adobe'])) {
859 $this->_parseMarkerAdobe();
862 if ($this->_markers
== null) {
866 if (isset($this->_info
['adobe']) && is_array($this->_info
['adobe'])) {
867 if (isset($this->_info
['adobe']['ThumbnailData'])) {
877 * Send embedded thumbnail to browser
879 * @author Sebastian Delmont <sdelmont@zonageek.com>
881 * @param string $which possible values: 'any', 'exif' or 'adobe'
884 function sendThumbnail($which = 'any') {
887 if (($which == 'any') ||
($which == 'exif')) {
888 if (!isset($this->_info
['exif'])) {
889 $this->_parseMarkerExif();
892 if ($this->_markers
== null) {
896 if (isset($this->_info
['exif']) && is_array($this->_info
['exif'])) {
897 if (isset($this->_info
['exif']['JFIFThumbnail'])) {
898 $data =& $this->_info
['exif']['JFIFThumbnail'];
903 if (($which == 'adobe') ||
($data == null)){
904 if (!isset($this->_info
['adobe'])) {
905 $this->_parseMarkerAdobe();
908 if ($this->_markers
== null) {
912 if (isset($this->_info
['adobe']) && is_array($this->_info
['adobe'])) {
913 if (isset($this->_info
['adobe']['ThumbnailData'])) {
914 $data =& $this->_info
['adobe']['ThumbnailData'];
920 header("Content-type: image/jpeg");
929 * Save changed Metadata
931 * @author Sebastian Delmont <sdelmont@zonageek.com>
932 * @author Andreas Gohr <andi@splitbrain.org>
934 * @param string $fileName file name or empty string for a random name
937 function save($fileName = "") {
938 if ($fileName == "") {
939 $tmpName = tempnam(dirname($this->_fileName
),'_metatemp_');
940 $this->_writeJPEG($tmpName);
941 if (file_exists($tmpName)) {
942 return io_rename($tmpName, $this->_fileName
);
945 return $this->_writeJPEG($fileName);
950 /*************************************************************/
951 /* PRIVATE FUNCTIONS (Internal Use Only!) */
952 /*************************************************************/
954 /*************************************************************/
955 function _dispose($fileName = "") {
956 $this->_fileName
= $fileName;
959 $this->_type
= 'unknown';
961 unset($this->_markers
);
965 /*************************************************************/
966 function _readJPEG() {
967 unset($this->_markers
);
968 //unset($this->_info);
969 $this->_markers
= array();
970 //$this->_info = array();
972 $this->_fp
= @fopen
($this->_fileName
, 'rb');
974 if (file_exists($this->_fileName
)) {
975 $this->_type
= 'file';
978 $this->_type
= 'url';
982 return false; // ERROR: Can't open file
985 // Check for the JPEG signature
986 $c1 = ord(fgetc($this->_fp
));
987 $c2 = ord(fgetc($this->_fp
));
989 if ($c1 != 0xFF ||
$c2 != 0xD8) { // (0xFF + SOI)
990 $this->_markers
= null;
991 return false; // ERROR: File is not a JPEG
1002 // First, skip any non 0xFF bytes
1004 $c = ord(fgetc($this->_fp
));
1005 while (!feof($this->_fp
) && ($c != 0xFF)) {
1007 $c = ord(fgetc($this->_fp
));
1009 // Then skip all 0xFF until the marker byte
1011 $marker = ord(fgetc($this->_fp
));
1012 } while (!feof($this->_fp
) && ($marker == 0xFF));
1014 if (feof($this->_fp
)) {
1015 return false; // ERROR: Unexpected EOF
1017 if ($discarded != 0) {
1018 return false; // ERROR: Extraneous data
1021 $length = ord(fgetc($this->_fp
)) * 256 +
ord(fgetc($this->_fp
));
1022 if (feof($this->_fp
)) {
1023 return false; // ERROR: Unexpected EOF
1026 return false; // ERROR: Extraneous data
1028 $length = $length - 2; // The length we got counts itself
1035 case 0xE0: // APP0: JFIF data
1036 case 0xE1: // APP1: EXIF or XMP data
1037 case 0xED: // APP13: IPTC / Photoshop data
1040 case 0xDA: // SOS: Start of scan... the image itself and the last block on the file
1042 $length = -1; // This field has no length... it includes all data until EOF
1046 $capture = true;//false;
1050 $this->_markers
[$count] = array();
1051 $this->_markers
[$count]['marker'] = $marker;
1052 $this->_markers
[$count]['length'] = $length;
1056 $this->_markers
[$count]['data'] = fread($this->_fp
, $length);
1058 $this->_markers
[$count]['data'] = "";
1061 $result = @fseek
($this->_fp
, $length, SEEK_CUR
);
1062 // fseek doesn't seem to like HTTP 'files', but fgetc has no problem
1063 if (!($result === 0)) {
1064 for ($i = 0; $i < $length; $i++
) {
1080 /*************************************************************/
1081 function _parseAll() {
1082 if (!isset($this->_info
['file'])) {
1083 $this->_parseFileInfo();
1085 if (!isset($this->_markers
)) {
1089 if ($this->_markers
== null) {
1093 if (!isset($this->_info
['jfif'])) {
1094 $this->_parseMarkerJFIF();
1096 if (!isset($this->_info
['jpeg'])) {
1097 $this->_parseMarkerSOF();
1099 if (!isset($this->_info
['exif'])) {
1100 $this->_parseMarkerExif();
1102 if (!isset($this->_info
['xmp'])) {
1103 $this->_parseMarkerXmp();
1105 if (!isset($this->_info
['adobe'])) {
1106 $this->_parseMarkerAdobe();
1110 /*************************************************************/
1113 * @param string $outputName
1117 function _writeJPEG($outputName) {
1121 $wroteAdobe = false;
1123 $this->_fp
= @fopen
($this->_fileName
, 'r');
1125 if (file_exists($this->_fileName
)) {
1126 $this->_type
= 'file';
1129 $this->_type
= 'url';
1133 return false; // ERROR: Can't open file
1136 $this->_fpout
= fopen($outputName, 'wb');
1137 if (!$this->_fpout
) {
1138 $this->_fpout
= null;
1141 return false; // ERROR: Can't open output file
1144 // Check for the JPEG signature
1145 $c1 = ord(fgetc($this->_fp
));
1146 $c2 = ord(fgetc($this->_fp
));
1148 if ($c1 != 0xFF ||
$c2 != 0xD8) { // (0xFF + SOI)
1149 return false; // ERROR: File is not a JPEG
1152 fputs($this->_fpout
, chr(0xFF), 1);
1153 fputs($this->_fpout
, chr(0xD8), 1); // (0xFF + SOI)
1161 // First, skip any non 0xFF bytes
1163 $c = ord(fgetc($this->_fp
));
1164 while (!feof($this->_fp
) && ($c != 0xFF)) {
1166 $c = ord(fgetc($this->_fp
));
1168 // Then skip all 0xFF until the marker byte
1170 $marker = ord(fgetc($this->_fp
));
1171 } while (!feof($this->_fp
) && ($marker == 0xFF));
1173 if (feof($this->_fp
)) {
1175 break; // ERROR: Unexpected EOF
1177 if ($discarded != 0) {
1179 break; // ERROR: Extraneous data
1182 $length = ord(fgetc($this->_fp
)) * 256 +
ord(fgetc($this->_fp
));
1183 if (feof($this->_fp
)) {
1185 break; // ERROR: Unexpected EOF
1189 break; // ERROR: Extraneous data
1191 $length = $length - 2; // The length we got counts itself
1194 if ($marker == 0xE1) { // APP1: EXIF data
1195 $data =& $this->_createMarkerEXIF();
1198 elseif ($marker == 0xED) { // APP13: IPTC / Photoshop data
1199 $data =& $this->_createMarkerAdobe();
1202 elseif ($marker == 0xDA) { // SOS: Start of scan... the image itself and the last block on the file
1206 if (!$wroteEXIF && (($marker < 0xE0) ||
($marker > 0xEF))) {
1207 if (isset($this->_info
['exif']) && is_array($this->_info
['exif'])) {
1208 $exif =& $this->_createMarkerEXIF();
1209 $this->_writeJPEGMarker(0xE1, strlen($exif), $exif, 0);
1215 if (!$wroteAdobe && (($marker < 0xE0) ||
($marker > 0xEF))) {
1216 if ((isset($this->_info
['adobe']) && is_array($this->_info
['adobe']))
1217 ||
(isset($this->_info
['iptc']) && is_array($this->_info
['iptc']))) {
1218 $adobe =& $this->_createMarkerAdobe();
1219 $this->_writeJPEGMarker(0xED, strlen($adobe), $adobe, 0);
1225 $origLength = $length;
1227 $length = strlen($data);
1230 if ($marker != -1) {
1231 $this->_writeJPEGMarker($marker, $length, $data, $origLength);
1240 if ($this->_fpout
) {
1241 fclose($this->_fpout
);
1242 $this->_fpout
= null;
1248 /*************************************************************/
1251 * @param integer $marker
1252 * @param integer $length
1253 * @param string $data
1254 * @param integer $origLength
1258 function _writeJPEGMarker($marker, $length, &$data, $origLength) {
1263 fputs($this->_fpout
, chr(0xFF), 1);
1264 fputs($this->_fpout
, chr($marker), 1);
1265 fputs($this->_fpout
, chr((($length +
2) & 0x0000FF00) >> 8), 1);
1266 fputs($this->_fpout
, chr((($length +
2) & 0x000000FF) >> 0), 1);
1269 // Copy the generated data
1270 fputs($this->_fpout
, $data, $length);
1272 if ($origLength > 0) { // Skip the original data
1273 $result = @fseek
($this->_fp
, $origLength, SEEK_CUR
);
1274 // fseek doesn't seem to like HTTP 'files', but fgetc has no problem
1276 for ($i = 0; $i < $origLength; $i++
) {
1282 if ($marker == 0xDA) { // Copy until EOF
1283 while (!feof($this->_fp
)) {
1284 $data = fread($this->_fp
, 1024 * 16);
1285 fputs($this->_fpout
, $data, strlen($data));
1287 } else { // Copy only $length bytes
1288 $data = @fread
($this->_fp
, $length);
1289 fputs($this->_fpout
, $data, $length);
1297 * Gets basic info from the file - should work with non-JPEGs
1299 * @author Sebastian Delmont <sdelmont@zonageek.com>
1300 * @author Andreas Gohr <andi@splitbrain.org>
1302 function _parseFileInfo() {
1303 if (file_exists($this->_fileName
) && is_file($this->_fileName
)) {
1304 $this->_info
['file'] = array();
1305 $this->_info
['file']['Name'] = utf8_decodeFN(\dokuwiki\Utf8\PhpString
::basename($this->_fileName
));
1306 $this->_info
['file']['Path'] = fullpath($this->_fileName
);
1307 $this->_info
['file']['Size'] = filesize($this->_fileName
);
1308 if ($this->_info
['file']['Size'] < 1024) {
1309 $this->_info
['file']['NiceSize'] = $this->_info
['file']['Size'] . 'B';
1310 } elseif ($this->_info
['file']['Size'] < (1024 * 1024)) {
1311 $this->_info
['file']['NiceSize'] = round($this->_info
['file']['Size'] / 1024) . 'KB';
1312 } elseif ($this->_info
['file']['Size'] < (1024 * 1024 * 1024)) {
1313 $this->_info
['file']['NiceSize'] = round($this->_info
['file']['Size'] / (1024*1024)) . 'MB';
1315 $this->_info
['file']['NiceSize'] = $this->_info
['file']['Size'] . 'B';
1317 $this->_info
['file']['UnixTime'] = filemtime($this->_fileName
);
1319 // get image size directly from file
1320 if ($size = getimagesize($this->_fileName
)) {
1321 $this->_info
['file']['Width'] = $size[0];
1322 $this->_info
['file']['Height'] = $size[1];
1324 // set mime types and formats
1325 // http://php.net/manual/en/function.getimagesize.php
1326 // http://php.net/manual/en/function.image-type-to-mime-type.php
1329 $this->_info
['file']['Mime'] = 'image/gif';
1330 $this->_info
['file']['Format'] = 'GIF';
1333 $this->_info
['file']['Mime'] = 'image/jpeg';
1334 $this->_info
['file']['Format'] = 'JPEG';
1337 $this->_info
['file']['Mime'] = 'image/png';
1338 $this->_info
['file']['Format'] = 'PNG';
1341 $this->_info
['file']['Mime'] = 'application/x-shockwave-flash';
1342 $this->_info
['file']['Format'] = 'SWF';
1345 $this->_info
['file']['Mime'] = 'image/psd';
1346 $this->_info
['file']['Format'] = 'PSD';
1349 $this->_info
['file']['Mime'] = 'image/bmp';
1350 $this->_info
['file']['Format'] = 'BMP';
1353 $this->_info
['file']['Mime'] = 'image/tiff';
1354 $this->_info
['file']['Format'] = 'TIFF (Intel)';
1357 $this->_info
['file']['Mime'] = 'image/tiff';
1358 $this->_info
['file']['Format'] = 'TIFF (Motorola)';
1361 $this->_info
['file']['Mime'] = 'application/octet-stream';
1362 $this->_info
['file']['Format'] = 'JPC';
1365 $this->_info
['file']['Mime'] = 'image/jp2';
1366 $this->_info
['file']['Format'] = 'JP2';
1369 $this->_info
['file']['Mime'] = 'application/octet-stream';
1370 $this->_info
['file']['Format'] = 'JPX';
1373 $this->_info
['file']['Mime'] = 'application/octet-stream';
1374 $this->_info
['file']['Format'] = 'JB2';
1377 $this->_info
['file']['Mime'] = 'application/x-shockwave-flash';
1378 $this->_info
['file']['Format'] = 'SWC';
1381 $this->_info
['file']['Mime'] = 'image/iff';
1382 $this->_info
['file']['Format'] = 'IFF';
1385 $this->_info
['file']['Mime'] = 'image/vnd.wap.wbmp';
1386 $this->_info
['file']['Format'] = 'WBMP';
1389 $this->_info
['file']['Mime'] = 'image/xbm';
1390 $this->_info
['file']['Format'] = 'XBM';
1393 $this->_info
['file']['Mime'] = 'image/unknown';
1397 $this->_info
['file'] = array();
1398 $this->_info
['file']['Name'] = \dokuwiki\Utf8\PhpString
::basename($this->_fileName
);
1399 $this->_info
['file']['Url'] = $this->_fileName
;
1405 /*************************************************************/
1406 function _parseMarkerJFIF() {
1407 if (!isset($this->_markers
)) {
1411 if ($this->_markers
== null) {
1416 $count = count($this->_markers
);
1417 for ($i = 0; $i < $count; $i++
) {
1418 if ($this->_markers
[$i]['marker'] == 0xE0) {
1419 $signature = $this->_getFixedString($this->_markers
[$i]['data'], 0, 4);
1420 if ($signature == 'JFIF') {
1421 $data =& $this->_markers
[$i]['data'];
1427 if ($data == null) {
1428 $this->_info
['jfif'] = false;
1432 $this->_info
['jfif'] = array();
1434 $vmaj = $this->_getByte($data, 5);
1435 $vmin = $this->_getByte($data, 6);
1437 $this->_info
['jfif']['Version'] = sprintf('%d.%02d', $vmaj, $vmin);
1439 $units = $this->_getByte($data, 7);
1442 $this->_info
['jfif']['Units'] = 'pixels';
1445 $this->_info
['jfif']['Units'] = 'dpi';
1448 $this->_info
['jfif']['Units'] = 'dpcm';
1451 $this->_info
['jfif']['Units'] = 'unknown';
1455 $xdens = $this->_getShort($data, 8);
1456 $ydens = $this->_getShort($data, 10);
1458 $this->_info
['jfif']['XDensity'] = $xdens;
1459 $this->_info
['jfif']['YDensity'] = $ydens;
1461 $thumbx = $this->_getByte($data, 12);
1462 $thumby = $this->_getByte($data, 13);
1464 $this->_info
['jfif']['ThumbnailWidth'] = $thumbx;
1465 $this->_info
['jfif']['ThumbnailHeight'] = $thumby;
1470 /*************************************************************/
1471 function _parseMarkerSOF() {
1472 if (!isset($this->_markers
)) {
1476 if ($this->_markers
== null) {
1481 $count = count($this->_markers
);
1482 for ($i = 0; $i < $count; $i++
) {
1483 switch ($this->_markers
[$i]['marker']) {
1488 $data =& $this->_markers
[$i]['data'];
1489 $marker = $this->_markers
[$i]['marker'];
1494 if ($data == null) {
1495 $this->_info
['sof'] = false;
1500 $this->_info
['sof'] = array();
1504 $format = 'Baseline';
1507 $format = 'Progessive';
1510 $format = 'Non-baseline';
1513 $format = 'Arithmetic';
1519 $this->_info
['sof']['Format'] = $format;
1520 $this->_info
['sof']['SamplePrecision'] = $this->_getByte($data, $pos +
0);
1521 $this->_info
['sof']['ImageHeight'] = $this->_getShort($data, $pos +
1);
1522 $this->_info
['sof']['ImageWidth'] = $this->_getShort($data, $pos +
3);
1523 $this->_info
['sof']['ColorChannels'] = $this->_getByte($data, $pos +
5);
1529 * Parses the XMP data
1531 * @author Hakan Sandell <hakan.sandell@mydata.se>
1533 function _parseMarkerXmp() {
1534 if (!isset($this->_markers
)) {
1538 if ($this->_markers
== null) {
1543 $count = count($this->_markers
);
1544 for ($i = 0; $i < $count; $i++
) {
1545 if ($this->_markers
[$i]['marker'] == 0xE1) {
1546 $signature = $this->_getFixedString($this->_markers
[$i]['data'], 0, 29);
1547 if ($signature == "http://ns.adobe.com/xap/1.0/\0") {
1548 $data = substr($this->_markers
[$i]['data'], 29);
1554 if ($data == null) {
1555 $this->_info
['xmp'] = false;
1559 $parser = xml_parser_create();
1560 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING
, 0);
1561 xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE
, 1);
1562 $result = xml_parse_into_struct($parser, $data, $values, $tags);
1563 xml_parser_free($parser);
1566 $this->_info
['xmp'] = false;
1570 $this->_info
['xmp'] = array();
1571 $count = count($values);
1572 for ($i = 0; $i < $count; $i++
) {
1573 if ($values[$i]['tag'] == 'rdf:Description' && $values[$i]['type'] == 'open') {
1575 while ((++
$i < $count) && ($values[$i]['tag'] != 'rdf:Description')) {
1576 $this->_parseXmpNode($values, $i, $this->_info
['xmp'][$values[$i]['tag']], $count);
1584 * Parses XMP nodes by recursion
1586 * @author Hakan Sandell <hakan.sandell@mydata.se>
1588 * @param array $values
1590 * @param mixed $meta
1591 * @param integer $count
1593 function _parseXmpNode($values, &$i, &$meta, $count) {
1594 if ($values[$i]['type'] == 'close') return;
1596 if ($values[$i]['type'] == 'complete') {
1597 // Simple Type property
1598 $meta = $values[$i]['value'];
1603 if ($i >= $count) return;
1605 if ($values[$i]['tag'] == 'rdf:Bag' ||
$values[$i]['tag'] == 'rdf:Seq') {
1608 while ($values[++
$i]['tag'] == 'rdf:li') {
1609 $this->_parseXmpNode($values, $i, $meta[], $count);
1611 $i++
; // skip closing Bag/Seq tag
1613 } elseif ($values[$i]['tag'] == 'rdf:Alt') {
1614 // Language Alternative property, only the first (default) value is used
1615 if ($values[$i]['type'] == 'open') {
1617 $this->_parseXmpNode($values, $i, $meta, $count);
1618 while ((++
$i < $count) && ($values[$i]['tag'] != 'rdf:Alt'));
1619 $i++
; // skip closing Alt tag
1623 // Structure property
1625 $startTag = $values[$i-1]['tag'];
1627 $this->_parseXmpNode($values, $i, $meta[$values[$i]['tag']], $count);
1628 } while ((++
$i < $count) && ($values[$i]['tag'] != $startTag));
1632 /*************************************************************/
1633 function _parseMarkerExif() {
1634 if (!isset($this->_markers
)) {
1638 if ($this->_markers
== null) {
1643 $count = count($this->_markers
);
1644 for ($i = 0; $i < $count; $i++
) {
1645 if ($this->_markers
[$i]['marker'] == 0xE1) {
1646 $signature = $this->_getFixedString($this->_markers
[$i]['data'], 0, 6);
1647 if ($signature == "Exif\0\0") {
1648 $data =& $this->_markers
[$i]['data'];
1654 if ($data == null) {
1655 $this->_info
['exif'] = false;
1659 $this->_info
['exif'] = array();
1661 // We don't increment $pos after this because Exif uses offsets relative to this point
1663 $byteAlign = $this->_getShort($data, $pos +
0);
1665 if ($byteAlign == 0x4949) { // "II"
1666 $isBigEndian = false;
1667 } elseif ($byteAlign == 0x4D4D) { // "MM"
1668 $isBigEndian = true;
1670 return false; // Unexpected data
1673 $alignCheck = $this->_getShort($data, $pos +
2, $isBigEndian);
1674 if ($alignCheck != 0x002A) // That's the expected value
1675 return false; // Unexpected data
1678 $this->_info
['exif']['ByteAlign'] = "Big Endian";
1680 $this->_info
['exif']['ByteAlign'] = "Little Endian";
1683 $offsetIFD0 = $this->_getLong($data, $pos +
4, $isBigEndian);
1684 if ($offsetIFD0 < 8)
1685 return false; // Unexpected data
1687 $offsetIFD1 = $this->_readIFD($data, $pos, $offsetIFD0, $isBigEndian, 'ifd0');
1688 if ($offsetIFD1 != 0)
1689 $this->_readIFD($data, $pos, $offsetIFD1, $isBigEndian, 'ifd1');
1694 /*************************************************************/
1697 * @param mixed $data
1698 * @param integer $base
1699 * @param integer $offset
1700 * @param boolean $isBigEndian
1701 * @param string $mode
1705 function _readIFD($data, $base, $offset, $isBigEndian, $mode) {
1706 $EXIFTags = $this->_exifTagNames($mode);
1708 $numEntries = $this->_getShort($data, $base +
$offset, $isBigEndian);
1711 $exifTIFFOffset = 0;
1712 $exifTIFFLength = 0;
1713 $exifThumbnailOffset = 0;
1714 $exifThumbnailLength = 0;
1716 for ($i = 0; $i < $numEntries; $i++
) {
1717 $tag = $this->_getShort($data, $base +
$offset, $isBigEndian);
1719 $type = $this->_getShort($data, $base +
$offset, $isBigEndian);
1721 $count = $this->_getLong($data, $base +
$offset, $isBigEndian);
1724 if (($type < 1) ||
($type > 12))
1725 return false; // Unexpected Type
1727 $typeLengths = array( -1, 1, 1, 2, 4, 8, 1, 1, 2, 4, 8, 4, 8 );
1729 $dataLength = $typeLengths[$type] * $count;
1730 if ($dataLength > 4) {
1731 $dataOffset = $this->_getLong($data, $base +
$offset, $isBigEndian);
1732 $rawValue = $this->_getFixedString($data, $base +
$dataOffset, $dataLength);
1734 $rawValue = $this->_getFixedString($data, $base +
$offset, $dataLength);
1741 $value = $this->_getByte($rawValue, 0);
1744 for ($j = 0; $j < $count; $j++
)
1745 $value[$j] = $this->_getByte($rawValue, $j);
1753 $value = $this->_getShort($rawValue, 0, $isBigEndian);
1756 for ($j = 0; $j < $count; $j++
)
1757 $value[$j] = $this->_getShort($rawValue, $j * 2, $isBigEndian);
1762 $value = $this->_getLong($rawValue, 0, $isBigEndian);
1765 for ($j = 0; $j < $count; $j++
)
1766 $value[$j] = $this->_getLong($rawValue, $j * 4, $isBigEndian);
1769 case 5: // URATIONAL
1771 $a = $this->_getLong($rawValue, 0, $isBigEndian);
1772 $b = $this->_getLong($rawValue, 4, $isBigEndian);
1777 if (($a != 0) && ($b != 0)) {
1778 $value['val'] = $a / $b;
1782 for ($j = 0; $j < $count; $j++
) {
1783 $a = $this->_getLong($rawValue, $j * 8, $isBigEndian);
1784 $b = $this->_getLong($rawValue, ($j * 8) +
4, $isBigEndian);
1786 $value[$j]['val'] = 0;
1787 $value[$j]['num'] = $a;
1788 $value[$j]['den'] = $b;
1789 if (($a != 0) && ($b != 0))
1790 $value[$j]['val'] = $a / $b;
1796 $value = $this->_getByte($rawValue, 0);
1799 for ($j = 0; $j < $count; $j++
)
1800 $value[$j] = $this->_getByte($rawValue, $j);
1803 case 7: // UNDEFINED
1808 $value = $this->_getShort($rawValue, 0, $isBigEndian);
1811 for ($j = 0; $j < $count; $j++
)
1812 $value[$j] = $this->_getShort($rawValue, $j * 2, $isBigEndian);
1817 $value = $this->_getLong($rawValue, 0, $isBigEndian);
1820 for ($j = 0; $j < $count; $j++
)
1821 $value[$j] = $this->_getLong($rawValue, $j * 4, $isBigEndian);
1824 case 10: // SRATIONAL
1826 $a = $this->_getLong($rawValue, 0, $isBigEndian);
1827 $b = $this->_getLong($rawValue, 4, $isBigEndian);
1832 if (($a != 0) && ($b != 0))
1833 $value['val'] = $a / $b;
1836 for ($j = 0; $j < $count; $j++
) {
1837 $a = $this->_getLong($rawValue, $j * 8, $isBigEndian);
1838 $b = $this->_getLong($rawValue, ($j * 8) +
4, $isBigEndian);
1840 $value[$j]['val'] = 0;
1841 $value[$j]['num'] = $a;
1842 $value[$j]['den'] = $b;
1843 if (($a != 0) && ($b != 0))
1844 $value[$j]['val'] = $a / $b;
1856 return false; // Unexpected Type
1860 if (($mode == 'ifd0') && ($tag == 0x8769)) { // ExifIFDOffset
1861 $this->_readIFD($data, $base, $value, $isBigEndian, 'exif');
1862 } elseif (($mode == 'ifd0') && ($tag == 0x8825)) { // GPSIFDOffset
1863 $this->_readIFD($data, $base, $value, $isBigEndian, 'gps');
1864 } elseif (($mode == 'ifd1') && ($tag == 0x0111)) { // TIFFStripOffsets
1865 $exifTIFFOffset = $value;
1866 } elseif (($mode == 'ifd1') && ($tag == 0x0117)) { // TIFFStripByteCounts
1867 $exifTIFFLength = $value;
1868 } elseif (($mode == 'ifd1') && ($tag == 0x0201)) { // TIFFJFIFOffset
1869 $exifThumbnailOffset = $value;
1870 } elseif (($mode == 'ifd1') && ($tag == 0x0202)) { // TIFFJFIFLength
1871 $exifThumbnailLength = $value;
1872 } elseif (($mode == 'exif') && ($tag == 0xA005)) { // InteropIFDOffset
1873 $this->_readIFD($data, $base, $value, $isBigEndian, 'interop');
1875 // elseif (($mode == 'exif') && ($tag == 0x927C)) { // MakerNote
1878 if (isset($EXIFTags[$tag])) {
1879 $tagName = $EXIFTags[$tag];
1880 if (isset($this->_info
['exif'][$tagName])) {
1881 if (!is_array($this->_info
['exif'][$tagName])) {
1883 $aux[0] = $this->_info
['exif'][$tagName];
1884 $this->_info
['exif'][$tagName] = $aux;
1887 $this->_info
['exif'][$tagName][count($this->_info
['exif'][$tagName])] = $value;
1889 $this->_info
['exif'][$tagName] = $value;
1894 echo sprintf("<h1>Unknown tag %02x (t: %d l: %d) %s in %s</h1>", $tag, $type, $count, $mode, $this->_fileName);
1895 // Unknown Tags will be ignored!!!
1896 // That's because the tag might be a pointer (like the Exif tag)
1897 // and saving it without saving the data it points to might
1898 // create an invalid file.
1904 if (($exifThumbnailOffset > 0) && ($exifThumbnailLength > 0)) {
1905 $this->_info
['exif']['JFIFThumbnail'] = $this->_getFixedString($data, $base +
$exifThumbnailOffset, $exifThumbnailLength);
1908 if (($exifTIFFOffset > 0) && ($exifTIFFLength > 0)) {
1909 $this->_info
['exif']['TIFFStrips'] = $this->_getFixedString($data, $base +
$exifTIFFOffset, $exifTIFFLength);
1912 $nextOffset = $this->_getLong($data, $base +
$offset, $isBigEndian);
1916 /*************************************************************/
1917 function & _createMarkerExif() {
1919 $count = count($this->_markers
);
1920 for ($i = 0; $i < $count; $i++
) {
1921 if ($this->_markers
[$i]['marker'] == 0xE1) {
1922 $signature = $this->_getFixedString($this->_markers
[$i]['data'], 0, 6);
1923 if ($signature == "Exif\0\0") {
1924 $data =& $this->_markers
[$i]['data'];
1930 if (!isset($this->_info
['exif'])) {
1938 if (isset($this->_info
['exif']['ByteAlign']) && ($this->_info
['exif']['ByteAlign'] == "Big Endian")) {
1939 $isBigEndian = true;
1941 $pos = $this->_putString($data, $pos, $aux);
1943 $isBigEndian = false;
1945 $pos = $this->_putString($data, $pos, $aux);
1947 $pos = $this->_putShort($data, $pos, 0x002A, $isBigEndian);
1948 $pos = $this->_putLong($data, $pos, 0x00000008, $isBigEndian); // IFD0 Offset is always 8
1950 $ifd0 =& $this->_getIFDEntries($isBigEndian, 'ifd0');
1951 $ifd1 =& $this->_getIFDEntries($isBigEndian, 'ifd1');
1953 $pos = $this->_writeIFD($data, $pos, $offsetBase, $ifd0, $isBigEndian, true);
1954 $pos = $this->_writeIFD($data, $pos, $offsetBase, $ifd1, $isBigEndian, false);
1959 /*************************************************************/
1962 * @param mixed $data
1963 * @param integer $pos
1964 * @param integer $offsetBase
1965 * @param array $entries
1966 * @param boolean $isBigEndian
1967 * @param boolean $hasNext
1971 function _writeIFD(&$data, $pos, $offsetBase, &$entries, $isBigEndian, $hasNext) {
1973 $tiffDataOffsetPos = -1;
1975 $entryCount = count($entries);
1977 $dataPos = $pos +
2 +
($entryCount * 12) +
4;
1978 $pos = $this->_putShort($data, $pos, $entryCount, $isBigEndian);
1980 for ($i = 0; $i < $entryCount; $i++
) {
1981 $tag = $entries[$i]['tag'];
1982 $type = $entries[$i]['type'];
1984 if ($type == -99) { // SubIFD
1985 $pos = $this->_putShort($data, $pos, $tag, $isBigEndian);
1986 $pos = $this->_putShort($data, $pos, 0x04, $isBigEndian); // LONG
1987 $pos = $this->_putLong($data, $pos, 0x01, $isBigEndian); // Count = 1
1988 $pos = $this->_putLong($data, $pos, $dataPos - $offsetBase, $isBigEndian);
1990 $dataPos = $this->_writeIFD($data, $dataPos, $offsetBase, $entries[$i]['value'], $isBigEndian, false);
1991 } elseif ($type == -98) { // TIFF Data
1992 $pos = $this->_putShort($data, $pos, $tag, $isBigEndian);
1993 $pos = $this->_putShort($data, $pos, 0x04, $isBigEndian); // LONG
1994 $pos = $this->_putLong($data, $pos, 0x01, $isBigEndian); // Count = 1
1995 $tiffDataOffsetPos = $pos;
1996 $pos = $this->_putLong($data, $pos, 0x00, $isBigEndian); // For Now
1997 $tiffData =& $entries[$i]['value'] ;
1998 } else { // Regular Entry
1999 $pos = $this->_putShort($data, $pos, $tag, $isBigEndian);
2000 $pos = $this->_putShort($data, $pos, $type, $isBigEndian);
2001 $pos = $this->_putLong($data, $pos, $entries[$i]['count'], $isBigEndian);
2002 if (strlen($entries[$i]['value']) > 4) {
2003 $pos = $this->_putLong($data, $pos, $dataPos - $offsetBase, $isBigEndian);
2004 $dataPos = $this->_putString($data, $dataPos, $entries[$i]['value']);
2006 $val = str_pad($entries[$i]['value'], 4, "\0");
2007 $pos = $this->_putString($data, $pos, $val);
2012 if ($tiffData != null) {
2013 $this->_putLong($data, $tiffDataOffsetPos, $dataPos - $offsetBase, $isBigEndian);
2014 $dataPos = $this->_putString($data, $dataPos, $tiffData);
2018 $pos = $this->_putLong($data, $pos, $dataPos - $offsetBase, $isBigEndian);
2020 $pos = $this->_putLong($data, $pos, 0, $isBigEndian);
2026 /*************************************************************/
2029 * @param boolean $isBigEndian
2030 * @param string $mode
2034 function & _getIFDEntries($isBigEndian, $mode) {
2035 $EXIFNames = $this->_exifTagNames($mode);
2036 $EXIFTags = $this->_exifNameTags($mode);
2037 $EXIFTypeInfo = $this->_exifTagTypes($mode);
2039 $ifdEntries = array();
2042 foreach($EXIFNames as $tag => $name) {
2043 $type = $EXIFTypeInfo[$tag][0];
2044 $count = $EXIFTypeInfo[$tag][1];
2047 if (($mode == 'ifd0') && ($tag == 0x8769)) { // ExifIFDOffset
2048 if (isset($this->_info
['exif']['EXIFVersion'])) {
2049 $value =& $this->_getIFDEntries($isBigEndian, "exif");
2055 } elseif (($mode == 'ifd0') && ($tag == 0x8825)) { // GPSIFDOffset
2056 if (isset($this->_info
['exif']['GPSVersionID'])) {
2057 $value =& $this->_getIFDEntries($isBigEndian, "gps");
2062 } elseif (($mode == 'ifd1') && ($tag == 0x0111)) { // TIFFStripOffsets
2063 if (isset($this->_info
['exif']['TIFFStrips'])) {
2064 $value =& $this->_info
['exif']['TIFFStrips'];
2069 } elseif (($mode == 'ifd1') && ($tag == 0x0117)) { // TIFFStripByteCounts
2070 if (isset($this->_info
['exif']['TIFFStrips'])) {
2071 $value = strlen($this->_info
['exif']['TIFFStrips']);
2075 } elseif (($mode == 'ifd1') && ($tag == 0x0201)) { // TIFFJFIFOffset
2076 if (isset($this->_info
['exif']['JFIFThumbnail'])) {
2077 $value =& $this->_info
['exif']['JFIFThumbnail'];
2082 } elseif (($mode == 'ifd1') && ($tag == 0x0202)) { // TIFFJFIFLength
2083 if (isset($this->_info
['exif']['JFIFThumbnail'])) {
2084 $value = strlen($this->_info
['exif']['JFIFThumbnail']);
2088 } elseif (($mode == 'exif') && ($tag == 0xA005)) { // InteropIFDOffset
2089 if (isset($this->_info
['exif']['InteroperabilityIndex'])) {
2090 $value =& $this->_getIFDEntries($isBigEndian, "interop");
2095 } elseif (isset($this->_info
['exif'][$name])) {
2096 $origValue =& $this->_info
['exif'][$name];
2098 // This makes it easier to process variable size elements
2099 if (!is_array($origValue) ||
isset($origValue['val'])) {
2100 unset($origValue); // Break the reference
2101 $origValue = array($this->_info
['exif'][$name]);
2103 $origCount = count($origValue);
2105 if ($origCount == 0 ) {
2106 $type = -1; // To ignore this field
2114 $count = $origCount;
2118 while (($j < $count) && ($j < $origCount)) {
2120 $this->_putByte($value, $j, $origValue[$j]);
2124 while ($j < $count) {
2125 $this->_putByte($value, $j, 0);
2130 $v = strval($origValue[0]);
2131 if (($count != 0) && (strlen($v) > $count)) {
2132 $v = substr($v, 0, $count);
2134 elseif (($count > 0) && (strlen($v) < $count)) {
2135 $v = str_pad($v, $count, "\0");
2138 $count = strlen($v);
2140 $this->_putString($value, 0, $v);
2144 $count = $origCount;
2148 while (($j < $count) && ($j < $origCount)) {
2149 $this->_putShort($value, $j * 2, $origValue[$j], $isBigEndian);
2153 while ($j < $count) {
2154 $this->_putShort($value, $j * 2, 0, $isBigEndian);
2160 $count = $origCount;
2164 while (($j < $count) && ($j < $origCount)) {
2165 $this->_putLong($value, $j * 4, $origValue[$j], $isBigEndian);
2169 while ($j < $count) {
2170 $this->_putLong($value, $j * 4, 0, $isBigEndian);
2174 case 5: // URATIONAL
2176 $count = $origCount;
2180 while (($j < $count) && ($j < $origCount)) {
2181 $v = $origValue[$j];
2189 // TODO: Allow other types and convert them
2191 $this->_putLong($value, $j * 8, $a, $isBigEndian);
2192 $this->_putLong($value, ($j * 8) +
4, $b, $isBigEndian);
2196 while ($j < $count) {
2197 $this->_putLong($value, $j * 8, 0, $isBigEndian);
2198 $this->_putLong($value, ($j * 8) +
4, 0, $isBigEndian);
2204 $count = $origCount;
2208 while (($j < $count) && ($j < $origCount)) {
2209 $this->_putByte($value, $j, $origValue[$j]);
2213 while ($j < $count) {
2214 $this->_putByte($value, $j, 0);
2218 case 7: // UNDEFINED
2219 $v = strval($origValue[0]);
2220 if (($count != 0) && (strlen($v) > $count)) {
2221 $v = substr($v, 0, $count);
2223 elseif (($count > 0) && (strlen($v) < $count)) {
2224 $v = str_pad($v, $count, "\0");
2227 $count = strlen($v);
2229 $this->_putString($value, 0, $v);
2233 $count = $origCount;
2237 while (($j < $count) && ($j < $origCount)) {
2238 $this->_putShort($value, $j * 2, $origValue[$j], $isBigEndian);
2242 while ($j < $count) {
2243 $this->_putShort($value, $j * 2, 0, $isBigEndian);
2249 $count = $origCount;
2253 while (($j < $count) && ($j < $origCount)) {
2254 $this->_putLong($value, $j * 4, $origValue[$j], $isBigEndian);
2258 while ($j < $count) {
2259 $this->_putLong($value, $j * 4, 0, $isBigEndian);
2263 case 10: // SRATIONAL
2265 $count = $origCount;
2269 while (($j < $count) && ($j < $origCount)) {
2270 $v = $origValue[$j];
2278 // TODO: Allow other types and convert them
2281 $this->_putLong($value, $j * 8, $a, $isBigEndian);
2282 $this->_putLong($value, ($j * 8) +
4, $b, $isBigEndian);
2286 while ($j < $count) {
2287 $this->_putLong($value, $j * 8, 0, $isBigEndian);
2288 $this->_putLong($value, ($j * 8) +
4, 0, $isBigEndian);
2294 $count = $origCount;
2298 while (($j < $count) && ($j < $origCount)) {
2299 $v = strval($origValue[$j]);
2300 if (strlen($v) > 4) {
2301 $v = substr($v, 0, 4);
2303 elseif (strlen($v) < 4) {
2304 $v = str_pad($v, 4, "\0");
2306 $this->_putString($value, $j * 4, $v);
2310 while ($j < $count) {
2312 $this->_putString($value, $j * 4, $v);
2318 $count = $origCount;
2322 while (($j < $count) && ($j < $origCount)) {
2323 $v = strval($origValue[$j]);
2324 if (strlen($v) > 8) {
2325 $v = substr($v, 0, 8);
2327 elseif (strlen($v) < 8) {
2328 $v = str_pad($v, 8, "\0");
2330 $this->_putString($value, $j * 8, $v);
2334 while ($j < $count) {
2335 $v = "\0\0\0\0\0\0\0\0";
2336 $this->_putString($value, $j * 8, $v);
2346 if ($value != null) {
2347 $ifdEntries[$entryCount] = array();
2348 $ifdEntries[$entryCount]['tag'] = $tag;
2349 $ifdEntries[$entryCount]['type'] = $type;
2350 $ifdEntries[$entryCount]['count'] = $count;
2351 $ifdEntries[$entryCount]['value'] = $value;
2360 /*************************************************************/
2361 function _parseMarkerAdobe() {
2362 if (!isset($this->_markers
)) {
2366 if ($this->_markers
== null) {
2371 $count = count($this->_markers
);
2372 for ($i = 0; $i < $count; $i++
) {
2373 if ($this->_markers
[$i]['marker'] == 0xED) {
2374 $signature = $this->_getFixedString($this->_markers
[$i]['data'], 0, 14);
2375 if ($signature == "Photoshop 3.0\0") {
2376 $data =& $this->_markers
[$i]['data'];
2382 if ($data == null) {
2383 $this->_info
['adobe'] = false;
2384 $this->_info
['iptc'] = false;
2388 $this->_info
['adobe'] = array();
2389 $this->_info
['adobe']['raw'] = array();
2390 $this->_info
['iptc'] = array();
2392 $datasize = strlen($data);
2394 while ($pos < $datasize) {
2395 $signature = $this->_getFixedString($data, $pos, 4);
2396 if ($signature != '8BIM')
2400 $type = $this->_getShort($data, $pos);
2403 $strlen = $this->_getByte($data, $pos);
2406 for ($i = 0; $i < $strlen; $i++
) {
2407 $header .= $data[$pos +
$i];
2409 $pos +
= $strlen +
1 - ($strlen %
2); // The string is padded to even length, counting the length byte itself
2411 $length = $this->_getLong($data, $pos);
2417 case 0x0404: // Caption (IPTC Data)
2418 $pos = $this->_readIPTC($data, $pos);
2422 case 0x040A: // CopyrightFlag
2423 $this->_info
['adobe']['CopyrightFlag'] = $this->_getByte($data, $pos);
2426 case 0x040B: // ImageURL
2427 $this->_info
['adobe']['ImageURL'] = $this->_getFixedString($data, $pos, $length);
2430 case 0x040C: // Thumbnail
2431 $aux = $this->_getLong($data, $pos);
2434 $this->_info
['adobe']['ThumbnailWidth'] = $this->_getLong($data, $pos);
2436 $this->_info
['adobe']['ThumbnailHeight'] = $this->_getLong($data, $pos);
2439 $pos +
= 16; // Skip some data
2441 $this->_info
['adobe']['ThumbnailData'] = $this->_getFixedString($data, $pos, $length - 28);
2442 $pos +
= $length - 28;
2449 // We save all blocks, even those we recognized
2450 $label = sprintf('8BIM_0x%04x', $type);
2451 $this->_info
['adobe']['raw'][$label] = array();
2452 $this->_info
['adobe']['raw'][$label]['type'] = $type;
2453 $this->_info
['adobe']['raw'][$label]['header'] = $header;
2454 $this->_info
['adobe']['raw'][$label]['data'] =& $this->_getFixedString($data, $basePos, $length);
2456 $pos = $basePos +
$length +
($length %
2); // Even padding
2461 /*************************************************************/
2462 function _readIPTC(&$data, $pos = 0) {
2463 $totalLength = strlen($data);
2465 $IPTCTags = $this->_iptcTagNames();
2467 while ($pos < ($totalLength - 5)) {
2468 $signature = $this->_getShort($data, $pos);
2469 if ($signature != 0x1C02)
2473 $type = $this->_getByte($data, $pos);
2475 $length = $this->_getShort($data, $pos);
2481 if (isset($IPTCTags[$type])) {
2482 $label = $IPTCTags[$type];
2484 $label = sprintf('IPTC_0x%02x', $type);
2488 if (isset($this->_info
['iptc'][$label])) {
2489 if (!is_array($this->_info
['iptc'][$label])) {
2491 $aux[0] = $this->_info
['iptc'][$label];
2492 $this->_info
['iptc'][$label] = $aux;
2494 $this->_info
['iptc'][$label][ count($this->_info
['iptc'][$label]) ] = $this->_getFixedString($data, $pos, $length);
2496 $this->_info
['iptc'][$label] = $this->_getFixedString($data, $pos, $length);
2500 $pos = $basePos +
$length; // No padding
2505 /*************************************************************/
2506 function & _createMarkerAdobe() {
2507 if (isset($this->_info
['iptc'])) {
2508 if (!isset($this->_info
['adobe'])) {
2509 $this->_info
['adobe'] = array();
2511 if (!isset($this->_info
['adobe']['raw'])) {
2512 $this->_info
['adobe']['raw'] = array();
2514 if (!isset($this->_info
['adobe']['raw']['8BIM_0x0404'])) {
2515 $this->_info
['adobe']['raw']['8BIM_0x0404'] = array();
2517 $this->_info
['adobe']['raw']['8BIM_0x0404']['type'] = 0x0404;
2518 $this->_info
['adobe']['raw']['8BIM_0x0404']['header'] = "Caption";
2519 $this->_info
['adobe']['raw']['8BIM_0x0404']['data'] =& $this->_writeIPTC();
2522 if (isset($this->_info
['adobe']['raw']) && (count($this->_info
['adobe']['raw']) > 0)) {
2523 $data = "Photoshop 3.0\0";
2526 reset($this->_info
['adobe']['raw']);
2527 foreach ($this->_info
['adobe']['raw'] as $value){
2528 $pos = $this->_write8BIM(
2540 /*************************************************************/
2543 * @param mixed $data
2544 * @param integer $pos
2546 * @param string $type
2547 * @param string $header
2548 * @param mixed $value
2552 function _write8BIM(&$data, $pos, $type, $header, &$value) {
2553 $signature = "8BIM";
2555 $pos = $this->_putString($data, $pos, $signature);
2556 $pos = $this->_putShort($data, $pos, $type);
2558 $len = strlen($header);
2560 $pos = $this->_putByte($data, $pos, $len);
2561 $pos = $this->_putString($data, $pos, $header);
2562 if (($len %
2) == 0) { // Even padding, including the length byte
2563 $pos = $this->_putByte($data, $pos, 0);
2566 $len = strlen($value);
2567 $pos = $this->_putLong($data, $pos, $len);
2568 $pos = $this->_putString($data, $pos, $value);
2569 if (($len %
2) != 0) { // Even padding
2570 $pos = $this->_putByte($data, $pos, 0);
2575 /*************************************************************/
2576 function & _writeIPTC() {
2580 $IPTCNames =& $this->_iptcNameTags();
2582 foreach($this->_info
['iptc'] as $label => $value) {
2583 $value =& $this->_info
['iptc'][$label];
2586 if (isset($IPTCNames[$label])) {
2587 $type = $IPTCNames[$label];
2589 elseif (substr($label, 0, 7) == "IPTC_0x") {
2590 $type = hexdec(substr($label, 7, 2));
2594 if (is_array($value)) {
2595 $vcnt = count($value);
2596 for ($i = 0; $i < $vcnt; $i++
) {
2597 $pos = $this->_writeIPTCEntry($data, $pos, $type, $value[$i]);
2601 $pos = $this->_writeIPTCEntry($data, $pos, $type, $value);
2609 /*************************************************************/
2612 * @param mixed $data
2613 * @param integer $pos
2615 * @param string $type
2616 * @param mixed $value
2620 function _writeIPTCEntry(&$data, $pos, $type, &$value) {
2621 $pos = $this->_putShort($data, $pos, 0x1C02);
2622 $pos = $this->_putByte($data, $pos, $type);
2623 $pos = $this->_putShort($data, $pos, strlen($value));
2624 $pos = $this->_putString($data, $pos, $value);
2629 /*************************************************************/
2630 function _exifTagNames($mode) {
2633 if ($mode == 'ifd0') {
2634 $tags[0x010E] = 'ImageDescription';
2635 $tags[0x010F] = 'Make';
2636 $tags[0x0110] = 'Model';
2637 $tags[0x0112] = 'Orientation';
2638 $tags[0x011A] = 'XResolution';
2639 $tags[0x011B] = 'YResolution';
2640 $tags[0x0128] = 'ResolutionUnit';
2641 $tags[0x0131] = 'Software';
2642 $tags[0x0132] = 'DateTime';
2643 $tags[0x013B] = 'Artist';
2644 $tags[0x013E] = 'WhitePoint';
2645 $tags[0x013F] = 'PrimaryChromaticities';
2646 $tags[0x0211] = 'YCbCrCoefficients';
2647 $tags[0x0212] = 'YCbCrSubSampling';
2648 $tags[0x0213] = 'YCbCrPositioning';
2649 $tags[0x0214] = 'ReferenceBlackWhite';
2650 $tags[0x8298] = 'Copyright';
2651 $tags[0x8769] = 'ExifIFDOffset';
2652 $tags[0x8825] = 'GPSIFDOffset';
2654 if ($mode == 'ifd1') {
2655 $tags[0x00FE] = 'TIFFNewSubfileType';
2656 $tags[0x00FF] = 'TIFFSubfileType';
2657 $tags[0x0100] = 'TIFFImageWidth';
2658 $tags[0x0101] = 'TIFFImageHeight';
2659 $tags[0x0102] = 'TIFFBitsPerSample';
2660 $tags[0x0103] = 'TIFFCompression';
2661 $tags[0x0106] = 'TIFFPhotometricInterpretation';
2662 $tags[0x0107] = 'TIFFThreshholding';
2663 $tags[0x0108] = 'TIFFCellWidth';
2664 $tags[0x0109] = 'TIFFCellLength';
2665 $tags[0x010A] = 'TIFFFillOrder';
2666 $tags[0x010E] = 'TIFFImageDescription';
2667 $tags[0x010F] = 'TIFFMake';
2668 $tags[0x0110] = 'TIFFModel';
2669 $tags[0x0111] = 'TIFFStripOffsets';
2670 $tags[0x0112] = 'TIFFOrientation';
2671 $tags[0x0115] = 'TIFFSamplesPerPixel';
2672 $tags[0x0116] = 'TIFFRowsPerStrip';
2673 $tags[0x0117] = 'TIFFStripByteCounts';
2674 $tags[0x0118] = 'TIFFMinSampleValue';
2675 $tags[0x0119] = 'TIFFMaxSampleValue';
2676 $tags[0x011A] = 'TIFFXResolution';
2677 $tags[0x011B] = 'TIFFYResolution';
2678 $tags[0x011C] = 'TIFFPlanarConfiguration';
2679 $tags[0x0122] = 'TIFFGrayResponseUnit';
2680 $tags[0x0123] = 'TIFFGrayResponseCurve';
2681 $tags[0x0128] = 'TIFFResolutionUnit';
2682 $tags[0x0131] = 'TIFFSoftware';
2683 $tags[0x0132] = 'TIFFDateTime';
2684 $tags[0x013B] = 'TIFFArtist';
2685 $tags[0x013C] = 'TIFFHostComputer';
2686 $tags[0x0140] = 'TIFFColorMap';
2687 $tags[0x0152] = 'TIFFExtraSamples';
2688 $tags[0x0201] = 'TIFFJFIFOffset';
2689 $tags[0x0202] = 'TIFFJFIFLength';
2690 $tags[0x0211] = 'TIFFYCbCrCoefficients';
2691 $tags[0x0212] = 'TIFFYCbCrSubSampling';
2692 $tags[0x0213] = 'TIFFYCbCrPositioning';
2693 $tags[0x0214] = 'TIFFReferenceBlackWhite';
2694 $tags[0x8298] = 'TIFFCopyright';
2695 $tags[0x9286] = 'TIFFUserComment';
2696 } elseif ($mode == 'exif') {
2697 $tags[0x829A] = 'ExposureTime';
2698 $tags[0x829D] = 'FNumber';
2699 $tags[0x8822] = 'ExposureProgram';
2700 $tags[0x8824] = 'SpectralSensitivity';
2701 $tags[0x8827] = 'ISOSpeedRatings';
2702 $tags[0x8828] = 'OECF';
2703 $tags[0x9000] = 'EXIFVersion';
2704 $tags[0x9003] = 'DateTimeOriginal';
2705 $tags[0x9004] = 'DateTimeDigitized';
2706 $tags[0x9101] = 'ComponentsConfiguration';
2707 $tags[0x9102] = 'CompressedBitsPerPixel';
2708 $tags[0x9201] = 'ShutterSpeedValue';
2709 $tags[0x9202] = 'ApertureValue';
2710 $tags[0x9203] = 'BrightnessValue';
2711 $tags[0x9204] = 'ExposureBiasValue';
2712 $tags[0x9205] = 'MaxApertureValue';
2713 $tags[0x9206] = 'SubjectDistance';
2714 $tags[0x9207] = 'MeteringMode';
2715 $tags[0x9208] = 'LightSource';
2716 $tags[0x9209] = 'Flash';
2717 $tags[0x920A] = 'FocalLength';
2718 $tags[0x927C] = 'MakerNote';
2719 $tags[0x9286] = 'UserComment';
2720 $tags[0x9290] = 'SubSecTime';
2721 $tags[0x9291] = 'SubSecTimeOriginal';
2722 $tags[0x9292] = 'SubSecTimeDigitized';
2723 $tags[0xA000] = 'FlashPixVersion';
2724 $tags[0xA001] = 'ColorSpace';
2725 $tags[0xA002] = 'PixelXDimension';
2726 $tags[0xA003] = 'PixelYDimension';
2727 $tags[0xA004] = 'RelatedSoundFile';
2728 $tags[0xA005] = 'InteropIFDOffset';
2729 $tags[0xA20B] = 'FlashEnergy';
2730 $tags[0xA20C] = 'SpatialFrequencyResponse';
2731 $tags[0xA20E] = 'FocalPlaneXResolution';
2732 $tags[0xA20F] = 'FocalPlaneYResolution';
2733 $tags[0xA210] = 'FocalPlaneResolutionUnit';
2734 $tags[0xA214] = 'SubjectLocation';
2735 $tags[0xA215] = 'ExposureIndex';
2736 $tags[0xA217] = 'SensingMethod';
2737 $tags[0xA300] = 'FileSource';
2738 $tags[0xA301] = 'SceneType';
2739 $tags[0xA302] = 'CFAPattern';
2740 } elseif ($mode == 'interop') {
2741 $tags[0x0001] = 'InteroperabilityIndex';
2742 $tags[0x0002] = 'InteroperabilityVersion';
2743 $tags[0x1000] = 'RelatedImageFileFormat';
2744 $tags[0x1001] = 'RelatedImageWidth';
2745 $tags[0x1002] = 'RelatedImageLength';
2746 } elseif ($mode == 'gps') {
2747 $tags[0x0000] = 'GPSVersionID';
2748 $tags[0x0001] = 'GPSLatitudeRef';
2749 $tags[0x0002] = 'GPSLatitude';
2750 $tags[0x0003] = 'GPSLongitudeRef';
2751 $tags[0x0004] = 'GPSLongitude';
2752 $tags[0x0005] = 'GPSAltitudeRef';
2753 $tags[0x0006] = 'GPSAltitude';
2754 $tags[0x0007] = 'GPSTimeStamp';
2755 $tags[0x0008] = 'GPSSatellites';
2756 $tags[0x0009] = 'GPSStatus';
2757 $tags[0x000A] = 'GPSMeasureMode';
2758 $tags[0x000B] = 'GPSDOP';
2759 $tags[0x000C] = 'GPSSpeedRef';
2760 $tags[0x000D] = 'GPSSpeed';
2761 $tags[0x000E] = 'GPSTrackRef';
2762 $tags[0x000F] = 'GPSTrack';
2763 $tags[0x0010] = 'GPSImgDirectionRef';
2764 $tags[0x0011] = 'GPSImgDirection';
2765 $tags[0x0012] = 'GPSMapDatum';
2766 $tags[0x0013] = 'GPSDestLatitudeRef';
2767 $tags[0x0014] = 'GPSDestLatitude';
2768 $tags[0x0015] = 'GPSDestLongitudeRef';
2769 $tags[0x0016] = 'GPSDestLongitude';
2770 $tags[0x0017] = 'GPSDestBearingRef';
2771 $tags[0x0018] = 'GPSDestBearing';
2772 $tags[0x0019] = 'GPSDestDistanceRef';
2773 $tags[0x001A] = 'GPSDestDistance';
2779 /*************************************************************/
2780 function _exifTagTypes($mode) {
2783 if ($mode == 'ifd0') {
2784 $tags[0x010E] = array(2, 0); // ImageDescription -> ASCII, Any
2785 $tags[0x010F] = array(2, 0); // Make -> ASCII, Any
2786 $tags[0x0110] = array(2, 0); // Model -> ASCII, Any
2787 $tags[0x0112] = array(3, 1); // Orientation -> SHORT, 1
2788 $tags[0x011A] = array(5, 1); // XResolution -> RATIONAL, 1
2789 $tags[0x011B] = array(5, 1); // YResolution -> RATIONAL, 1
2790 $tags[0x0128] = array(3, 1); // ResolutionUnit -> SHORT
2791 $tags[0x0131] = array(2, 0); // Software -> ASCII, Any
2792 $tags[0x0132] = array(2, 20); // DateTime -> ASCII, 20
2793 $tags[0x013B] = array(2, 0); // Artist -> ASCII, Any
2794 $tags[0x013E] = array(5, 2); // WhitePoint -> RATIONAL, 2
2795 $tags[0x013F] = array(5, 6); // PrimaryChromaticities -> RATIONAL, 6
2796 $tags[0x0211] = array(5, 3); // YCbCrCoefficients -> RATIONAL, 3
2797 $tags[0x0212] = array(3, 2); // YCbCrSubSampling -> SHORT, 2
2798 $tags[0x0213] = array(3, 1); // YCbCrPositioning -> SHORT, 1
2799 $tags[0x0214] = array(5, 6); // ReferenceBlackWhite -> RATIONAL, 6
2800 $tags[0x8298] = array(2, 0); // Copyright -> ASCII, Any
2801 $tags[0x8769] = array(4, 1); // ExifIFDOffset -> LONG, 1
2802 $tags[0x8825] = array(4, 1); // GPSIFDOffset -> LONG, 1
2804 if ($mode == 'ifd1') {
2805 $tags[0x00FE] = array(4, 1); // TIFFNewSubfileType -> LONG, 1
2806 $tags[0x00FF] = array(3, 1); // TIFFSubfileType -> SHORT, 1
2807 $tags[0x0100] = array(4, 1); // TIFFImageWidth -> LONG (or SHORT), 1
2808 $tags[0x0101] = array(4, 1); // TIFFImageHeight -> LONG (or SHORT), 1
2809 $tags[0x0102] = array(3, 3); // TIFFBitsPerSample -> SHORT, 3
2810 $tags[0x0103] = array(3, 1); // TIFFCompression -> SHORT, 1
2811 $tags[0x0106] = array(3, 1); // TIFFPhotometricInterpretation -> SHORT, 1
2812 $tags[0x0107] = array(3, 1); // TIFFThreshholding -> SHORT, 1
2813 $tags[0x0108] = array(3, 1); // TIFFCellWidth -> SHORT, 1
2814 $tags[0x0109] = array(3, 1); // TIFFCellLength -> SHORT, 1
2815 $tags[0x010A] = array(3, 1); // TIFFFillOrder -> SHORT, 1
2816 $tags[0x010E] = array(2, 0); // TIFFImageDescription -> ASCII, Any
2817 $tags[0x010F] = array(2, 0); // TIFFMake -> ASCII, Any
2818 $tags[0x0110] = array(2, 0); // TIFFModel -> ASCII, Any
2819 $tags[0x0111] = array(4, 0); // TIFFStripOffsets -> LONG (or SHORT), Any (one per strip)
2820 $tags[0x0112] = array(3, 1); // TIFFOrientation -> SHORT, 1
2821 $tags[0x0115] = array(3, 1); // TIFFSamplesPerPixel -> SHORT, 1
2822 $tags[0x0116] = array(4, 1); // TIFFRowsPerStrip -> LONG (or SHORT), 1
2823 $tags[0x0117] = array(4, 0); // TIFFStripByteCounts -> LONG (or SHORT), Any (one per strip)
2824 $tags[0x0118] = array(3, 0); // TIFFMinSampleValue -> SHORT, Any (SamplesPerPixel)
2825 $tags[0x0119] = array(3, 0); // TIFFMaxSampleValue -> SHORT, Any (SamplesPerPixel)
2826 $tags[0x011A] = array(5, 1); // TIFFXResolution -> RATIONAL, 1
2827 $tags[0x011B] = array(5, 1); // TIFFYResolution -> RATIONAL, 1
2828 $tags[0x011C] = array(3, 1); // TIFFPlanarConfiguration -> SHORT, 1
2829 $tags[0x0122] = array(3, 1); // TIFFGrayResponseUnit -> SHORT, 1
2830 $tags[0x0123] = array(3, 0); // TIFFGrayResponseCurve -> SHORT, Any (2^BitsPerSample)
2831 $tags[0x0128] = array(3, 1); // TIFFResolutionUnit -> SHORT, 1
2832 $tags[0x0131] = array(2, 0); // TIFFSoftware -> ASCII, Any
2833 $tags[0x0132] = array(2, 20); // TIFFDateTime -> ASCII, 20
2834 $tags[0x013B] = array(2, 0); // TIFFArtist -> ASCII, Any
2835 $tags[0x013C] = array(2, 0); // TIFFHostComputer -> ASCII, Any
2836 $tags[0x0140] = array(3, 0); // TIFFColorMap -> SHORT, Any (3 * 2^BitsPerSample)
2837 $tags[0x0152] = array(3, 0); // TIFFExtraSamples -> SHORT, Any (SamplesPerPixel - 3)
2838 $tags[0x0201] = array(4, 1); // TIFFJFIFOffset -> LONG, 1
2839 $tags[0x0202] = array(4, 1); // TIFFJFIFLength -> LONG, 1
2840 $tags[0x0211] = array(5, 3); // TIFFYCbCrCoefficients -> RATIONAL, 3
2841 $tags[0x0212] = array(3, 2); // TIFFYCbCrSubSampling -> SHORT, 2
2842 $tags[0x0213] = array(3, 1); // TIFFYCbCrPositioning -> SHORT, 1
2843 $tags[0x0214] = array(5, 6); // TIFFReferenceBlackWhite -> RATIONAL, 6
2844 $tags[0x8298] = array(2, 0); // TIFFCopyright -> ASCII, Any
2845 $tags[0x9286] = array(2, 0); // TIFFUserComment -> ASCII, Any
2846 } elseif ($mode == 'exif') {
2847 $tags[0x829A] = array(5, 1); // ExposureTime -> RATIONAL, 1
2848 $tags[0x829D] = array(5, 1); // FNumber -> RATIONAL, 1
2849 $tags[0x8822] = array(3, 1); // ExposureProgram -> SHORT, 1
2850 $tags[0x8824] = array(2, 0); // SpectralSensitivity -> ASCII, Any
2851 $tags[0x8827] = array(3, 0); // ISOSpeedRatings -> SHORT, Any
2852 $tags[0x8828] = array(7, 0); // OECF -> UNDEFINED, Any
2853 $tags[0x9000] = array(7, 4); // EXIFVersion -> UNDEFINED, 4
2854 $tags[0x9003] = array(2, 20); // DateTimeOriginal -> ASCII, 20
2855 $tags[0x9004] = array(2, 20); // DateTimeDigitized -> ASCII, 20
2856 $tags[0x9101] = array(7, 4); // ComponentsConfiguration -> UNDEFINED, 4
2857 $tags[0x9102] = array(5, 1); // CompressedBitsPerPixel -> RATIONAL, 1
2858 $tags[0x9201] = array(10, 1); // ShutterSpeedValue -> SRATIONAL, 1
2859 $tags[0x9202] = array(5, 1); // ApertureValue -> RATIONAL, 1
2860 $tags[0x9203] = array(10, 1); // BrightnessValue -> SRATIONAL, 1
2861 $tags[0x9204] = array(10, 1); // ExposureBiasValue -> SRATIONAL, 1
2862 $tags[0x9205] = array(5, 1); // MaxApertureValue -> RATIONAL, 1
2863 $tags[0x9206] = array(5, 1); // SubjectDistance -> RATIONAL, 1
2864 $tags[0x9207] = array(3, 1); // MeteringMode -> SHORT, 1
2865 $tags[0x9208] = array(3, 1); // LightSource -> SHORT, 1
2866 $tags[0x9209] = array(3, 1); // Flash -> SHORT, 1
2867 $tags[0x920A] = array(5, 1); // FocalLength -> RATIONAL, 1
2868 $tags[0x927C] = array(7, 0); // MakerNote -> UNDEFINED, Any
2869 $tags[0x9286] = array(7, 0); // UserComment -> UNDEFINED, Any
2870 $tags[0x9290] = array(2, 0); // SubSecTime -> ASCII, Any
2871 $tags[0x9291] = array(2, 0); // SubSecTimeOriginal -> ASCII, Any
2872 $tags[0x9292] = array(2, 0); // SubSecTimeDigitized -> ASCII, Any
2873 $tags[0xA000] = array(7, 4); // FlashPixVersion -> UNDEFINED, 4
2874 $tags[0xA001] = array(3, 1); // ColorSpace -> SHORT, 1
2875 $tags[0xA002] = array(4, 1); // PixelXDimension -> LONG (or SHORT), 1
2876 $tags[0xA003] = array(4, 1); // PixelYDimension -> LONG (or SHORT), 1
2877 $tags[0xA004] = array(2, 13); // RelatedSoundFile -> ASCII, 13
2878 $tags[0xA005] = array(4, 1); // InteropIFDOffset -> LONG, 1
2879 $tags[0xA20B] = array(5, 1); // FlashEnergy -> RATIONAL, 1
2880 $tags[0xA20C] = array(7, 0); // SpatialFrequencyResponse -> UNDEFINED, Any
2881 $tags[0xA20E] = array(5, 1); // FocalPlaneXResolution -> RATIONAL, 1
2882 $tags[0xA20F] = array(5, 1); // FocalPlaneYResolution -> RATIONAL, 1
2883 $tags[0xA210] = array(3, 1); // FocalPlaneResolutionUnit -> SHORT, 1
2884 $tags[0xA214] = array(3, 2); // SubjectLocation -> SHORT, 2
2885 $tags[0xA215] = array(5, 1); // ExposureIndex -> RATIONAL, 1
2886 $tags[0xA217] = array(3, 1); // SensingMethod -> SHORT, 1
2887 $tags[0xA300] = array(7, 1); // FileSource -> UNDEFINED, 1
2888 $tags[0xA301] = array(7, 1); // SceneType -> UNDEFINED, 1
2889 $tags[0xA302] = array(7, 0); // CFAPattern -> UNDEFINED, Any
2890 } elseif ($mode == 'interop') {
2891 $tags[0x0001] = array(2, 0); // InteroperabilityIndex -> ASCII, Any
2892 $tags[0x0002] = array(7, 4); // InteroperabilityVersion -> UNKNOWN, 4
2893 $tags[0x1000] = array(2, 0); // RelatedImageFileFormat -> ASCII, Any
2894 $tags[0x1001] = array(4, 1); // RelatedImageWidth -> LONG (or SHORT), 1
2895 $tags[0x1002] = array(4, 1); // RelatedImageLength -> LONG (or SHORT), 1
2896 } elseif ($mode == 'gps') {
2897 $tags[0x0000] = array(1, 4); // GPSVersionID -> BYTE, 4
2898 $tags[0x0001] = array(2, 2); // GPSLatitudeRef -> ASCII, 2
2899 $tags[0x0002] = array(5, 3); // GPSLatitude -> RATIONAL, 3
2900 $tags[0x0003] = array(2, 2); // GPSLongitudeRef -> ASCII, 2
2901 $tags[0x0004] = array(5, 3); // GPSLongitude -> RATIONAL, 3
2902 $tags[0x0005] = array(2, 2); // GPSAltitudeRef -> ASCII, 2
2903 $tags[0x0006] = array(5, 1); // GPSAltitude -> RATIONAL, 1
2904 $tags[0x0007] = array(5, 3); // GPSTimeStamp -> RATIONAL, 3
2905 $tags[0x0008] = array(2, 0); // GPSSatellites -> ASCII, Any
2906 $tags[0x0009] = array(2, 2); // GPSStatus -> ASCII, 2
2907 $tags[0x000A] = array(2, 2); // GPSMeasureMode -> ASCII, 2
2908 $tags[0x000B] = array(5, 1); // GPSDOP -> RATIONAL, 1
2909 $tags[0x000C] = array(2, 2); // GPSSpeedRef -> ASCII, 2
2910 $tags[0x000D] = array(5, 1); // GPSSpeed -> RATIONAL, 1
2911 $tags[0x000E] = array(2, 2); // GPSTrackRef -> ASCII, 2
2912 $tags[0x000F] = array(5, 1); // GPSTrack -> RATIONAL, 1
2913 $tags[0x0010] = array(2, 2); // GPSImgDirectionRef -> ASCII, 2
2914 $tags[0x0011] = array(5, 1); // GPSImgDirection -> RATIONAL, 1
2915 $tags[0x0012] = array(2, 0); // GPSMapDatum -> ASCII, Any
2916 $tags[0x0013] = array(2, 2); // GPSDestLatitudeRef -> ASCII, 2
2917 $tags[0x0014] = array(5, 3); // GPSDestLatitude -> RATIONAL, 3
2918 $tags[0x0015] = array(2, 2); // GPSDestLongitudeRef -> ASCII, 2
2919 $tags[0x0016] = array(5, 3); // GPSDestLongitude -> RATIONAL, 3
2920 $tags[0x0017] = array(2, 2); // GPSDestBearingRef -> ASCII, 2
2921 $tags[0x0018] = array(5, 1); // GPSDestBearing -> RATIONAL, 1
2922 $tags[0x0019] = array(2, 2); // GPSDestDistanceRef -> ASCII, 2
2923 $tags[0x001A] = array(5, 1); // GPSDestDistance -> RATIONAL, 1
2929 /*************************************************************/
2930 function _exifNameTags($mode) {
2931 $tags = $this->_exifTagNames($mode);
2932 return $this->_names2Tags($tags);
2935 /*************************************************************/
2936 function _iptcTagNames() {
2938 $tags[0x14] = 'SuplementalCategories';
2939 $tags[0x19] = 'Keywords';
2940 $tags[0x78] = 'Caption';
2941 $tags[0x7A] = 'CaptionWriter';
2942 $tags[0x69] = 'Headline';
2943 $tags[0x28] = 'SpecialInstructions';
2944 $tags[0x0F] = 'Category';
2945 $tags[0x50] = 'Byline';
2946 $tags[0x55] = 'BylineTitle';
2947 $tags[0x6E] = 'Credit';
2948 $tags[0x73] = 'Source';
2949 $tags[0x74] = 'CopyrightNotice';
2950 $tags[0x05] = 'ObjectName';
2951 $tags[0x5A] = 'City';
2952 $tags[0x5C] = 'Sublocation';
2953 $tags[0x5F] = 'ProvinceState';
2954 $tags[0x65] = 'CountryName';
2955 $tags[0x67] = 'OriginalTransmissionReference';
2956 $tags[0x37] = 'DateCreated';
2957 $tags[0x0A] = 'CopyrightFlag';
2962 /*************************************************************/
2963 function & _iptcNameTags() {
2964 $tags = $this->_iptcTagNames();
2965 return $this->_names2Tags($tags);
2968 /*************************************************************/
2969 function _names2Tags($tags2Names) {
2970 $names2Tags = array();
2972 foreach($tags2Names as $tag => $name) {
2973 $names2Tags[$name] = $tag;
2979 /*************************************************************/
2983 * @param integer $pos
2987 function _getByte(&$data, $pos) {
2988 return ord($data[$pos]);
2991 /*************************************************************/
2994 * @param mixed $data
2995 * @param integer $pos
3001 function _putByte(&$data, $pos, $val) {
3002 $val = intval($val);
3004 $data[$pos] = chr($val);
3009 /*************************************************************/
3010 function _getShort(&$data, $pos, $bigEndian = true) {
3012 return (ord($data[$pos]) << 8)
3013 +
ord($data[$pos +
1]);
3015 return ord($data[$pos])
3016 +
(ord($data[$pos +
1]) << 8);
3020 /*************************************************************/
3021 function _putShort(&$data, $pos = 0, $val = 0, $bigEndian = true) {
3022 $val = intval($val);
3025 $data[$pos +
0] = chr(($val & 0x0000FF00) >> 8);
3026 $data[$pos +
1] = chr(($val & 0x000000FF) >> 0);
3028 $data[$pos +
0] = chr(($val & 0x00FF) >> 0);
3029 $data[$pos +
1] = chr(($val & 0xFF00) >> 8);
3035 /*************************************************************/
3038 * @param mixed $data
3039 * @param integer $pos
3041 * @param bool $bigEndian
3045 function _getLong(&$data, $pos, $bigEndian = true) {
3047 return (ord($data[$pos]) << 24)
3048 +
(ord($data[$pos +
1]) << 16)
3049 +
(ord($data[$pos +
2]) << 8)
3050 +
ord($data[$pos +
3]);
3052 return ord($data[$pos])
3053 +
(ord($data[$pos +
1]) << 8)
3054 +
(ord($data[$pos +
2]) << 16)
3055 +
(ord($data[$pos +
3]) << 24);
3059 /*************************************************************/
3062 * @param mixed $data
3063 * @param integer $pos
3066 * @param bool $bigEndian
3070 function _putLong(&$data, $pos, $val, $bigEndian = true) {
3071 $val = intval($val);
3074 $data[$pos +
0] = chr(($val & 0xFF000000) >> 24);
3075 $data[$pos +
1] = chr(($val & 0x00FF0000) >> 16);
3076 $data[$pos +
2] = chr(($val & 0x0000FF00) >> 8);
3077 $data[$pos +
3] = chr(($val & 0x000000FF) >> 0);
3079 $data[$pos +
0] = chr(($val & 0x000000FF) >> 0);
3080 $data[$pos +
1] = chr(($val & 0x0000FF00) >> 8);
3081 $data[$pos +
2] = chr(($val & 0x00FF0000) >> 16);
3082 $data[$pos +
3] = chr(($val & 0xFF000000) >> 24);
3088 /*************************************************************/
3089 function & _getNullString(&$data, $pos) {
3091 $max = strlen($data);
3093 while ($pos < $max) {
3094 if (ord($data[$pos]) == 0) {
3097 $str .= $data[$pos];
3105 /*************************************************************/
3106 function & _getFixedString(&$data, $pos, $length = -1) {
3107 if ($length == -1) {
3108 $length = strlen($data) - $pos;
3111 $rv = substr($data, $pos, $length);
3115 /*************************************************************/
3116 function _putString(&$data, $pos, &$str) {
3117 $len = strlen($str);
3118 for ($i = 0; $i < $len; $i++
) {
3119 $data[$pos +
$i] = $str[$i];
3125 /*************************************************************/
3126 function _hexDump(&$data, $start = 0, $length = -1) {
3127 if (($length == -1) ||
(($length +
$start) > strlen($data))) {
3128 $end = strlen($data);
3130 $end = $start +
$length;
3138 while ($start < $end) {
3139 if (($count %
16) == 0) {
3140 echo sprintf('%04d', $count) . ': ';
3143 $c = ord($data[$start]);
3148 if (strlen($aux) == 1)
3163 if (($count %
4) == 0) {
3167 if (($count %
16) == 0) {
3168 echo ': ' . $ascii . "<br>\n";
3174 while (($count %
16) != 0) {
3177 if (($count %
4) == 0) {
3181 echo ': ' . $ascii . "<br>\n";
3187 /*****************************************************************/
3190 /* vim: set expandtab tabstop=4 shiftwidth=4: */