You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

3267 lines
114 KiB

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