ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
Tiff.php
Go to the documentation of this file.
1 <?php
2 
3 namespace GetId3\Module\Graphic;
4 
7 
10 // available at http://getid3.sourceforge.net //
11 // or http://www.getid3.org //
13 // See readme.txt for more details //
15 // //
16 // module.archive.tiff.php //
17 // module for analyzing TIFF files //
18 // dependencies: NONE //
19 // ///
21 
29 class Tiff extends BaseHandler
30 {
31 
36  public function analyze()
37  {
38  $info = &$this->getid3->info;
39 
40  fseek($this->getid3->fp, $info['avdataoffset'], SEEK_SET);
41  $TIFFheader = fread($this->getid3->fp, 4);
42 
43  switch (substr($TIFFheader, 0, 2)) {
44  case 'II':
45  $info['tiff']['byte_order'] = 'Intel';
46  break;
47  case 'MM':
48  $info['tiff']['byte_order'] = 'Motorola';
49  break;
50  default:
51  $info['error'][] = 'Invalid TIFF byte order identifier ('.substr($TIFFheader, 0, 2).') at offset '.$info['avdataoffset'];
52 
53  return false;
54  break;
55  }
56 
57  $info['fileformat'] = 'tiff';
58  $info['video']['dataformat'] = 'tiff';
59  $info['video']['lossless'] = true;
60  $info['tiff']['ifd'] = array();
61  $CurrentIFD = array();
62 
63  $FieldTypeByteLength = array(1=>1, 2=>1, 3=>2, 4=>4, 5=>8);
64 
65  $nextIFDoffset = $this->TIFFendian2Int(fread($this->getid3->fp, 4), $info['tiff']['byte_order']);
66 
67  while ($nextIFDoffset > 0) {
68 
69  $CurrentIFD['offset'] = $nextIFDoffset;
70 
71  fseek($this->getid3->fp, $info['avdataoffset'] + $nextIFDoffset, SEEK_SET);
72  $CurrentIFD['fieldcount'] = $this->TIFFendian2Int(fread($this->getid3->fp, 2), $info['tiff']['byte_order']);
73 
74  for ($i = 0; $i < $CurrentIFD['fieldcount']; $i++) {
75  $CurrentIFD['fields'][$i]['raw']['tag'] = $this->TIFFendian2Int(fread($this->getid3->fp, 2), $info['tiff']['byte_order']);
76  $CurrentIFD['fields'][$i]['raw']['type'] = $this->TIFFendian2Int(fread($this->getid3->fp, 2), $info['tiff']['byte_order']);
77  $CurrentIFD['fields'][$i]['raw']['length'] = $this->TIFFendian2Int(fread($this->getid3->fp, 4), $info['tiff']['byte_order']);
78  $CurrentIFD['fields'][$i]['raw']['offset'] = fread($this->getid3->fp, 4);
79 
80  switch ($CurrentIFD['fields'][$i]['raw']['type']) {
81  case 1: // BYTE An 8-bit unsigned integer.
82  if ($CurrentIFD['fields'][$i]['raw']['length'] <= 4) {
83  $CurrentIFD['fields'][$i]['value'] = $this->TIFFendian2Int(substr($CurrentIFD['fields'][$i]['raw']['offset'], 0, 1), $info['tiff']['byte_order']);
84  } else {
85  $CurrentIFD['fields'][$i]['offset'] = $this->TIFFendian2Int($CurrentIFD['fields'][$i]['raw']['offset'], $info['tiff']['byte_order']);
86  }
87  break;
88 
89  case 2: // ASCII 8-bit bytes that store ASCII codes; the last byte must be null.
90  if ($CurrentIFD['fields'][$i]['raw']['length'] <= 4) {
91  $CurrentIFD['fields'][$i]['value'] = substr($CurrentIFD['fields'][$i]['raw']['offset'], 3);
92  } else {
93  $CurrentIFD['fields'][$i]['offset'] = $this->TIFFendian2Int($CurrentIFD['fields'][$i]['raw']['offset'], $info['tiff']['byte_order']);
94  }
95  break;
96 
97  case 3: // SHORT A 16-bit (2-byte) unsigned integer.
98  if ($CurrentIFD['fields'][$i]['raw']['length'] <= 2) {
99  $CurrentIFD['fields'][$i]['value'] = $this->TIFFendian2Int(substr($CurrentIFD['fields'][$i]['raw']['offset'], 0, 2), $info['tiff']['byte_order']);
100  } else {
101  $CurrentIFD['fields'][$i]['offset'] = $this->TIFFendian2Int($CurrentIFD['fields'][$i]['raw']['offset'], $info['tiff']['byte_order']);
102  }
103  break;
104 
105  case 4: // LONG A 32-bit (4-byte) unsigned integer.
106  if ($CurrentIFD['fields'][$i]['raw']['length'] <= 1) {
107  $CurrentIFD['fields'][$i]['value'] = $this->TIFFendian2Int($CurrentIFD['fields'][$i]['raw']['offset'], $info['tiff']['byte_order']);
108  } else {
109  $CurrentIFD['fields'][$i]['offset'] = $this->TIFFendian2Int($CurrentIFD['fields'][$i]['raw']['offset'], $info['tiff']['byte_order']);
110  }
111  break;
112 
113  case 5: // RATIONAL Two LONG_s: the first represents the numerator of a fraction, the second the denominator.
114  break;
115  }
116  }
117 
118  $info['tiff']['ifd'][] = $CurrentIFD;
119  $CurrentIFD = array();
120  $nextIFDoffset = $this->TIFFendian2Int(fread($this->getid3->fp, 4), $info['tiff']['byte_order']);
121 
122  }
123 
124  foreach ($info['tiff']['ifd'] as $IFDid => $IFDarray) {
125  foreach ($IFDarray['fields'] as $key => $fieldarray) {
126  switch ($fieldarray['raw']['tag']) {
127  case 256: // ImageWidth
128  case 257: // ImageLength
129  case 258: // BitsPerSample
130  case 259: // Compression
131  if (!isset($fieldarray['value'])) {
132  fseek($this->getid3->fp, $fieldarray['offset'], SEEK_SET);
133  $info['tiff']['ifd'][$IFDid]['fields'][$key]['raw']['data'] = fread($this->getid3->fp, $fieldarray['raw']['length'] * $FieldTypeByteLength[$fieldarray['raw']['type']]);
134 
135  }
136  break;
137 
138  case 270: // ImageDescription
139  case 271: // Make
140  case 272: // Model
141  case 305: // Software
142  case 306: // DateTime
143  case 315: // Artist
144  case 316: // HostComputer
145  if (isset($fieldarray['value'])) {
146  $info['tiff']['ifd'][$IFDid]['fields'][$key]['raw']['data'] = $fieldarray['value'];
147  } else {
148  fseek($this->getid3->fp, $fieldarray['offset'], SEEK_SET);
149  $info['tiff']['ifd'][$IFDid]['fields'][$key]['raw']['data'] = fread($this->getid3->fp, $fieldarray['raw']['length'] * $FieldTypeByteLength[$fieldarray['raw']['type']]);
150 
151  }
152  break;
153  }
154  switch ($fieldarray['raw']['tag']) {
155  case 256: // ImageWidth
156  $info['video']['resolution_x'] = $fieldarray['value'];
157  break;
158 
159  case 257: // ImageLength
160  $info['video']['resolution_y'] = $fieldarray['value'];
161  break;
162 
163  case 258: // BitsPerSample
164  if (isset($fieldarray['value'])) {
165  $info['video']['bits_per_sample'] = $fieldarray['value'];
166  } else {
167  $info['video']['bits_per_sample'] = 0;
168  for ($i = 0; $i < $fieldarray['raw']['length']; $i++) {
169  $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']);
170  }
171  }
172  break;
173 
174  case 259: // Compression
175  $info['video']['codec'] = $this->TIFFcompressionMethod($fieldarray['value']);
176  break;
177 
178  case 270: // ImageDescription
179  case 271: // Make
180  case 272: // Model
181  case 305: // Software
182  case 306: // DateTime
183  case 315: // Artist
184  case 316: // HostComputer
185  $TIFFcommentName = $this->TIFFcommentName($fieldarray['raw']['tag']);
186  if (isset($info['tiff']['comments'][$TIFFcommentName])) {
187  $info['tiff']['comments'][$TIFFcommentName][] = $info['tiff']['ifd'][$IFDid]['fields'][$key]['raw']['data'];
188  } else {
189  $info['tiff']['comments'][$TIFFcommentName] = array($info['tiff']['ifd'][$IFDid]['fields'][$key]['raw']['data']);
190  }
191  break;
192 
193  default:
194  break;
195  }
196  }
197  }
198 
199  return true;
200  }
201 
208  public function TIFFendian2Int($bytestring, $byteorder)
209  {
210  if ($byteorder == 'Intel') {
211  return Helper::LittleEndian2Int($bytestring);
212  } elseif ($byteorder == 'Motorola') {
213  return Helper::BigEndian2Int($bytestring);
214  }
215 
216  return false;
217  }
218 
225  public function TIFFcompressionMethod($id)
226  {
227  static $TIFFcompressionMethod = array();
228  if (empty($TIFFcompressionMethod)) {
229  $TIFFcompressionMethod = array(
230  1 => 'Uncompressed',
231  2 => 'Huffman',
232  3 => 'Fax - CCITT 3',
233  5 => 'LZW',
234  32773 => 'PackBits',
235  );
236  }
237 
238  return (isset($TIFFcompressionMethod[$id]) ? $TIFFcompressionMethod[$id] : 'unknown/invalid ('.$id.')');
239  }
240 
247  public function TIFFcommentName($id)
248  {
249  static $TIFFcommentName = array();
250  if (empty($TIFFcommentName)) {
251  $TIFFcommentName = array(
252  270 => 'imagedescription',
253  271 => 'make',
254  272 => 'model',
255  305 => 'software',
256  306 => 'datetime',
257  315 => 'artist',
258  316 => 'hostcomputer',
259  );
260  }
261 
262  return (isset($TIFFcommentName[$id]) ? $TIFFcommentName[$id] : 'unknown/invalid ('.$id.')');
263  }
264 
265 }
GetId3() by James Heinrich info@getid3.org //.
Definition: BaseHandler.php:25
GetId3() by James Heinrich info@getid3.org //.
Definition: Tiff.php:29
TIFFendian2Int($bytestring, $byteorder)
Definition: Tiff.php:208
TIFFcommentName($id)
array $TIFFcommentName
Definition: Tiff.php:247
fseek($bytes, $whence=SEEK_SET)
$info
Definition: example_052.php:80
TIFFcompressionMethod($id)
array $TIFFcompressionMethod
Definition: Tiff.php:225
Create styles array
The data for the language used.
static LittleEndian2Int($byteword, $signed=false)
Definition: Helper.php:413
static BigEndian2Int($byteword, $synchsafe=false, $signed=false)
Definition: Helper.php:374