Completed
Pull Request — master (#27)
by Marc
02:03
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
	);
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 71
	public function __construct()
78
	{
79 71
		foreach ($this->supportedTypes as $imageType => $extension)
80
		{
81 71
			$className = '\FastImageSize\Type\Type' . mb_convert_case(mb_strtolower($imageType), MB_CASE_TITLE);
82 71
			$this->type[$imageType] = new $className($this);
83
84
			// Create class map
85 71
			foreach ($extension as $ext)
86
			{
87
				/** @var Type\TypeInterface */
88 71
				$this->classMap[$ext] = $this->type[$imageType];
89 71
			}
90 71
		}
91 71
	}
92
93
	/**
94
	 * Get image dimensions of supplied image
95
	 *
96
	 * @param string $file Path to image that should be checked
97
	 * @param string $type Mimetype of image
98
	 * @return array|bool Array with image dimensions if successful, false if not
99
	 */
100 71
	public function getImageSize($file, $type = '')
101
	{
102
		// Reset values
103 71
		$this->resetValues();
104
105
		// Treat image type as unknown if extension or mime type is unknown
106 71
		if (!preg_match('/\.([a-z0-9]+)$/i', $file, $match) && empty($type))
107 71
		{
108 17
			$this->getImagesizeUnknownType($file);
109 17
		}
110
		else
111
		{
112 54
			$extension = (isset($match[1])) ? $match[1] : preg_replace('/.+\/([a-z0-9-.]+)$/i', '$1', $type);
113
114 54
			$this->getImageSizeByExtension($file, $extension);
115
		}
116
117 71
		return sizeof($this->size) > 1 ? $this->size : false;
118
	}
119
120
	/**
121
	 * Get dimensions of image if type is unknown
122
	 *
123
	 * @param string $filename Path to file
124
	 */
125 17
	protected function getImagesizeUnknownType($filename)
126
	{
127
		// Grab the maximum amount of bytes we might need
128 17
		$data = $this->getImage($filename, 0, Type\TypeJpeg::JPEG_MAX_HEADER_SIZE, false);
129
130 17
		if ($data !== false)
131 17
		{
132 16
			foreach ($this->type as $imageType)
133
			{
134 16
				$imageType->getSize($filename);
135
136 16
				if (sizeof($this->size) > 1)
137 16
				{
138 16
					break;
139
				}
140 16
			}
141 16
		}
142 17
	}
143
144
	/**
145
	 * Get image size by file extension
146
	 *
147
	 * @param string $file Path to image that should be checked
148
	 * @param string $extension Extension/type of image
149
	 */
150 54
	protected function getImageSizeByExtension($file, $extension)
151
	{
152 54
		$extension = strtolower($extension);
153 54
		if (isset($this->classMap[$extension]))
154 54
		{
155 53
			$this->classMap[$extension]->getSize($file);
156 53
		}
157 54
	}
158
159
	/**
160
	 * Reset values to default
161
	 */
162 71
	protected function resetValues()
163
	{
164 71
		$this->size = array();
165 71
		$this->data = '';
166 71
	}
167
168
	/**
169
	 * Set mime type based on supplied image
170
	 *
171
	 * @param int $type Type of image
172
	 */
173 46
	public function setImageType($type)
174
	{
175 46
		$this->size['type'] = $type;
176 46
	}
177
178
	/**
179
	 * Set size info
180
	 *
181
	 * @param array $size Array containing size info for image
182
	 */
183 52
	public function setSize($size)
184
	{
185 52
		$this->size = $size;
186 52
	}
187
188
	/**
189
	 * Get image from specified path/source
190
	 *
191
	 * @param string $filename Path to image
192
	 * @param int $offset Offset at which reading of the image should start
193
	 * @param int $length Maximum length that should be read
194
	 * @param bool $forceLength True if the length needs to be the specified
195
	 *			length, false if not. Default: true
196
	 *
197
	 * @return false|string Image data or false if result was empty
198
	 */
199 70
	public function getImage($filename, $offset, $length, $forceLength = true)
200
	{
201 70
		if (empty($this->data))
202 70
		{
203 70
			$this->data = @file_get_contents($filename, null, null, $offset, $length);
204 70
		}
205
206
		// Force length to expected one. Return false if data length
207
		// is smaller than expected length
208 70
		if ($forceLength === true)
209 70
		{
210 46
			return (strlen($this->data) < $length) ? false : substr($this->data, $offset, $length) ;
211
		}
212
213 40
		return empty($this->data) ? false : $this->data;
214
	}
215
216
	/**
217
	 * Get return data
218
	 *
219
	 * @return array|bool Size array if dimensions could be found, false if not
220
	 */
221
	protected function getReturnData()
222
	{
223
		return sizeof($this->size) > 1 ? $this->size : false;
224
	}
225
}
226