Don't add white space to images in upload

fahrenheit

Member
XNullUser
Joined
Nov 7, 2021
Messages
121
Reaction score
0
Points
16
NullCash
2
Prestashop has a setting that doen't make sense, at least for me, that is adding white spaces (or transparency) in images to complete the dimensions. Fortunately there is a simple way to solve it:

Here's a quick fix for this if anyone runs into this issue. Open ImageManager.php in the classes folder, go to line 265 and delete all of this:
Code:
$destImage = imagecreatetruecolor($destinationWidth, $destinationHeight);

// If image is a PNG and the output is PNG, fill with transparency. Else fill with white background.
        if ($fileType == 'png' && $type == IMAGETYPE_PNG) {
            imagealphablending($destImage, false);
            imagesavealpha($destImage, true);
            $transparent = imagecolorallocatealpha($destImage, 255, 255, 255, 127);
            imagefilledrectangle($destImage, 0, 0, $destinationWidth, $destinationHeight, $transparent);
        } else {
            $white = imagecolorallocate($destImage, 255, 255, 255);
            imagefilledrectangle($destImage, 0, 0, $destinationWidth, $destinationHeight, $white);
        }

$srcImage = ImageManager::create($type, $sourceFile);
        if ($rotate) {
            $srcImage = imagerotate($srcImage, $rotate, 0);
        }

if ($destinationWidth >= $sourceWidth && $destinationHeight >= $sourceHeight) {
            imagecopyresized($destImage, $srcImage, (int) (($destinationWidth - $nextWidth) / 2), (int) (($destinationHeight - $nextHeight) / 2), 0, 0, $nextWidth, $nextHeight, $sourceWidth, $sourceHeight);
        } else {
            ImageManager::imagecopyresampled($destImage, $srcImage, (int) (($destinationWidth - $nextWidth) / 2), (int) (($destinationHeight - $nextHeight) / 2), 0, 0, $nextWidth, $nextHeight, $sourceWidth, $sourceHeight, $quality);
        }
then replace with

Code:
$dest_w = round(($sourceWidth / $sourceHeight) * $targetHeight);
        if ($dest_w > $targetWidth) {
            $dest_w = $targetWidth;
            $dest_h = round(($sourceHeight / $sourceWidth) * $targetHeight);
        } else {
            $dest_h = $targetHeight;
        }

$destImage = imagecreatetruecolor($dest_w, $dest_h);

$srcImage = ImageManager::create($type, $sourceFile);
        if ($rotate) {
            $srcImage = imagerotate($srcImage, $rotate, 0);
        }

imagecopyresampled($destImage, $srcImage, 0, 0, 0, 0, $dest_w, $dest_h, $sourceWidth, $sourceHeight);
This stops prestacart adding white (or transparent) padding to your images, it makes the dimensions you enter into image backend work as CONSTRAINTS, not dimensions to add padding to. Not an edit that should have ever needed to be made..... It also works just fine with the classic theme, with the included CSS keeping everything aligned just fine. So it is still BEYOND me why the images are edited and padding added in the first place.

VERSION 1.7.6.5
 

d-shilko

Well-known member
☆☆ Special ☆☆
☆ Pro ☆
Joined
Jun 10, 2021
Messages
2,587
Reaction score
1,601
Points
113
NullCash
1,081
Prestashop has a setting that doen't make sense, at least for me, that is adding white spaces (or transparency) in images to complete the dimensions. Fortunately there is a simple way to solve it:

Here's a quick fix for this if anyone runs into this issue. Open ImageManager.php in the classes folder, go to line 265 and delete all of this:
Code:
$destImage = imagecreatetruecolor($destinationWidth, $destinationHeight);

// If image is a PNG and the output is PNG, fill with transparency. Else fill with white background.
        if ($fileType == 'png' && $type == IMAGETYPE_PNG) {
            imagealphablending($destImage, false);
            imagesavealpha($destImage, true);
            $transparent = imagecolorallocatealpha($destImage, 255, 255, 255, 127);
            imagefilledrectangle($destImage, 0, 0, $destinationWidth, $destinationHeight, $transparent);
        } else {
            $white = imagecolorallocate($destImage, 255, 255, 255);
            imagefilledrectangle($destImage, 0, 0, $destinationWidth, $destinationHeight, $white);
        }

$srcImage = ImageManager::create($type, $sourceFile);
        if ($rotate) {
            $srcImage = imagerotate($srcImage, $rotate, 0);
        }

if ($destinationWidth >= $sourceWidth && $destinationHeight >= $sourceHeight) {
            imagecopyresized($destImage, $srcImage, (int) (($destinationWidth - $nextWidth) / 2), (int) (($destinationHeight - $nextHeight) / 2), 0, 0, $nextWidth, $nextHeight, $sourceWidth, $sourceHeight);
        } else {
            ImageManager::imagecopyresampled($destImage, $srcImage, (int) (($destinationWidth - $nextWidth) / 2), (int) (($destinationHeight - $nextHeight) / 2), 0, 0, $nextWidth, $nextHeight, $sourceWidth, $sourceHeight, $quality);
        }
then replace with

Code:
$dest_w = round(($sourceWidth / $sourceHeight) * $targetHeight);
        if ($dest_w > $targetWidth) {
            $dest_w = $targetWidth;
            $dest_h = round(($sourceHeight / $sourceWidth) * $targetHeight);
        } else {
            $dest_h = $targetHeight;
        }

$destImage = imagecreatetruecolor($dest_w, $dest_h);

$srcImage = ImageManager::create($type, $sourceFile);
        if ($rotate) {
            $srcImage = imagerotate($srcImage, $rotate, 0);
        }

imagecopyresampled($destImage, $srcImage, 0, 0, 0, 0, $dest_w, $dest_h, $sourceWidth, $sourceHeight);
This stops prestacart adding white (or transparent) padding to your images, it makes the dimensions you enter into image backend work as CONSTRAINTS, not dimensions to add padding to. Not an edit that should have ever needed to be made..... It also works just fine with the classic theme, with the included CSS keeping everything aligned just fine. So it is still BEYOND me why the images are edited and padding added in the first place.

VERSION 1.7.6.5
Your solution works with issue.

I made module with right way with class override.
U can test by yourself.

1636322635325.png
 
Top