diff --git a/lib/ImageResize.php b/lib/ImageResize.php index 24ef5cc..57d58a0 100644 --- a/lib/ImageResize.php +++ b/lib/ImageResize.php @@ -211,16 +211,21 @@ public function imageCreateJpegfromExif($filename) * @param string $image_type * @param integer $quality * @param integer $permissions + * @param boolean $exact_size * @return static */ - public function save($filename, $image_type = null, $quality = null, $permissions = null) + public function save($filename, $image_type = null, $quality = null, $permissions = null, $exact_size = false) { $image_type = $image_type ?: $this->source_type; $quality = is_numeric($quality) ? (int) abs($quality) : null; switch ($image_type) { case IMAGETYPE_GIF: - $dest_image = imagecreatetruecolor($this->getDestWidth(), $this->getDestHeight()); + if( !empty($exact_size) && is_array($exact_size) ){ + $dest_image = imagecreatetruecolor($exact_size[0], $exact_size[1]); + } else{ + $dest_image = imagecreatetruecolor($this->getDestWidth(), $this->getDestHeight()); + } $background = imagecolorallocatealpha($dest_image, 255, 255, 255, 1); imagecolortransparent($dest_image, $background); @@ -229,41 +234,70 @@ public function save($filename, $image_type = null, $quality = null, $permission break; case IMAGETYPE_JPEG: - $dest_image = imagecreatetruecolor($this->getDestWidth(), $this->getDestHeight()); - - $background = imagecolorallocate($dest_image, 255, 255, 255); - imagefilledrectangle($dest_image, 0, 0, $this->getDestWidth(), $this->getDestHeight(), $background); + if( !empty($exact_size) && is_array($exact_size) ){ + $dest_image = imagecreatetruecolor($exact_size[0], $exact_size[1]); + $background = imagecolorallocate($dest_image, 255, 255, 255); + imagefilledrectangle($dest_image, 0, 0, $exact_size[0], $exact_size[1], $background); + } else{ + $dest_image = imagecreatetruecolor($this->getDestWidth(), $this->getDestHeight()); + $background = imagecolorallocate($dest_image, 255, 255, 255); + imagefilledrectangle($dest_image, 0, 0, $this->getDestWidth(), $this->getDestHeight(), $background); + } break; case IMAGETYPE_WEBP: if (version_compare(PHP_VERSION, '5.5.0', '<')) { throw new ImageResizeException('For WebP support PHP >= 5.5.0 is required'); } - $dest_image = imagecreatetruecolor($this->getDestWidth(), $this->getDestHeight()); - - $background = imagecolorallocate($dest_image, 255, 255, 255); - imagefilledrectangle($dest_image, 0, 0, $this->getDestWidth(), $this->getDestHeight(), $background); + if( !empty($exact_size) && is_array($exact_size) ){ + $dest_image = imagecreatetruecolor($exact_size[0], $exact_size[1]); + $background = imagecolorallocate($dest_image, 255, 255, 255); + imagefilledrectangle($dest_image, 0, 0, $exact_size[0], $exact_size[1], $background); + } else{ + $dest_image = imagecreatetruecolor($this->getDestWidth(), $this->getDestHeight()); + $background = imagecolorallocate($dest_image, 255, 255, 255); + imagefilledrectangle($dest_image, 0, 0, $this->getDestWidth(), $this->getDestHeight(), $background); + } break; case IMAGETYPE_PNG: if (!$this->quality_truecolor && !imageistruecolor($this->source_image)) { - $dest_image = imagecreate($this->getDestWidth(), $this->getDestHeight()); - - $background = imagecolorallocatealpha($dest_image, 255, 255, 255, 1); - imagecolortransparent($dest_image, $background); - imagefill($dest_image, 0, 0, $background); + if( !empty($exact_size) && is_array($exact_size) ){ + $dest_image = imagecreate($exact_size[0], $exact_size[1]); + } else{ + $dest_image = imagecreate($this->getDestWidth(), $this->getDestHeight()); + } } else { - $dest_image = imagecreatetruecolor($this->getDestWidth(), $this->getDestHeight()); + if( !empty($exact_size) && is_array($exact_size) ){ + $dest_image = imagecreatetruecolor($exact_size[0], $exact_size[1]); + } else{ + $dest_image = imagecreatetruecolor($this->getDestWidth(), $this->getDestHeight()); + } } imagealphablending($dest_image, false); imagesavealpha($dest_image, true); + + $background = imagecolorallocatealpha($dest_image, 255, 255, 255, 127); + imagecolortransparent($dest_image, $background); + imagefill($dest_image, 0, 0, $background); break; } imageinterlace($dest_image, $this->interlace); - + imagegammacorrect($this->source_image, 2.2, 1.0); + + if( !empty($exact_size) && is_array($exact_size) ) { + if ($this->getSourceHeight() < $this->getSourceWidth()) { + $this->dest_x = 0; + $this->dest_y = ($exact_size[1] - $this->getDestHeight()) / 2; + } + if ($this->getSourceHeight() > $this->getSourceWidth()) { + $this->dest_x = ($exact_size[0] - $this->getDestWidth()) / 2; + $this->dest_y = 0; + } + } imagecopyresampled( $dest_image,