| 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 |  |  | 	public static function getClassname($imageType) | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  | 	{ | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  | 		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 |  |  | 	public static function getExtensionType($filepath) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 		$extension = strtolower(pathinfo(parse_url($filepath, PHP_URL_PATH), PATHINFO_EXTENSION)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 		if (empty($extension)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 			return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 		//Find approriate type of extension and instantiate Type object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 		foreach (self::$supportedTypes as $imageType => $extensions) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 			if (in_array($extension, $extensions)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 				return $imageType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 |  |  | 	public static function create($filepath) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 		$imageType = self::getExtensionType($filepath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 		if ($imageType !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 			$className = self::getClassname($imageType); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 			return new $className($filepath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 		//Could not find type, try matching type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 		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 |  |  | 	public static function detectAndCreate($filepath) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 		// Jpeg type uses the most bytes, so grab the maximum image bytes we could need | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 		$image = new TypeJpeg($filepath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 		$maxedSizedHeader = $image->getHeaderPart(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 		foreach (self::$supportedTypes as $imageType => $extensions) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 			$className = self::getClassname($imageType); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 			$image = new $className($filepath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 			//Set header from the maxed header to prevent multiple byte fetching of same file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 			$image->setHeader($maxedSizedHeader); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 			 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 			if ($image->isTypeMatch()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 				return $image; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 		//Unsupported extension and/or could not match the type signature | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 		return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 148 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 149 |  |  |  |