1: <?php
2:
3: /**
4: ##DOC-SIGNATURE##
5:
6: This file is part of WideImage.
7:
8: WideImage is free software; you can redistribute it and/or modify
9: it under the terms of the GNU Lesser General Public License as published by
10: the Free Software Foundation; either version 2.1 of the License, or
11: (at your option) any later version.
12:
13: WideImage is distributed in the hope that it will be useful,
14: but WITHOUT ANY WARRANTY; without even the implied warranty of
15: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16: GNU Lesser General Public License for more details.
17:
18: You should have received a copy of the GNU Lesser General Public License
19: along with WideImage; if not, write to the Free Software
20: Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21:
22: * @package WideImage
23: **/
24:
25: namespace WideImage;
26:
27: use WideImage\Mapper;
28: use WideImage\Exception\UnsupportedFormatException;
29:
30: /**
31: * Mapper factory
32: *
33: * @package Internals
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: * Returns a mapper, based on the $uri and $format
51: *
52: * @param string $uri File URI
53: * @param string $format File format (extension or mime-type) or null
54: * @return mixed
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: // why not use autoloading?
71: // if (!class_exists($mapperClassName, false)) {
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: // mime-type match
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: // clean the string
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: