1717namespace Phauthentic \Infrastructure \Storage \Processor \Image ;
1818
1919use GuzzleHttp \Psr7 \StreamWrapper ;
20+ use http \Exception \InvalidArgumentException ;
2021use Intervention \Image \Image ;
2122use Intervention \Image \ImageManager ;
2223use League \Flysystem \Config ;
2526use Phauthentic \Infrastructure \Storage \PathBuilder \PathBuilderInterface ;
2627use Phauthentic \Infrastructure \Storage \FileStorageInterface ;
2728use Phauthentic \Infrastructure \Storage \Processor \ProcessorInterface ;
29+ use Phauthentic \Infrastructure \Storage \UrlBuilder \UrlBuilderInterface ;
2830use Phauthentic \Infrastructure \Storage \Utility \TemporaryFile ;
2931
3032use function Phauthentic \Infrastructure \Storage \fopen ;
@@ -71,6 +73,13 @@ class ImageProcessor implements ProcessorInterface
7173 */
7274 protected Image $ image ;
7375
76+ /**
77+ * Quality setting for writing images
78+ *
79+ * @var int
80+ */
81+ protected int $ quality = 90 ;
82+
7483 /**
7584 * @param \Phauthentic\Infrastructure\Storage\FileStorageInterface $storageHandler File Storage Handler
7685 * @param \Phauthentic\Infrastructure\Storage\PathBuilder\PathBuilderInterface $pathBuilder Path Builder
@@ -79,11 +88,13 @@ class ImageProcessor implements ProcessorInterface
7988 public function __construct (
8089 FileStorageInterface $ storageHandler ,
8190 PathBuilderInterface $ pathBuilder ,
82- ImageManager $ imageManager
91+ ImageManager $ imageManager ,
92+ ?UrlBuilderInterface $ urlBuilder = null
8393 ) {
8494 $ this ->storageHandler = $ storageHandler ;
8595 $ this ->pathBuilder = $ pathBuilder ;
8696 $ this ->imageManager = $ imageManager ;
97+ $ this ->urlBuilder = $ urlBuilder ;
8798 }
8899
89100 /**
@@ -97,6 +108,24 @@ protected function setMimeTypes(array $mimeTypes): self
97108 return $ this ;
98109 }
99110
111+ /**
112+ * @param int $quality Quality
113+ * @return $this
114+ */
115+ public function setQuality (int $ quality ): self
116+ {
117+ if ($ quality > 100 || $ quality <= 0 ) {
118+ throw new InvalidArgumentException (sprintf (
119+ 'Quality has to be a positive integer between 1 and 100. %s was provided ' ,
120+ (string )$ quality
121+ ));
122+ }
123+
124+ $ this ->quality = $ quality ;
125+
126+ return $ this ;
127+ }
128+
100129 /**
101130 * @param \Phauthentic\Infrastructure\Storage\FileInterface $file File
102131 * @return bool
@@ -221,13 +250,19 @@ public function process(FileInterface $file): FileInterface
221250 } else {
222251 $ storage ->writeStream (
223252 $ path ,
224- StreamWrapper::getResource ($ this ->image ->stream ($ file ->extension (), 90 )),
253+ StreamWrapper::getResource ($ this ->image ->stream ($ file ->extension (), $ this -> quality )),
225254 new Config ()
226255 );
227256 }
228257
229258 $ data ['path ' ] = $ path ;
230259 $ file = $ file ->withVariant ($ variant , $ data );
260+
261+ if ($ this ->urlBuilder ) {
262+ $ data ['url ' ] = $ this ->urlBuilder ->urlForVariant ($ file , $ variant );
263+ }
264+
265+ $ file = $ file ->withVariant ($ variant , $ data );
231266 }
232267
233268 unlink ($ tempFile );
0 commit comments