Factory   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Test Coverage

Coverage 96.43%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 133
ccs 27
cts 28
cp 0.9643
rs 10
c 2
b 0
f 0
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getClassname() 0 3 1
A create() 0 11 2
A detectAndCreate() 0 20 3
A getExtensionType() 0 16 4
1
<?php
2
3
/**
4
 * fast-image-size image type base
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\Image;
13
14
class Factory 
15
{
16
	//In order of which they will be tried if type is unknown
17
	//Changing order might have performance impact and/or type detection mismatch
18
	private static $supportedTypes = array(
19
		'jpeg'	=> array(
20
			'jpeg',
21
			'jpg',
22
			'jpe',
23
			'jif',
24
			'jfif',
25
			'jfi',
26
		),
27
		'png'	=> array('png'),
28
		'gif'	=> array('gif'),
29
		'jp2'	=> array(
30
			'jp2',
31
			'j2k',
32
			'jpf',
33
			'jpg2',
34
			'jpx',
35
			'jpm',
36
		),
37
		'psd'	=> array(
38
			'psd',
39
			'photoshop',
40
		),
41
		'bmp'	=> array('bmp'),
42
		'tif'	=> array(
43
			'tif',
44
			'tiff',
45
		),
46
		'iff'	=> array(
47
			'iff',
48
			'x-iff',
49
		),
50
		'ico'	=> array(
51
			'ico',
52
			'vnd.microsoft.icon',
53
			'x-icon',
54
			'icon',
55
		),
56
		'wbmp'	=> array(
57
			'wbm',
58
			'wbmp',
59
			'vnd.wap.wbmp',
60
		)
61
	);
62
63
	/**
64
	 * Create image object from filepath
65
	 *
66
	 * @param string $imageType
67
	 *
68
	 * @return string classname
69
	 */
70 145
	public static function getClassname($imageType)
71
	{
72 145
		return __NAMESPACE__.'\\Type'.ucfirst($imageType);
73
	}
74
75
	/**
76
	 * Get type from filepath extension
77
	 *
78
	 * @param string $filepath
79
	 *
80
	 * @return string|false imagetype or false if not found
81
	 */
82 145
	public static function getExtensionType($filepath)
83
	{
84 145
		$extension = strtolower(pathinfo(parse_url($filepath, PHP_URL_PATH), PATHINFO_EXTENSION));
85
86 145
		if (empty($extension)) {
87 120
			return false;
88
		}
89
90
		//Find approriate type of extension and instantiate Type object
91 25
		foreach (self::$supportedTypes as $imageType => $extensions) {
92 25
			if (in_array($extension, $extensions)) {
93 25
				return $imageType;
94
			}
95 4
		}
96
97
		return false;
98
	}
99
100
	/**
101
	 * Create image object from filepath
102
	 *
103
	 * @param string $filepath
104
	 *
105
	 * @return TypeInterface|null
106
	 */
107 145
	public static function create($filepath)
108
	{
109 145
		$imageType = self::getExtensionType($filepath);
110
111 145
		if ($imageType !== false) {
112 25
			$className = self::getClassname($imageType);
113 25
			return new $className($filepath);
114
		}
115
		
116
		//Could not find type, try matching type
117 120
		return self::detectAndCreate($filepath);
118
	}
119
120
	/**
121
	 * Detect image type by trials and create appropriate TypeInterface object
122
	 *
123
	 * @param string $filepath
124
	 *
125
	 * @return TypeInterface|null
126
	 */
127 120
	public static function detectAndCreate($filepath)
128
	{
129
		// Jpeg type uses the most bytes, so grab the maximum image bytes we could need
130 120
		$image = new TypeJpeg($filepath);
131 120
		$maxedSizedHeader = $image->getHeaderPart();
132
133 120
		foreach (self::$supportedTypes as $imageType => $extensions) {
134
135 120
			$className = self::getClassname($imageType);
136 120
			$image = new $className($filepath);
137
138
			//Set header from the maxed header to prevent multiple byte fetching of same file
139 120
			$image->setHeader($maxedSizedHeader);
140
			
141 120
			if ($image->isTypeMatch()) {
142 108
				return $image;
143
			}
144 102
		}
145
		//Unsupported extension and/or could not match the type signature
146 12
		return null;
147
	}
148
}
149