aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/upload/imagesize.php
blob: 3ef258f0a26d82cea6dee34075d5f034b2c36b65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
<?php
/**
 *
 * This file is part of the phpBB Forum Software package.
 *
 * @copyright (c) phpBB Limited <https://www.phpbb.com>
 * @license GNU General Public License, version 2 (GPL-2.0)
 *
 * For full copyright and license information, please see
 * the docs/CREDITS.txt file.
 *
 */

namespace phpbb\upload;

/**
 * This class handles the retrieval of image dimensions
 */
class imagesize
{
	/** @var int 4-byte long size */
	const LONG_SIZE = 4;

	/** @var int 2-byte short size */
	const SHORT_SIZE = 2;

	/** @var string PNG header */
	const PNG_HEADER = "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a";

	/** @var int PNG IHDR offset */
	const PNG_IHDR_OFFSET = 12;

	/** @var string GIF87a header */
	const GIF87A_HEADER = "\x47\x49\x46\x38\x37\x61";

	/** @var string GIF89a header */
	const GIF89A_HEADER = "\x47\x49\x46\x38\x39\x61";

	/** @var int GIF header size */
	const GIF_HEADER_SIZE = 6;

	/** @var int JPG max header size. Headers can be bigger, but we'll abort
	 *			going throught he header after this */
	const JPG_MAX_HEADER_SIZE = 24576;

	/** @var string PSD signature */
	const PSD_SIGNATURE = "8BPS";

	/** @var int PSD header size */
	const PSD_HEADER_SIZE = 22;

	/** @var int PSD dimensions info offset */
	const PSD_DIMENSIONS_OFFSET = 14;

	/** @var int BMP header size needed for retrieving dimensions */
	const BMP_HEADER_SIZE = 26;

	/** @var string BMP signature */
	const BMP_SIGNATURE = "\x42\x4D";

	/** qvar int BMP dimensions offset */
	const BMP_DIMENSIONS_OFFSET = 18;

	/** @var int TIF header size. The header might be larger but the dimensions
	 *			should be in the first 512 bytes */
	const TIF_HEADER_SIZE = 512;

	/** @var int TIF tag for image height */
	const TIF_TAG_IMAGE_HEIGHT = 257;

	/** @var int TIF tag for image width */
	const TIF_TAG_IMAGE_WIDTH = 256;

	/** @var int TIF tag type for short */
	const TIF_TAG_TYPE_SHORT = 3;

	/** @var int TIF IFD entry size */
	const TIF_IFD_ENTRY_SIZE = 12;

	/** @var int IFF header size. Grab more than what should be needed to make
	 * sure we have the necessary data */
	const IFF_HEADER_SIZE = 32;

	/** @var string JPEG 2000 signature */
	const JPEG_2000_SIGNATURE = "\x00\x00\x00\x0C\x6A\x50\x20\x20\x0D\x0A\x87\x0A";

	/** @var array Size info that is returned */
	protected $size = array();

	/** @var string Data retrieved from remote */
	protected $data = '';

	/**
	 * Get image dimensions of supplied image
	 *
	 * @param string $file Path to image that should be checked
	 * @param string $type Mimetype of image
	 * @return array|bool Array with image dimensions if successful, false if not
	 */
	public function get_imagesize($file, $type = '')
	{
		// Reset values
		$this->reset_values();

		// Treat image type as unknown if extension or mime type is unknown
		if (!preg_match('/\.([a-z0-9]+)$/i', $file, $match) && empty($type))
		{
			$this->get_imagesize_unknown_type($file);
		}
		else
		{
			$extension = (isset($match[1])) ? $match[1] : preg_replace('/.+\/([a-z0-9-.]+)$/i', '$1', $type);

			// Reset size info
			$this->size = array();

			switch ($extension)
			{
				case 'png':
					$this->get_png_size($file);
				break;

				case 'gif':
					$this->get_gif_size($file);
				break;

				case 'jpeg':
				case 'jpg':
				case 'jpe':
				case 'jif':
				case 'jfif':
				case 'jfi':
					$this->get_jpeg_size($file);
				break;

				case 'jp2':
				case 'j2k':
				case 'jpf':
				case 'jpg2':
				case 'jpx':
				case 'jpm':
					$this->get_jp2_size($file);
				break;

				case 'psd':
				case 'photoshop':
					$this->get_psd_size($file);
				break;

				case 'bmp':
					$this->get_bmp_size($file);
				break;

				case 'tif':
				case 'tiff':
					// get_tif_size() sets mime type
					$this->get_tif_size($file);
				break;

				case 'wbm':
				case 'wbmp':
				case 'vnd.wap.wbmp':
					$this->get_wbmp_size($file);
				break;

				case 'iff':
				case 'x-iff':
					$this->get_iff_size($file);
				break;

				default:
					return false;
			}
		}

		return sizeof($this->size) > 1 ? $this->size : false;
	}

