EyalAr / lwip

Light Weight Image Processor for NodeJS
MIT License
2.36k stars 230 forks source link

canvas decimals #177

Open ChiefORZ opened 9 years ago

ChiefORZ commented 9 years ago

Hey,

i just came across the bug, that evry image i saves was hieroglyphic. for example the favicon of Google's new Logo

I fixed this bug debugging my code this whole morning. So, i open locally stored images using lwip and calculating the scaling - so that every image gets scaled to a maximum 300x300 square thumbnail. After that part, i create a blank canvas with white background and paste my scaled image on top of that, to prevent black background on transparent images and save it locally.

lwip.open(imagePath, function(err, image) {
    var ratio        = 300 / Math.max(image.width(), image.height()),
        scaledHeight = image.height() * ratio,
        scaledWidth  = image.width() * ratio;
    image.scale(ratio, function(err, image) {
        // create a blank canvas with the same dimensions as our image
        lwip.create(scaledWidth, scaledHeight, 'white', function(err, canvas) {
            // paste original image on top of the canvas
            canvas.paste(0, 0, image, function(err, image) {
                // save it locally
            });
        });
    });
});

but i discovered, that lwip.create does not support float numbers (decimal numbers), it only works with Integers... so a workaroud for me was to use Math.floor before creating the white canvas.

I know it's specific, but i just want it to be documented... if someone else stumbles upon the same bug.

EyalAr commented 9 years ago

Thanks @ChiefORZ Note that the docs state that the dimensions must be integers. But nevertheless it should probably throw an exception when non integers are passed.