@@ -105,7 +105,8 @@ class Image
105105 const EXACT = 0b1000 ;
106106
107107 /** image types */
108- const JPEG = IMAGETYPE_JPEG ,
108+ const
109+ JPEG = IMAGETYPE_JPEG ,
109110 PNG = IMAGETYPE_PNG ,
110111 GIF = IMAGETYPE_GIF ;
111112
@@ -114,6 +115,8 @@ class Image
114115 /** @deprecated */
115116 const ENLARGE = 0 ;
116117
118+ static private $ formats = [self ::JPEG => 'jpeg ' , self ::PNG => 'png ' , self ::GIF => 'gif ' ];
119+
117120 /** @var resource */
118121 private $ image ;
119122
@@ -151,17 +154,12 @@ public static function fromFile($file, & $format = NULL)
151154 throw new Nette \NotSupportedException ('PHP extension GD is not loaded. ' );
152155 }
153156
154- static $ funcs = [
155- self ::JPEG => 'imagecreatefromjpeg ' ,
156- self ::PNG => 'imagecreatefrompng ' ,
157- self ::GIF => 'imagecreatefromgif ' ,
158- ];
159157 $ format = @getimagesize ($ file )[2 ]; // @ - files smaller than 12 bytes causes read error
160-
161- if (! isset ( $ funcs [ $ format])) {
158+ if (! isset ( self :: $ formats [ $ format ])) {
159+ $ format = NULL ;
162160 throw new UnknownImageFileException (is_file ($ file ) ? "Unknown type of file ' $ file'. " : "File ' $ file' not found. " );
163161 }
164- return new static (Callback::invokeSafe ($ funcs [$ format ], [$ file ], function ($ message ) {
162+ return new static (Callback::invokeSafe (' imagecreatefrom ' . self :: $ formats [$ format ], [$ file ], function ($ message ) {
165163 throw new ImageException ($ message );
166164 }));
167165 }
@@ -182,7 +180,7 @@ public static function fromString($s, & $format = NULL)
182180
183181 if (func_num_args () > 1 ) {
184182 $ tmp = @getimagesizefromstring ($ s )[2 ]; // @ - strings smaller than 12 bytes causes read error
185- $ format = in_array ( $ tmp , [ self ::JPEG , self :: PNG , self :: GIF ], TRUE ) ? $ tmp : NULL ;
183+ $ format = isset ( self ::$ formats [ $ tmp ] ) ? $ tmp : NULL ;
186184 }
187185
188186 return new static (Callback::invokeSafe ('imagecreatefromstring ' , [$ s ], function ($ message ) {
@@ -519,20 +517,12 @@ public function place(Image $image, $left = 0, $top = 0, $opacity = 100)
519517 public function save ($ file = NULL , $ quality = NULL , $ type = NULL )
520518 {
521519 if ($ type === NULL ) {
522- switch (strtolower ($ ext = pathinfo ($ file , PATHINFO_EXTENSION ))) {
523- case 'jpg ' :
524- case 'jpeg ' :
525- $ type = self ::JPEG ;
526- break ;
527- case 'png ' :
528- $ type = self ::PNG ;
529- break ;
530- case 'gif ' :
531- $ type = self ::GIF ;
532- break ;
533- default :
520+ $ extensions = array_flip (self ::$ formats ) + ['jpg ' => self ::JPEG ];
521+ $ ext = strtolower (pathinfo ($ file , PATHINFO_EXTENSION ));
522+ if (!isset ($ extensions [$ ext ])) {
534523 throw new Nette \InvalidArgumentException ("Unsupported file extension ' $ ext'. " );
535524 }
525+ $ type = $ extensions [$ ext ];
536526 }
537527
538528 switch ($ type ) {
@@ -595,10 +585,10 @@ public function __toString()
595585 */
596586 public function send ($ type = self ::JPEG , $ quality = NULL )
597587 {
598- if (!in_array ( $ type , [ self ::JPEG , self :: PNG , self :: GIF ], TRUE )) {
588+ if (!isset ( self ::$ formats [ $ type ] )) {
599589 throw new Nette \InvalidArgumentException ("Unsupported image type ' $ type'. " );
600590 }
601- header ('Content-Type: ' . image_type_to_mime_type ( $ type) );
591+ header ('Content-Type: image/ ' . self :: $ formats [ $ type] );
602592 return $ this ->save (NULL , $ quality , $ type );
603593 }
604594
0 commit comments