Completed
Pull Request — master (#49)
by Josh
04:04
created

FastImageSize::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 0
crap 3
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 array Size info that is returned */
17
	protected $size = array();
18
19
	/** @var string Data retrieved from remote */
20
	protected $data = '';
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
		'webp'	=> array(
67
				'webp',
68
		)
69
	);
70
71
	/** @var array Class map that links image extensions/mime types to class */
72
	protected $classMap;
73
74
	/** @var array An array containing the classes of supported image types */
75
	protected $type;
76
77
	/**
78
	 * Get image dimensions of supplied image
79
	 *
80
	 * @param string $file Path to image that should be checked
81
	 * @param string $type Mimetype of image
82
	 * @return array|bool Array with image dimensions if successful, false if not
83
	 */
84 88
	public function getImageSize($file, $type = '')
85
	{
86
		// Reset values
87 88
		$this->resetValues();
88
89
		// Treat image type as unknown if extension or mime type is unknown
90 88
		if (!preg_match('/\.([a-z0-9]+)$/i', $file, $match) && empty($type))
91 88
		{
92 17
			$this->getImagesizeUnknownType($file);
93 17
		}
94
		else
95
		{
96 71
			$extension = (empty($type) && isset($match[1])) ? $match[1] : preg_replace('/.+\/([a-z0-9-.]+)$/i', '$1', $type);
97
98 71
			$this->getImageSizeByExtension($file, $extension);
99
		}
100
101 88
		return sizeof($this->size) > 1 ? $this->size : false;
102
	}
103
104
	/**
105
	 * Get dimensions of image if type is unknown
106
	 *
107
	 * @param string $filename Path to file
108
	 */
109 17
	protected function getImagesizeUnknownType($filename)
110
	{
111
		// Grab the maximum amount of bytes we might need
112 17
		$data = $this->getImage($filename, 0, Type\TypeJpeg::JPEG_MAX_HEADER_SIZE, false);
113
114 17
		if ($data !== false)
115 17
		{
116 16
			$this->loadAllTypes();
117 16
			foreach ($this->type as $imageType)
118
			{
119 16
				$imageType->getSize($filename);
120
121 16
				if (sizeof($this->size) > 1)
122 16
				{
123 16
					break;
124
				}
125 16
			}
126 16
		}
127 17
	}
128
129
	/**
130
	 * Get image size by file extension
131
	 *
132
	 * @param string $file Path to image that should be checked
133
	 * @param string $extension Extension/type of image
134
	 */
135 71
	protected function getImageSizeByExtension($file, $extension)
136
	{
137 71
		$extension = strtolower($extension);
138 71
		$this->loadExtension($extension);
139 71
		if (isset($this->classMap[$extension]))
140 71
		{
141 70
			$this->classMap[$extension]->getSize($file);
142 70
		}
143 71
	}
144
145
	/**
146
	 * Reset values to default
147
	 */
148 88
	protected function resetValues()
149
	{
150 88
		$this->size = array();
151 88
		$this->data = '';
152 88
	}
153
154
	/**
155
	 * Set mime type based on supplied image
156
	 *
157
	 * @param int $type Type of image
158
	 */
159 56
	public function setImageType($type)
160
	{
161 56
		$this->size['type'] = $type;
162 56
	}
163
164
	/**
165
	 * Set size info
166
	 *
167
	 * @param array $size Array containing size info for image
168
	 */
169 63
	public function setSize($size)
170
	{
171 63
		$this->size = $size;
172 63
	}
173
174
	/**
175
	 * Get image from specified path/source
176
	 *
177
	 * @param string $filename Path to image
178
	 * @param int $offset Offset at which reading of the image should start
179
	 * @param int $length Maximum length that should be read
180
	 * @param bool $forceLength True if the length needs to be the specified
181
	 *			length, false if not. Default: true
182
	 *
183
	 * @return false|string Image data or false if result was empty
184
	 */
185 87
	public function getImage($filename, $offset, $length, $forceLength = true)
186
	{
187 87
		if (empty($this->data))
188 87
		{
189 87
			$this->data = @file_get_contents($filename, null, null, $offset, $length);
190 87
		}
191
192
		// Force length to expected one. Return false if data length
193
		// is smaller than expected length
194 87
		if ($forceLength === true)
195 87
		{
196 55
			return (strlen($this->data) < $length) ? false : substr($this->data, $offset, $length) ;
197
		}
198
199 48
		return empty($this->data) ? false : $this->data;
200
	}
201
202
	/**
203
	 * Get return data
204
	 *
205
	 * @return array|bool Size array if dimensions could be found, false if not
206
	 */
207
	protected function getReturnData()
208
	{
209
		return sizeof($this->size) > 1 ? $this->size : false;
210
	}
211
212
	/**
213
	 * Load all supported types
214
	 */
215 16
	protected function loadAllTypes()
216
	{
217 16
		foreach ($this->supportedTypes as $imageType => $extension)
218
		{
219 16
			$this->loadType($imageType, $extension);
0 ignored issues
show
Unused Code introduced by
The call to FastImageSize::loadType() has too many arguments starting with $extension.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
220 16
		}
221 16
	}
222
223
	/**
224
	 * Load an image type by extension
225
	 *
226
	 * @param string $extension Extension of image
227
	 */
228 71
	protected function loadExtension($extension)
229
	{
230 71
		if (isset($this->classMap[$extension]))
231 71
		{
232 3
			return;
233
		}
234 71
		foreach ($this->supportedTypes as $imageType => $extensions)
235
		{
236 71
			if (in_array($extension, $extensions, true))
237 71
			{
238 70
				$this->loadType($imageType);
239 70
			}
240 71
		}
241 71
	}
242
243
	/**
244
	 * Load an image type
245
	 *
246
	 * @param string $imageType Mimetype
247
	 */
248 86
	protected function loadType($imageType)
249
	{
250 86
		if (isset($this->type[$imageType]))
251 86
		{
252
			return;
253
		}
254
255 86
		$className = '\FastImageSize\Type\Type' . mb_convert_case(mb_strtolower($imageType), MB_CASE_TITLE);
256 86
		$this->type[$imageType] = new $className($this);
257
258
		// Create class map
259 86
		foreach ($this->supportedTypes[$imageType] as $ext)
260
		{
261
			/** @var Type\TypeInterface */
262 86
			$this->classMap[$ext] = $this->type[$imageType];
263 86
		}
264 86
	}
265
}
266