1 <?php
2 /**
3 * @author Gasper Kozak
4 * @copyright 2007-2011
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 /**
26 * Thrown when image format isn't supported
27 *
28 * @package Exceptions
29 */
30 class WideImage_UnsupportedFormatException extends WideImage_Exception {}
31
32 /**
33 * Mapper factory
34 *
35 * @package Internals
36 **/
37 abstract class WideImage_MapperFactory
38 {
39 static protected $mappers = array();
40 static protected $customMappers = array();
41
42 static protected $mimeTable = array(
43 'image/jpg' => 'JPEG',
44 'image/jpeg' => 'JPEG',
45 'image/pjpeg' => 'JPEG',
46 'image/gif' => 'GIF',
47 'image/png' => 'PNG'
48 );
49
50 /**
51 * Returns a mapper, based on the $uri and $format
52 *
53 * @param string $uri File URI
54 * @param string $format File format (extension or mime-type) or null
55 * @return WideImage_Mapper
56 **/
57 static function selectMapper($uri, $format = null)
58 {
59 $format = self::determineFormat($uri, $format);
60
61 if (array_key_exists($format, self::$mappers))
62 return self::$mappers[$format];
63
64 $mapperClassName = 'WideImage_Mapper_' . $format;
65
66 if (!class_exists($mapperClassName, false))
67 {
68 $mapperFileName = WideImage::path() . 'Mapper/' . $format . '.php';
69 if (file_exists($mapperFileName))
70 require_once $mapperFileName;
71 }
72
73 if (class_exists($mapperClassName))
74 {
75 self::$mappers[$format] = new $mapperClassName();
76 return self::$mappers[$format];
77 }
78
79 throw new WideImage_UnsupportedFormatException("Format '{$format}' is not supported.");
80 }
81
82 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 static function getCustomMappers()
89 {
90 return self::$customMappers;
91 }
92
93 static function determineFormat($uri, $format = null)
94 {
95 if ($format == null)
96 $format = self::extractExtension($uri);
97
98 // mime-type match
99 if (preg_match('~[a-z]*/[a-z-]*~i', $format))
100 if (isset(self::$mimeTable[strtolower($format)]))
101 {
102 return self::$mimeTable[strtolower($format)];
103 }
104
105 // clean the string
106 $format = strtoupper(preg_replace('/[^a-z0-9_-]/i', '', $format));
107 if ($format == 'JPG')
108 $format = 'JPEG';
109
110 return $format;
111 }
112
113 static function mimeType($format)
114 {
115 return array_search(strtoupper($format), self::$mimeTable);
116 }
117
118 static function extractExtension($uri)
119 {
120 $p = strrpos($uri, '.');
121 if ($p === false)
122 return '';
123 else
124 return substr($uri, $p + 1);
125 }
126 }
127