Completed
Push — ticket/25 ( 26e02b...aba345 )
by Marc
02:00
created

FastImageSize::getSize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 0
crap 2
1
<?php
2
3
/**
4
 * fast-image-size base class
5
 * @package fast-image-size
6
 * @copyright (c) Marc Alexander <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FastImageSize;
13
14
class FastImageSize
15
{
16
	/** @var StreamReader */
17
	protected $streamReader;
18
19
	/** @var array Size info that is returned */
20
	protected $size = array();
21
22
	/** @var array List of supported image types and associated image types */
23
	protected $supportedTypes = array(
24
		'png'	=> array('png'),
25
		'gif'	=> array('gif'),
26
		'jpeg'	=> array(
27
				'jpeg',
28
				'jpg',
29
				'jpe',
30
				'jif',
31
				'jfif',
32
				'jfi',
33
			),
34
		'jp2'	=> array(
35
				'jp2',
36
				'j2k',
37
				'jpf',
38
				'jpg2',
39
				'jpx',
40
				'jpm',
41
			),
42
		'psd'	=> array(
43
				'psd',
44
				'photoshop',
45
			),
46
		'bmp'	=> array('bmp'),
47
		'tif'	=> array(
48
				'tif',
49
				'tiff',
50
			),
51
		'wbmp'	=> array(
52
				'wbm',
53
				'wbmp',
54
				'vnd.wap.wbmp',
55
			),
56
		'iff'	=> array(
57
				'iff',
58
				'x-iff',
59
		),
60
		'ico'	=> array(
61
				'ico',
62
				'vnd.microsoft.icon',
63
				'x-icon',
64
				'icon',
65
		),
66
	);
67
68
	/** @var array Class map that links image extensions/mime types to class */
69
	protected $classMap;
70
71
	/** @var array An array containing the classes of supported image types */
72
	protected $type;
73
74
	/**
75
	 * Constructor for fastImageSize class
76
	 */
77 72
	public function __construct()
78
	{
79 72
		$this->streamReader = new StreamReader();
80
81 72
		foreach ($this->supportedTypes as $imageType => $extension)
82
		{
83 72
			$className = '\FastImageSize\Type\Type' . mb_convert_case(mb_strtolower($imageType), MB_CASE_TITLE);
84 72
			$this->type[$imageType] = new $className($this, $this->streamReader);
85
86
			// Create class map
87 72
			foreach ($extension as $ext)
88
			{
89
				/** @var Type\TypeInterface */
90 72
				$this->classMap[$ext] = $this->type[$imageType];
91 72
			}
92 72
		}
93 72
	}
94
95
	/**
96
	 * Get size array
97
	 *
98
	 * @return array|bool Size array if size could be evaluated, false if not
99
	 */
100 72
	protected function getSize()
101
	{
102 72
		return sizeof($this->size) > 1 ? $this->size : false;
103
	}
104
105
	/**
106
	 * Get image dimensions of supplied image
107
	 *
108
	 * @param string $file Path to image that should be checked
109
	 * @param string $type Mimetype of image
110
	 * @return array|bool Array with image dimensions if successful, false if not
111
	 */
112 72
	public function getImageSize($file, $type = '')
113
	{
114
		// Reset values
115 72
		$this->resetValues();
116
117
		// Treat image type as unknown if extension or mime type is unknown
118 72
		if (!preg_match('/\.([a-z0-9]+)$/i', $file, $match) && empty($type))
119 72
		{
120 18
			$this->getImagesizeUnknownType($file);
121 18
		}
122
		else
123
		{
124 54
			$extension = (isset($match[1])) ? $match[1] : preg_replace('/.+\/([a-z0-9-.]+)$/i', '$1', $type);
125
126 54
			$this->getImageSizeByExtension($file, strtolower($extension));
127
		}
128
129 72
		return $this->getSize();
130
	}
131
132
	/**
133
	 * Get dimensions of image if type is unknown
134
	 *
135
	 * @param string $filename Path to file
136
	 */
137 18
	protected function getImagesizeUnknownType($filename)
138
	{
139
		// Grab the maximum amount of bytes we might need
140 18
		$data = $this->streamReader->getImage($filename, 0, Type\TypeJpeg::JPEG_MAX_HEADER_SIZE, false);
141
142 18
		if ($data !== false)
143 18
		{
144 17
			foreach ($this->type as $imageType)
145
			{
146 17
				$imageType->getSize($filename);
147
148 17
				if (sizeof($this->size) > 1)
149 17
				{
150 17
					break;
151
				}
152 17
			}
153 17
		}
154 18
	}
155
156
	/**
157
	 * Get image size by file extension
158
	 *
159
	 * @param string $file Path to image that should be checked
160
	 * @param string $extension Extension/type of image
161
	 */
162 54
	protected function getImageSizeByExtension($file, $extension)
163
	{
164 54
		if (isset($this->classMap[$extension]))
165 54
		{
166 53
			$this->classMap[$extension]->getSize($file);
167 53
		}
168 54
	}
169
170
	/**
171
	 * Reset values to default
172
	 */
173 72
	protected function resetValues()
174
	{
175 72
		$this->size = array();
176 72
		$this->streamReader->resetData();
177 72
	}
178
179
	/**
180
	 * Set mime type based on supplied image
181
	 *
182
	 * @param int $type Type of image
183
	 */
184 47
	public function setImageType($type)
185
	{
186 47
		$this->size['type'] = $type;
187 47
	}
188
189
	/**
190
	 * Set size info
191
	 *
192
	 * @param array $size Array containing size info for image
193
	 */
194 53
	public function setSize($size)
195
	{
196 53
		$this->size = $size;
197 53
	}
198
199
	/**
200
	 * Get return data
201
	 *
202
	 * @return array|bool Size array if dimensions could be found, false if not
203
	 */
204
	protected function getReturnData()
205
	{
206
		return sizeof($this->size) > 1 ? $this->size : false;
207
	}
208
}
209