@@ -481,7 +481,7 @@ public function place(self $image, $left = 0, $top = 0, int $opacity = 100)
481481 * Saves image to the file. Quality is 0..100 for JPEG and WEBP, 0..9 for PNG.
482482 * @throws ImageException
483483 */
484- public function save (string $ file = null , int $ quality = null , int $ type = null ): void
484+ public function save (string $ file , int $ quality = null , int $ type = null ): void
485485 {
486486 if ($ type === null ) {
487487 $ extensions = array_flip (self ::$ formats ) + ['jpg ' => self ::JPEG ];
@@ -492,32 +492,7 @@ public function save(string $file = null, int $quality = null, int $type = null)
492492 $ type = $ extensions [$ ext ];
493493 }
494494
495- switch ($ type ) {
496- case self ::JPEG :
497- $ quality = $ quality === null ? 85 : max (0 , min (100 , $ quality ));
498- $ success = imagejpeg ($ this ->image , $ file , $ quality );
499- break ;
500-
501- case self ::PNG :
502- $ quality = $ quality === null ? 9 : max (0 , min (9 , $ quality ));
503- $ success = imagepng ($ this ->image , $ file , $ quality );
504- break ;
505-
506- case self ::GIF :
507- $ success = imagegif ($ this ->image , $ file );
508- break ;
509-
510- case self ::WEBP :
511- $ quality = $ quality === null ? 80 : max (0 , min (100 , $ quality ));
512- $ success = imagewebp ($ this ->image , $ file , $ quality );
513- break ;
514-
515- default :
516- throw new Nette \InvalidArgumentException ("Unsupported image type ' $ type'. " );
517- }
518- if (!$ success ) {
519- throw new ImageException (error_get_last ()['message ' ]);
520- }
495+ $ this ->output ($ type , $ quality , $ file );
521496 }
522497
523498
@@ -527,7 +502,7 @@ public function save(string $file = null, int $quality = null, int $type = null)
527502 public function toString (int $ type = self ::JPEG , int $ quality = null ): string
528503 {
529504 ob_start (function () {});
530- $ this ->save ( null , $ quality , $ type );
505+ $ this ->output ( $ type , $ quality );
531506 return ob_get_clean ();
532507 }
533508
@@ -558,7 +533,42 @@ public function send(int $type = self::JPEG, int $quality = null): void
558533 throw new Nette \InvalidArgumentException ("Unsupported image type ' $ type'. " );
559534 }
560535 header ('Content-Type: ' . image_type_to_mime_type ($ type ));
561- $ this ->save (null , $ quality , $ type );
536+ $ this ->output ($ type , $ quality );
537+ }
538+
539+
540+ /**
541+ * Outputs image to browser or file.
542+ * @throws ImageException
543+ */
544+ private function output (int $ type , ?int $ quality , string $ file = null ): void
545+ {
546+ switch ($ type ) {
547+ case self ::JPEG :
548+ $ quality = $ quality === null ? 85 : max (0 , min (100 , $ quality ));
549+ $ success = imagejpeg ($ this ->image , $ file , $ quality );
550+ break ;
551+
552+ case self ::PNG :
553+ $ quality = $ quality === null ? 9 : max (0 , min (9 , $ quality ));
554+ $ success = imagepng ($ this ->image , $ file , $ quality );
555+ break ;
556+
557+ case self ::GIF :
558+ $ success = imagegif ($ this ->image , $ file );
559+ break ;
560+
561+ case self ::WEBP :
562+ $ quality = $ quality === null ? 80 : max (0 , min (100 , $ quality ));
563+ $ success = imagewebp ($ this ->image , $ file , $ quality );
564+ break ;
565+
566+ default :
567+ throw new Nette \InvalidArgumentException ("Unsupported image type ' $ type'. " );
568+ }
569+ if (!$ success ) {
570+ throw new ImageException (error_get_last ()['message ' ]);
571+ }
562572 }
563573
564574
0 commit comments