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