@@ -108,14 +108,15 @@ class Image
108108 const
109109 JPEG = IMAGETYPE_JPEG ,
110110 PNG = IMAGETYPE_PNG ,
111- GIF = IMAGETYPE_GIF ;
111+ GIF = IMAGETYPE_GIF ,
112+ WEBP = 18 ; // IMAGETYPE_WEBP is available as of PHP 7.1
112113
113114 const EMPTY_GIF = "GIF89a \x01\x00\x01\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00! \xf9\x04\x01\x00\x00\x00\x00, \x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02D \x01\x00; " ;
114115
115116 /** @deprecated */
116117 const ENLARGE = 0 ;
117118
118- static private $ formats = [self ::JPEG => 'jpeg ' , self ::PNG => 'png ' , self ::GIF => 'gif ' ];
119+ static private $ formats = [self ::JPEG => 'jpeg ' , self ::PNG => 'png ' , self ::GIF => 'gif ' , self :: WEBP => ' webp ' ];
119120
120121 /** @var resource */
121122 private $ image ;
@@ -155,6 +156,9 @@ public static function fromFile($file, & $format = NULL)
155156 }
156157
157158 $ format = @getimagesize ($ file )[2 ]; // @ - files smaller than 12 bytes causes read error
159+ if (!$ format && PHP_VERSION_ID < 70100 && @file_get_contents ($ file , FALSE , NULL , 8 , 4 ) === 'WEBP ' ) { // @ - may not exists
160+ $ format = self ::WEBP ;
161+ }
158162 if (!isset (self ::$ formats [$ format ])) {
159163 $ format = NULL ;
160164 throw new UnknownImageFileException (is_file ($ file ) ? "Unknown type of file ' $ file'. " : "File ' $ file' not found. " );
@@ -510,7 +514,7 @@ public function place(Image $image, $left = 0, $top = 0, $opacity = 100)
510514 /**
511515 * Saves image to the file.
512516 * @param string filename
513- * @param int quality 0..100 ( for JPEG and PNG)
517+ * @param int quality ( 0..100 for JPEG and WEBP, 0..9 for PNG)
514518 * @param int optional image type
515519 * @return bool TRUE on success or FALSE on failure.
516520 */
@@ -537,6 +541,10 @@ public function save($file = NULL, $quality = NULL, $type = NULL)
537541 case self ::GIF :
538542 return imagegif ($ this ->image , $ file );
539543
544+ case self ::WEBP :
545+ $ quality = $ quality === NULL ? 80 : max (0 , min (100 , (int ) $ quality ));
546+ return imagewebp ($ this ->image , $ file , $ quality );
547+
540548 default :
541549 throw new Nette \InvalidArgumentException ("Unsupported image type ' $ type'. " );
542550 }
@@ -546,7 +554,7 @@ public function save($file = NULL, $quality = NULL, $type = NULL)
546554 /**
547555 * Outputs image to string.
548556 * @param int image type
549- * @param int quality 0..100 ( for JPEG and PNG)
557+ * @param int quality ( 0..100 for JPEG and WEBP, 0..9 for PNG)
550558 * @return string
551559 */
552560 public function toString ($ type = self ::JPEG , $ quality = NULL )
@@ -580,7 +588,7 @@ public function __toString()
580588 /**
581589 * Outputs image to browser.
582590 * @param int image type
583- * @param int quality 0..100 ( for JPEG and PNG)
591+ * @param int quality ( 0..100 for JPEG and WEBP, 0..9 for PNG)
584592 * @return bool TRUE on success or FALSE on failure.
585593 */
586594 public function send ($ type = self ::JPEG , $ quality = NULL )
0 commit comments