Jump to content

Why add white space to images when you could just use CSS???


d4n

Recommended Posts

I've run into a huge issue with prestashop (i'm a new user, and probably wont ever be again....) and moving thousands of images from a previous e-commerce platform. The issue is prestacart adding white to images so they fit to whatever size you enter in the image panel. It makes less than zero sense. It is idiotic to edit the image itself when you could easily add white padding with CSS. 

I'm going to have to hack apart the ImageManager.php to fix this monumental screw up on the developers behalf. I could understand if the dimensions you entered into the image admin "large_default" 800 x 800, was the maximum "ratio" that an image was fit to... but adding white to make the image 800x800 is just incredibly stupid. Considering how modular the rest of prestashop is, the image manager should not add anything to images that are uploaded, it should be done with CSS.

Rant over.

Link to comment
Share on other sites

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: 
 

$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

 

        $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

Edited by d4n
version added (see edit history)
Link to comment
Share on other sites

  • 2 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...