	/**
	 * Get dimensions of image if type is unknown
	 *
	 * @param string $filename Path to file
	 */
	protected function get_imagesize_unknown_type($filename)
	{
		// Grab the maximum amount of bytes we might need
		$data = $this->get_image($filename, 0, self::JPG_MAX_HEADER_SIZE, false);

		if ($data !== false)
		{
			$class_methods = preg_grep('/get_([a-z0-9]+)_size/i', get_class_methods($this));

			foreach ($class_methods as $method)
			{
				call_user_func_array(array($this, $method), array($filename));

				if (sizeof($this->size) > 1)
				{
					break;
				}
			}
		}
	}

	/**
	 * Reset values to default
	 */
	protected function reset_values()
	{
		$this->size = array();
		$this->data = '';
	}

	/**
	 * Set mime type based on supplied image
	 *
	 * @param int $type Type of image
	 */
	protected function set_image_type($type)
	{
		$this->size['type'] = $type;
	}

	/**
	 * Get image from specified path/source
	 *
	 * @param string $filename Path to image
	 * @param int $offset Offset at which reading of the image should start
	 * @param int $length Maximum length that should be read
	 * @param bool $force_length True if the length needs to be the specified
	 *			length, false if not. Default: true
	 *
	 * @return bool|string Image data or false if result was empty
	 */
	protected function get_image($filename, $offset, $length, $force_length = true)
	{
		if (empty($this->data))
		{
			$this->data = @file_get_contents($filename, null, null, $offset, $length);
		}

		// Force length to expected one. Return false if data length
		// is smaller than expected length
		if ($force_length === true)
		{
			return (strlen($this->data) < $length) ? false : substr($this->data, $offset, $length) ;
		}

		return empty($this->data) ? false : $this->data;
	}

	/**
	 * Get dimensions of PNG image
	 *
	 * @param string $filename Filename of image
	 *
	 * @return array|bool Array with image dimensions if successful, false if not
	 */
	protected function get_png_size($filename)
	{
		// Retrieve image data including the header, the IHDR tag, and the
		// following 2 chunks for the image width and height
		$data = $this->get_image($filename, 0, self::PNG_IHDR_OFFSET + 3 * self::LONG_SIZE);

		// Check if header fits expected format specified by RFC 2083
		if (substr($data, 0, self::PNG_IHDR_OFFSET - self::LONG_SIZE) !== self::PNG_HEADER || substr($data, self::PNG_IHDR_OFFSET, self::LONG_SIZE) !== 'IHDR')
		{
			return;
		}

		$this->size = unpack('Nwidth/Nheight', substr($data, self::PNG_IHDR_OFFSET + self::LONG_SIZE, self::LONG_SIZE * 2));

		$this->set_image_type(IMAGETYPE_PNG);
	}

	/**
	 * Get dimensions of GIF image
	 *
	 * @param string $filename Filename of image
	 *
	 * @return array|bool Array with image dimensions if successful, false if not
	 */
	protected function get_gif_size($filename)
	{
		// Get data needed for reading image dimensions as outlined by GIF87a
		// and GIF89a specifications
		$data = $this->get_image($filename, 0, self::GIF_HEADER_SIZE + self::SHORT_SIZE * 2);

		$type = substr($data, 0, self::GIF_HEADER_SIZE);
		if ($type !== self::GIF87A_HEADER && $type !== self::GIF89A_HEADER)
		{
			return;
		}

		$this->size = unpack('vwidth/vheight', substr($data, self::GIF_HEADER_SIZE, self::SHORT_SIZE * 2));

		$this->set_image_type(IMAGETYPE_GIF);
	}

