-
Notifications
You must be signed in to change notification settings - Fork 313
Exact Size #123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Exact Size #123
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems to have the additional space. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you mean by "the additional space"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The white space seems to be existed before the |
||
} | ||
} 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) ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's mentioned on issue #142. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @adityapatadia, this codes will effect and cause on the issue #142. The line |
||
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, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to have the additional space.