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