	/**
	 * Get dimensions of JPG image
	 *
	 * @param string $filename Filename of image
	 *
	 * @return array|bool Array with image dimensions if successful, false if not
	 */
	protected function get_jpeg_size($filename)
	{
		// Do not force the data length
		$data = $this->get_image($filename, 0, self::JPG_MAX_HEADER_SIZE, false);

		// Check if file is jpeg
		if ($data[0] !== "\xFF" || $data[1] !== "\xD8")
		{
			return;
		}

		// Look through file for SOF marker
		for ($i = 2 * self::SHORT_SIZE; $i < strlen($data); $i++)
		{
			if ($data[$i] === "\xFF" && in_array($data[$i+1], array("\xC0", "\xC1", "\xC2", "\xC3", "\xC5", "\xC6", "\xC7", "\xC8", "\xC9", "\xCA", "\xCB", "\xCD", "\xCE", "\xCF")))
			{
				// Extract size info from SOF marker
				list(, $unpacked) = unpack("H*", substr($data, $i + self::SHORT_SIZE, 7));

				// Get width and height from unpacked size info
				$this->size = array(
					'width'		=> hexdec(substr($unpacked, 10, 4)),
					'height'	=> hexdec(substr($unpacked, 6, 4)),
				);

				break;
			}
		}

		$this->set_image_type(IMAGETYPE_JPEG);
	}

	/**
	 * Get dimensions of PSD image
	 *
	 * @param string $filename Filename of image
	 *
	 * @return array|bool Array with image dimensions if successful, false if not
	 */
	protected function get_psd_size($filename)
	{
		$data = $this->get_image($filename, 0, self::PSD_HEADER_SIZE);

		if ($data === false)
		{
			return;
		}

		// Offset for version info is length of header but version is only a
		// 16-bit unsigned value
		$version = unpack('n', substr($data, self::LONG_SIZE, 2));

		// Check if supplied file is a PSD file
		if (substr($data, 0, self::LONG_SIZE) !== self::PSD_SIGNATURE || $version[1] !== 1)
		{
			return;
		}

		$this->size = unpack('Nheight/Nwidth', substr($data, self::PSD_DIMENSIONS_OFFSET, 2 * self::LONG_SIZE));

		$this->set_image_type(IMAGETYPE_PSD);
	}

	/**
	 * Get dimensions of BMP image
	 *
	 * @param string $filename Filename of image
	 *
	 * @return array|bool Array with image dimensions if successful, false if not
	 */
	protected function get_bmp_size($filename)
	{
		$data = $this->get_image($filename, 0, self::BMP_HEADER_SIZE);

		// Check if supplied file is a BMP file
		if (substr($data, 0, 2) !== self::BMP_SIGNATURE)
		{
			return;
		}

		$this->size = unpack('lwidth/lheight', substr($data, self::BMP_DIMENSIONS_OFFSET, 2 * self::LONG_SIZE));

		$this->set_image_type(IMAGETYPE_BMP);
	}

