@@ -95,33 +95,35 @@ public function store(string $source, string $filePath): bool
9595 */
9696 public function compressUntilSize (string $ filePath , string $ extension , int $ maxBytes ): bool
9797 {
98- if (-1 === $ this ->imageCompressionQuality || filesize ($ filePath ) <= $ maxBytes ) {
98+ if (-1 === ( int ) $ this ->imageCompressionQuality || filesize ($ filePath ) <= $ maxBytes ) {
9999 // don't compress images if disabled or smaller than max bytes
100100 return false ;
101101 }
102102 $ imagine = new Imagine ();
103103 $ image = $ imagine ->open ($ filePath );
104104 $ bytes = filesize ($ filePath );
105105 $ initialBytes = $ bytes ;
106+ $ lastBytes = $ bytes ;
106107 $ tempPath = "{$ filePath }_temp_compress. $ extension " ;
107108 $ compressed = false ;
108109 $ quality = 0.9 ;
109- if (0.1 <= $ this ->imageCompressionQuality && 1 > $ this ->imageCompressionQuality ) {
110+ if (0.3 <= $ this ->imageCompressionQuality && 1 > $ this ->imageCompressionQuality ) {
110111 $ quality = $ this ->imageCompressionQuality ;
111112 }
112- while ($ bytes > $ maxBytes && $ quality > 0.1 ) {
113+ while ($ bytes > $ maxBytes && $ quality > 0.3 ) {
113114 $ this ->logger ->debug ('[ImageManager::compressUntilSize] Trying to compress "{path}" with {q}% quality ' , ['path ' => $ tempPath , 'q ' => $ quality * 100 ]);
114115 $ image ->save ($ tempPath , [
115116 'jpeg_quality ' => $ quality * 100 , // jpeg max value is 100
116117 'png_compression_level ' => 9 , // this is lossless compression, so always use the max
117118 'webp_quality ' => $ quality * 100 , // webp quality max is 100
118119 ]);
119120 $ bytes = filesize ($ tempPath );
120- if ($ initialBytes === $ bytes ) {
121+ if ($ lastBytes === $ bytes ) {
121122 // there were no changes, so maybe it is in a format that cannot be compressed...
122123 break ;
123124 }
124125 $ compressed = true ;
126+ $ lastBytes = $ bytes ;
125127 $ quality -= 0.05 ;
126128 }
127129 $ copied = false ;
0 commit comments