module.graphic.tiff.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /////////////////////////////////////////////////////////////////
  3. /// getID3() by James Heinrich <info@getid3.org> //
  4. // available at http://getid3.sourceforge.net //
  5. // or http://www.getid3.org //
  6. /////////////////////////////////////////////////////////////////
  7. // See readme.txt for more details //
  8. /////////////////////////////////////////////////////////////////
  9. // //
  10. // module.archive.tiff.php //
  11. // module for analyzing TIFF files //
  12. // dependencies: NONE //
  13. // ///
  14. /////////////////////////////////////////////////////////////////
  15. class getid3_tiff extends getid3_handler
  16. {
  17. function Analyze() {
  18. $info = &$this->getid3->info;
  19. fseek($this->getid3->fp, $info['avdataoffset'], SEEK_SET);
  20. $TIFFheader = fread($this->getid3->fp, 4);
  21. switch (substr($TIFFheader, 0, 2)) {
  22. case 'II':
  23. $info['tiff']['byte_order'] = 'Intel';
  24. break;
  25. case 'MM':
  26. $info['tiff']['byte_order'] = 'Motorola';
  27. break;
  28. default:
  29. $info['error'][] = 'Invalid TIFF byte order identifier ('.substr($TIFFheader, 0, 2).') at offset '.$info['avdataoffset'];
  30. return false;
  31. break;
  32. }
  33. $info['fileformat'] = 'tiff';
  34. $info['video']['dataformat'] = 'tiff';
  35. $info['video']['lossless'] = true;
  36. $info['tiff']['ifd'] = array();
  37. $CurrentIFD = array();
  38. $FieldTypeByteLength = array(1=>1, 2=>1, 3=>2, 4=>4, 5=>8);
  39. $nextIFDoffset = $this->TIFFendian2Int(fread($this->getid3->fp, 4), $info['tiff']['byte_order']);
  40. while ($nextIFDoffset > 0) {
  41. $CurrentIFD['offset'] = $nextIFDoffset;
  42. fseek($this->getid3->fp, $info['avdataoffset'] + $nextIFDoffset, SEEK_SET);
  43. $CurrentIFD['fieldcount'] = $this->TIFFendian2Int(fread($this->getid3->fp, 2), $info['tiff']['byte_order']);
  44. for ($i = 0; $i < $CurrentIFD['fieldcount']; $i++) {
  45. $CurrentIFD['fields'][$i]['raw']['tag'] = $this->TIFFendian2Int(fread($this->getid3->fp, 2), $info['tiff']['byte_order']);
  46. $CurrentIFD['fields'][$i]['raw']['type'] = $this->TIFFendian2Int(fread($this->getid3->fp, 2), $info['tiff']['byte_order']);
  47. $CurrentIFD['fields'][$i]['raw']['length'] = $this->TIFFendian2Int(fread($this->getid3->fp, 4), $info['tiff']['byte_order']);
  48. $CurrentIFD['fields'][$i]['raw']['offset'] = fread($this->getid3->fp, 4);
  49. switch ($CurrentIFD['fields'][$i]['raw']['type']) {
  50. case 1: // BYTE An 8-bit unsigned integer.
  51. if ($CurrentIFD['fields'][$i]['raw']['length'] <= 4) {
  52. $CurrentIFD['fields'][$i]['value'] = $this->TIFFendian2Int(substr($CurrentIFD['fields'][$i]['raw']['offset'], 0, 1), $info['tiff']['byte_order']);
  53. } else {
  54. $CurrentIFD['fields'][$i]['offset'] = $this->TIFFendian2Int($CurrentIFD['fields'][$i]['raw']['offset'], $info['tiff']['byte_order']);
  55. }
  56. break;
  57. case 2: // ASCII 8-bit bytes that store ASCII codes; the last byte must be null.
  58. if ($CurrentIFD['fields'][$i]['raw']['length'] <= 4) {
  59. $CurrentIFD['fields'][$i]['value'] = substr($CurrentIFD['fields'][$i]['raw']['offset'], 3);
  60. } else {
  61. $CurrentIFD['fields'][$i]['offset'] = $this->TIFFendian2Int($CurrentIFD['fields'][$i]['raw']['offset'], $info['tiff']['byte_order']);
  62. }
  63. break;
  64. case 3: // SHORT A 16-bit (2-byte) unsigned integer.
  65. if ($CurrentIFD['fields'][$i]['raw']['length'] <= 2) {
  66. $CurrentIFD['fields'][$i]['value'] = $this->TIFFendian2Int(substr($CurrentIFD['fields'][$i]['raw']['offset'], 0, 2), $info['tiff']['byte_order']);
  67. } else {
  68. $CurrentIFD['fields'][$i]['offset'] = $this->TIFFendian2Int($CurrentIFD['fields'][$i]['raw']['offset'], $info['tiff']['byte_order']);
  69. }
  70. break;
  71. case 4: // LONG A 32-bit (4-byte) unsigned integer.
  72. if ($CurrentIFD['fields'][$i]['raw']['length'] <= 1) {
  73. $CurrentIFD['fields'][$i]['value'] = $this->TIFFendian2Int($CurrentIFD['fields'][$i]['raw']['offset'], $info['tiff']['byte_order']);
  74. } else {
  75. $CurrentIFD['fields'][$i]['offset'] = $this->TIFFendian2Int($CurrentIFD['fields'][$i]['raw']['offset'], $info['tiff']['byte_order']);
  76. }
  77. break;
  78. case 5: // RATIONAL Two LONG_s: the first represents the numerator of a fraction, the second the denominator.
  79. break;
  80. }
  81. }
  82. $info['tiff']['ifd'][] = $CurrentIFD;
  83. $CurrentIFD = array();
  84. $nextIFDoffset = $this->TIFFendian2Int(fread($this->getid3->fp, 4), $info['tiff']['byte_order']);
  85. }
  86. foreach ($info['tiff']['ifd'] as $IFDid => $IFDarray) {
  87. foreach ($IFDarray['fields'] as $key => $fieldarray) {
  88. switch ($fieldarray['raw']['tag']) {
  89. case 256: // ImageWidth
  90. case 257: // ImageLength
  91. case 258: // BitsPerSample
  92. case 259: // Compression
  93. if (!isset($fieldarray['value'])) {
  94. fseek($this->getid3->fp, $fieldarray['offset'], SEEK_SET);
  95. $info['tiff']['ifd'][$IFDid]['fields'][$key]['raw']['data'] = fread($this->getid3->fp, $fieldarray['raw']['length'] * $FieldTypeByteLength[$fieldarray['raw']['type']]);
  96. }
  97. break;
  98. case 270: // ImageDescription
  99. case 271: // Make
  100. case 272: // Model
  101. case 305: // Software
  102. case 306: // DateTime
  103. case 315: // Artist
  104. case 316: // HostComputer
  105. if (isset($fieldarray['value'])) {
  106. $info['tiff']['ifd'][$IFDid]['fields'][$key]['raw']['data'] = $fieldarray['value'];
  107. } else {
  108. fseek($this->getid3->fp, $fieldarray['offset'], SEEK_SET);
  109. $info['tiff']['ifd'][$IFDid]['fields'][$key]['raw']['data'] = fread($this->getid3->fp, $fieldarray['raw']['length'] * $FieldTypeByteLength[$fieldarray['raw']['type']]);
  110. }
  111. break;
  112. }
  113. switch ($fieldarray['raw']['tag']) {
  114. case 256: // ImageWidth
  115. $info['video']['resolution_x'] = $fieldarray['value'];
  116. break;
  117. case 257: // ImageLength
  118. $info['video']['resolution_y'] = $fieldarray['value'];
  119. break;
  120. case 258: // BitsPerSample
  121. if (isset($fieldarray['value'])) {
  122. $info['video']['bits_per_sample'] = $fieldarray['value'];
  123. } else {
  124. $info['video']['bits_per_sample'] = 0;
  125. for ($i = 0; $i < $fieldarray['raw']['length']; $i++) {
  126. $info['video']['bits_per_sample'] += $this->TIFFendian2Int(substr($info['tiff']['ifd'][$IFDid]['fields'][$key]['raw']['data'], $i * $FieldTypeByteLength[$fieldarray['raw']['type']], $FieldTypeByteLength[$fieldarray['raw']['type']]), $info['tiff']['byte_order']);
  127. }
  128. }
  129. break;
  130. case 259: // Compression
  131. $info['video']['codec'] = $this->TIFFcompressionMethod($fieldarray['value']);
  132. break;
  133. case 270: // ImageDescription
  134. case 271: // Make
  135. case 272: // Model
  136. case 305: // Software
  137. case 306: // DateTime
  138. case 315: // Artist
  139. case 316: // HostComputer
  140. $TIFFcommentName = $this->TIFFcommentName($fieldarray['raw']['tag']);
  141. if (isset($info['tiff']['comments'][$TIFFcommentName])) {
  142. $info['tiff']['comments'][$TIFFcommentName][] = $info['tiff']['ifd'][$IFDid]['fields'][$key]['raw']['data'];
  143. } else {
  144. $info['tiff']['comments'][$TIFFcommentName] = array($info['tiff']['ifd'][$IFDid]['fields'][$key]['raw']['data']);
  145. }
  146. break;
  147. default:
  148. break;
  149. }
  150. }
  151. }
  152. return true;
  153. }
  154. function TIFFendian2Int($bytestring, $byteorder) {
  155. if ($byteorder == 'Intel') {
  156. return getid3_lib::LittleEndian2Int($bytestring);
  157. } elseif ($byteorder == 'Motorola') {
  158. return getid3_lib::BigEndian2Int($bytestring);
  159. }
  160. return false;
  161. }
  162. function TIFFcompressionMethod($id) {
  163. static $TIFFcompressionMethod = array();
  164. if (empty($TIFFcompressionMethod)) {
  165. $TIFFcompressionMethod = array(
  166. 1 => 'Uncompressed',
  167. 2 => 'Huffman',
  168. 3 => 'Fax - CCITT 3',
  169. 5 => 'LZW',
  170. 32773 => 'PackBits',
  171. );
  172. }
  173. return (isset($TIFFcompressionMethod[$id]) ? $TIFFcompressionMethod[$id] : 'unknown/invalid ('.$id.')');
  174. }
  175. function TIFFcommentName($id) {
  176. static $TIFFcommentName = array();
  177. if (empty($TIFFcommentName)) {
  178. $TIFFcommentName = array(
  179. 270 => 'imagedescription',
  180. 271 => 'make',
  181. 272 => 'model',
  182. 305 => 'software',
  183. 306 => 'datetime',
  184. 315 => 'artist',
  185. 316 => 'hostcomputer',
  186. );
  187. }
  188. return (isset($TIFFcommentName[$id]) ? $TIFFcommentName[$id] : 'unknown/invalid ('.$id.')');
  189. }
  190. }
  191. ?>