Completed
Pull Request — master (#21)
by
unknown
02:07
created

FastImageSize   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 212
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 96.55%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 24
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 212
ccs 56
cts 58
cp 0.9655
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 3
B getImageSize() 0 19 5
A getImagesizeUnknownType() 0 18 4
A getImageSizeByExtension() 0 8 2
A resetValues() 0 5 1
A setImageType() 0 4 1
A setSize() 0 4 1
B getImage() 0 16 5
A getReturnData() 0 4 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 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 67
	public function __construct()
78
	{
79 67
		foreach ($this->supportedTypes as $imageType => $extension)
80
		{
81 67
			$className = '\FastImageSize\Type\Type' . mb_convert_case(mb_strtolower($imageType), MB_CASE_TITLE);
82 67
			$this->type[$imageType] = new $className($this);
83
84
			// Create class map
85 67
			foreach ($extension as $ext)
86
			{
87
				/** @var Type\TypeInterface */
88 67
				$this->classMap[$ext] = $this->type[$imageType];
89 67
			}
90 67
		}
91 67
	}
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 67
	public function getImageSize($file, $type = '')
101
	{
102
		// Reset values
103 67
		$this->resetValues();
104
105
		// Treat image type as unknown if extension or mime type is unknown
106 67
		if (!preg_match('/\.([a-z0-9]+)$/i', $file, $match) && empty($type))
107 67
		{
108 17
			$this->getImagesizeUnknownType($file);
109 17
		}
110
		else
111
		{
112 50
			$extension = (isset($match[1])) ? $match[1] : preg_replace('/.+\/([a-z0-9-.]+)$/i', '$1', $type);
113
114 50
			$this->getImageSizeByExtension($file, strtolower($extension));
115
		}
116
117 67
		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 50
	protected function getImageSizeByExtension($file, $extension)
151
	{
152 50
		$extension = strtolower($extension);
153 50
		if (isset($this->classMap[$extension]))
154 50
		{
155 49
			$this->classMap[$extension]->getSize($file);
156 49
		}
157 50
	}
158
159
	/**
160
	 * Reset values to default
161
	 */
162 67
	protected function resetValues()
163
	{
164 67
		$this->size = array();
165 67
		$this->data = '';
166 67
	}
167
168
	/**
169
	 * Set mime type based on supplied image
170
	 *
171
	 * @param int $type Type of image
172
	 */
173 42
	public function setImageType($type)
174
	{
175 42
		$this->size['type'] = $type;
176 42
	}
177
178
	/**
179
	 * Set size info
180
	 *
181
	 * @param array $size Array containing size info for image
182
	 */
183 48
	public function setSize($size)
184
	{
185 48
		$this->size = $size;
186 48
	}
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 66
	public function getImage($filename, $offset, $length, $forceLength = true)
200
	{
201 66
		if (empty($this->data))
202 66
		{
203 66
			$this->data = @file_get_contents($filename, null, null, $offset, $length);
204 66
		}
205
206
		// Force length to expected one. Return false if data length
207
		// is smaller than expected length
208 66
		if ($forceLength === true)
209 66
		{
210 46
			return (strlen($this->data) < $length) ? false : substr($this->data, $offset, $length) ;
211
		}
212
213 36
		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