@@ -211,16 +211,21 @@ public function imageCreateJpegfromExif($filename)
211
211
* @param string $image_type
212
212
* @param integer $quality
213
213
* @param integer $permissions
214
+ * @param boolean $exact_size
214
215
* @return static
215
216
*/
216
- public function save ($ filename , $ image_type = null , $ quality = null , $ permissions = null )
217
+ public function save ($ filename , $ image_type = null , $ quality = null , $ permissions = null , $ exact_size = false )
217
218
{
218
219
$ image_type = $ image_type ?: $ this ->source_type ;
219
220
$ quality = is_numeric ($ quality ) ? (int ) abs ($ quality ) : null ;
220
221
221
222
switch ($ image_type ) {
222
223
case IMAGETYPE_GIF :
223
- $ dest_image = imagecreatetruecolor ($ this ->getDestWidth (), $ this ->getDestHeight ());
224
+ if ( !empty ($ exact_size ) && is_array ($ exact_size ) ){
225
+ $ dest_image = imagecreatetruecolor ($ exact_size [0 ], $ exact_size [1 ]);
226
+ } else {
227
+ $ dest_image = imagecreatetruecolor ($ this ->getDestWidth (), $ this ->getDestHeight ());
228
+ }
224
229
225
230
$ background = imagecolorallocatealpha ($ dest_image , 255 , 255 , 255 , 1 );
226
231
imagecolortransparent ($ dest_image , $ background );
@@ -229,41 +234,70 @@ public function save($filename, $image_type = null, $quality = null, $permission
229
234
break ;
230
235
231
236
case IMAGETYPE_JPEG :
232
- $ dest_image = imagecreatetruecolor ($ this ->getDestWidth (), $ this ->getDestHeight ());
233
-
234
- $ background = imagecolorallocate ($ dest_image , 255 , 255 , 255 );
235
- imagefilledrectangle ($ dest_image , 0 , 0 , $ this ->getDestWidth (), $ this ->getDestHeight (), $ background );
237
+ if ( !empty ($ exact_size ) && is_array ($ exact_size ) ){
238
+ $ dest_image = imagecreatetruecolor ($ exact_size [0 ], $ exact_size [1 ]);
239
+ $ background = imagecolorallocate ($ dest_image , 255 , 255 , 255 );
240
+ imagefilledrectangle ($ dest_image , 0 , 0 , $ exact_size [0 ], $ exact_size [1 ], $ background );
241
+ } else {
242
+ $ dest_image = imagecreatetruecolor ($ this ->getDestWidth (), $ this ->getDestHeight ());
243
+ $ background = imagecolorallocate ($ dest_image , 255 , 255 , 255 );
244
+ imagefilledrectangle ($ dest_image , 0 , 0 , $ this ->getDestWidth (), $ this ->getDestHeight (), $ background );
245
+ }
236
246
break ;
237
247
238
248
case IMAGETYPE_WEBP :
239
249
if (version_compare (PHP_VERSION , '5.5.0 ' , '< ' )) {
240
250
throw new ImageResizeException ('For WebP support PHP >= 5.5.0 is required ' );
241
251
}
242
- $ dest_image = imagecreatetruecolor ($ this ->getDestWidth (), $ this ->getDestHeight ());
243
-
244
- $ background = imagecolorallocate ($ dest_image , 255 , 255 , 255 );
245
- imagefilledrectangle ($ dest_image , 0 , 0 , $ this ->getDestWidth (), $ this ->getDestHeight (), $ background );
252
+ if ( !empty ($ exact_size ) && is_array ($ exact_size ) ){
253
+ $ dest_image = imagecreatetruecolor ($ exact_size [0 ], $ exact_size [1 ]);
254
+ $ background = imagecolorallocate ($ dest_image , 255 , 255 , 255 );
255
+ imagefilledrectangle ($ dest_image , 0 , 0 , $ exact_size [0 ], $ exact_size [1 ], $ background );
256
+ } else {
257
+ $ dest_image = imagecreatetruecolor ($ this ->getDestWidth (), $ this ->getDestHeight ());
258
+ $ background = imagecolorallocate ($ dest_image , 255 , 255 , 255 );
259
+ imagefilledrectangle ($ dest_image , 0 , 0 , $ this ->getDestWidth (), $ this ->getDestHeight (), $ background );
260
+ }
246
261
break ;
247
262
248
263
case IMAGETYPE_PNG :
249
264
if (!$ this ->quality_truecolor && !imageistruecolor ($ this ->source_image )) {
250
- $ dest_image = imagecreate ( $ this -> getDestWidth (), $ this -> getDestHeight ());
251
-
252
- $ background = imagecolorallocatealpha ( $ dest_image , 255 , 255 , 255 , 1 );
253
- imagecolortransparent ( $ dest_image , $ background );
254
- imagefill ( $ dest_image , 0 , 0 , $ background );
265
+ if ( ! empty ( $ exact_size ) && is_array ( $ exact_size ) ){
266
+ $ dest_image = imagecreate ( $ exact_size [ 0 ], $ exact_size [ 1 ]);
267
+ } else {
268
+ $ dest_image = imagecreate ( $ this -> getDestWidth () , $ this -> getDestHeight () );
269
+ }
255
270
} else {
256
- $ dest_image = imagecreatetruecolor ($ this ->getDestWidth (), $ this ->getDestHeight ());
271
+ if ( !empty ($ exact_size ) && is_array ($ exact_size ) ){
272
+ $ dest_image = imagecreatetruecolor ($ exact_size [0 ], $ exact_size [1 ]);
273
+ } else {
274
+ $ dest_image = imagecreatetruecolor ($ this ->getDestWidth (), $ this ->getDestHeight ());
275
+ }
257
276
}
258
277
259
278
imagealphablending ($ dest_image , false );
260
279
imagesavealpha ($ dest_image , true );
280
+
281
+ $ background = imagecolorallocatealpha ($ dest_image , 255 , 255 , 255 , 127 );
282
+ imagecolortransparent ($ dest_image , $ background );
283
+ imagefill ($ dest_image , 0 , 0 , $ background );
261
284
break ;
262
285
}
263
286
264
287
imageinterlace ($ dest_image , $ this ->interlace );
265
-
288
+
266
289
imagegammacorrect ($ this ->source_image , 2.2 , 1.0 );
290
+
291
+ if ( !empty ($ exact_size ) && is_array ($ exact_size ) ) {
292
+ if ($ this ->getSourceHeight () < $ this ->getSourceWidth ()) {
293
+ $ this ->dest_x = 0 ;
294
+ $ this ->dest_y = ($ exact_size [1 ] - $ this ->getDestHeight ()) / 2 ;
295
+ }
296
+ if ($ this ->getSourceHeight () > $ this ->getSourceWidth ()) {
297
+ $ this ->dest_x = ($ exact_size [0 ] - $ this ->getDestWidth ()) / 2 ;
298
+ $ this ->dest_y = 0 ;
299
+ }
300
+ }
267
301
268
302
imagecopyresampled (
269
303
$ dest_image ,
0 commit comments