Skip to content

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

Merged
merged 2 commits into from
Nov 30, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 51 additions & 17 deletions lib/ImageResize.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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]);
Copy link
Contributor

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.

} else{
$dest_image = imagecreate($this->getDestWidth(), $this->getDestHeight());
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by "the additional space"?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The white space seems to be existed before the $dest_image variable :).

}
} 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) ) {
Copy link
Contributor

@peter279k peter279k Jun 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's mentioned on issue #142.

Copy link
Contributor

Choose a reason for hiding this comment

The 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 291 to 300.

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,
Expand Down