	/**
	 * Get dimensions of TIF/TIFF image
	 *
	 * @param string $filename Filename of image
	 *
	 * @return array|bool Array with image dimensions if successful, false if not
	 */
	protected function get_tif_size($filename)
	{
		// Do not force length of header
		$data = $this->get_image($filename, 0, self::TIF_HEADER_SIZE, false);

		$signature = substr($data, 0, self::SHORT_SIZE);

		if ($signature !== "II" && $signature !== "MM")
		{
			return;
		}

		if ($signature === "II")
		{
			$type_long = 'V';
			$type_short = 'v';
			$this->set_image_type(IMAGETYPE_TIFF_II);
		}
		else
		{
			$type_long = 'N';
			$type_short = 'n';
			$this->set_image_type(IMAGETYPE_TIFF_MM);
		}

		// Get offset of IFD
		list(, $offset) = unpack($type_long, substr($data, self::LONG_SIZE, self::LONG_SIZE));

		// Get size of IFD
		list(, $size_ifd) = unpack($type_short, substr($data, $offset, self::SHORT_SIZE));

		// Skip 2 bytes that define the IFD size
		$offset += self::SHORT_SIZE;

		// Filter through IFD
		for ($i = 0; $i < $size_ifd; $i++)
		{
			// Get IFD tag
			$type = unpack($type_short, substr($data, $offset, self::SHORT_SIZE));

			// Get field type of tag
			$field_type = unpack($type_short . 'type', substr($data, $offset + self::SHORT_SIZE, self::SHORT_SIZE));

			// Get IFD entry
			$ifd_value = substr($data, $offset + 2 * self::LONG_SIZE, self::LONG_SIZE);

			// Get actual dimensions from IFD
			if ($type[1] === self::TIF_TAG_IMAGE_HEIGHT)
			{
				$this->size = array_merge($this->size, ($field_type['type'] === self::TIF_TAG_TYPE_SHORT) ? unpack($type_short . 'height', $ifd_value) : unpack($type_long . 'height', $ifd_value));
			}
			else if ($type[1] === self::TIF_TAG_IMAGE_WIDTH)
			{
				$this->size = array_merge($this->size, ($field_type['type'] === self::TIF_TAG_TYPE_SHORT) ? unpack($type_short .'width', $ifd_value) : unpack($type_long . 'width', $ifd_value));
			}

			$offset += self::TIF_IFD_ENTRY_SIZE;
		}
	}

	/**
	 * Get dimensions of WBMP image
	 *
	 * @param string $filename Filename of image
	 *
	 * @return array|bool Array with image dimensions if successful, false if not
	 */
	protected function get_wbmp_size($filename)
	{
		$data = $this->get_image($filename, 0, self::LONG_SIZE);

		// Check if image is WBMP
		if (ord($data[0]) !== 0 || ord($data[1]) !== 0 || $data === substr(self::JPEG_2000_SIGNATURE, 0, 4))
		{
			return;
		}

		$this->size = unpack('Cwidth/Cheight', substr($data, self::SHORT_SIZE, self::SHORT_SIZE));

		$this->set_image_type(IMAGETYPE_WBMP);
	}

	/**
	 * Get dimensions of IFF image
	 *
	 * @param string $filename Filename of image
	 *
	 * @return array|bool Array with image dimensions if successful, false if not
	 */
	protected function get_iff_size($filename)
	{
		$data = $this->get_image($filename, 0, self::IFF_HEADER_SIZE);

		$signature = substr($data, 0, self::LONG_SIZE );

		// Check if image is IFF
		if ($signature !== 'FORM' && $signature !== 'FOR4')
		{
			return;
		}

		// Amiga version of IFF
		if ($signature === 'FORM')
		{
			$btmhd_position = strpos($data, 'BMHD');
			$this->size = unpack('nwidth/nheight', substr($data, $btmhd_position + 2 * self::LONG_SIZE, self::LONG_SIZE));
		}
		// Maya version
		else
		{
			$btmhd_position = strpos($data, 'BHD');
			$this->size = unpack('Nwidth/Nheight', substr($data, $btmhd_position + 2 * self::LONG_SIZE - 1, self::LONG_SIZE * 2));
		}

		$this->set_image_type(IMAGETYPE_IFF);
	}

	/**
	 * Get dimensions of JPEG 2000 image
	 *
	 * @param string $filename Filename of image
	 *
	 * @return array|bool Array with image dimensions if successful, false if not
	 */
	protected function get_jp2_size($filename)
	{
		$data = $this->get_image($filename, 0, self::JPG_MAX_HEADER_SIZE, false);

		// Check if file is jpeg 2000
		if (substr($data, 0, strlen(self::JPEG_2000_SIGNATURE)) !== self::JPEG_2000_SIGNATURE)
		{
			return;
		}

		// Get SOC position before starting to search for SIZ
		$soc_position = strpos($data, "\xFF\x4F");

		// Make sure we do not get SIZ before SOC
		$data = substr($data, $soc_position);

		$siz_position = strpos($data, "\xFF\x51");

		// Remove SIZ and everything before
		$data = substr($data, $siz_position + self::SHORT_SIZE);

		// Acquire size info from data
		$this->size = unpack('Nwidth/Nheight', substr($data, self::LONG_SIZE, self::LONG_SIZE * 2));

		$this->set_image_type(IMAGETYPE_JPEG2000);
	}
}