Skip to content

Commit e7a349b

Browse files
Merge pull request #123 from felipetnh/gumlet-save-exact-size
Exact Size
2 parents 67b0746 + 203a046 commit e7a349b

File tree

1 file changed

+51
-17
lines changed

1 file changed

+51
-17
lines changed

lib/ImageResize.php

+51-17
Original file line numberDiff line numberDiff line change
@@ -211,16 +211,21 @@ public function imageCreateJpegfromExif($filename)
211211
* @param string $image_type
212212
* @param integer $quality
213213
* @param integer $permissions
214+
* @param boolean $exact_size
214215
* @return static
215216
*/
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)
217218
{
218219
$image_type = $image_type ?: $this->source_type;
219220
$quality = is_numeric($quality) ? (int) abs($quality) : null;
220221

221222
switch ($image_type) {
222223
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+
}
224229

225230
$background = imagecolorallocatealpha($dest_image, 255, 255, 255, 1);
226231
imagecolortransparent($dest_image, $background);
@@ -229,41 +234,70 @@ public function save($filename, $image_type = null, $quality = null, $permission
229234
break;
230235

231236
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+
}
236246
break;
237247

238248
case IMAGETYPE_WEBP:
239249
if (version_compare(PHP_VERSION, '5.5.0', '<')) {
240250
throw new ImageResizeException('For WebP support PHP >= 5.5.0 is required');
241251
}
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+
}
246261
break;
247262

248263
case IMAGETYPE_PNG:
249264
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+
}
255270
} 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+
}
257276
}
258277

259278
imagealphablending($dest_image, false);
260279
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);
261284
break;
262285
}
263286

264287
imageinterlace($dest_image, $this->interlace);
265-
288+
266289
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+
}
267301

268302
imagecopyresampled(
269303
$dest_image,

0 commit comments

Comments
 (0)