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