1: | <?php |
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | |
18: | |
19: | |
20: | |
21: | |
22: | |
23: | |
24: | |
25: | namespace WideImage; |
26: | |
27: | use WideImage\Mapper; |
28: | use WideImage\Exception\UnsupportedFormatException; |
29: | |
30: | |
31: | |
32: | |
33: | |
34: | |
35: | abstract class MapperFactory |
36: | { |
37: | static protected $mappers = array(); |
38: | static protected $customMappers = array(); |
39: | |
40: | static protected $mimeTable = array( |
41: | 'image/jpg' => 'JPEG', |
42: | 'image/jpeg' => 'JPEG', |
43: | 'image/pjpeg' => 'JPEG', |
44: | 'image/gif' => 'GIF', |
45: | 'image/png' => 'PNG', |
46: | 'image/webp' => 'WEBP' |
47: | ); |
48: | |
49: | |
50: | |
51: | |
52: | |
53: | |
54: | |
55: | |
56: | public static function selectMapper($uri, $format = null) |
57: | { |
58: | $format = self::determineFormat($uri, $format); |
59: | |
60: | if (empty($format)) { |
61: | return false; |
62: | } |
63: | |
64: | if (array_key_exists($format, self::$mappers)) { |
65: | return self::$mappers[$format]; |
66: | } |
67: | |
68: | $mapperClassName = '\\WideImage\\Mapper\\' . $format; |
69: | |
70: | |
71: | |
72: | if (!class_exists($mapperClassName)) { |
73: | throw new UnsupportedFormatException("Format '{$format}' is not supported."); |
74: | } |
75: | |
76: | if (class_exists($mapperClassName)) { |
77: | self::$mappers[$format] = new $mapperClassName(); |
78: | return self::$mappers[$format]; |
79: | } |
80: | } |
81: | |
82: | public static function registerMapper($mapper_class_name, $mime_type, $extension) |
83: | { |
84: | self::$customMappers[$mime_type] = $mapper_class_name; |
85: | self::$mimeTable[$mime_type] = $extension; |
86: | } |
87: | |
88: | public static function getCustomMappers() |
89: | { |
90: | return self::$customMappers; |
91: | } |
92: | |
93: | public static function determineFormat($uri, $format = null) |
94: | { |
95: | if ($format == null) { |
96: | $format = self::extractExtension($uri); |
97: | } |
98: | |
99: | |
100: | if (preg_match('~[a-z]*/[a-z-]*~i', $format)) { |
101: | if (isset(self::$mimeTable[strtolower($format)])) { |
102: | return self::$mimeTable[strtolower($format)]; |
103: | } |
104: | } |
105: | |
106: | |
107: | $format = strtoupper(preg_replace('/[^a-z0-9_-]/i', '', $format)); |
108: | if ($format == 'JPG') { |
109: | $format = 'JPEG'; |
110: | } |
111: | |
112: | return $format; |
113: | } |
114: | |
115: | public static function mimeType($format) |
116: | { |
117: | $format = strtoupper($format); |
118: | |
119: | if ($format == 'JPG') { |
120: | $format = 'JPEG'; |
121: | } |
122: | |
123: | return array_search($format, self::$mimeTable); |
124: | } |
125: | |
126: | public static function extractExtension($uri) |
127: | { |
128: | $p = strrpos($uri, '.'); |
129: | |
130: | if ($p === false) { |
131: | return ''; |
132: | } |
133: | |
134: | return substr($uri, $p + 1); |
135: | } |
136: | } |
137: | |