JavaScript Client Side Image Thumbnailing

First of all, the Quality of the thumbnail is quite bad, at least by now (just tested in firefox). Furthermore it’s a bad idea to do thumbnailing on the clientside but it works.

function makeThumb(src, canvas, width, height) {
  canvas.width = width;
  canvas.height = height;
  var ctx = canvas.getContext('2d');
  ctx.scale(
    width / src.width,
    height / src.height
  );
  ctx.drawImage(src, 0, 0);
}

src must be an Image, canvas a canvas element and width/height is the size of the resulting thumbnail. If you want to get the image data of the thumbnail for saving on the server or whatever, you can do canvas.toDataURL() which returns the data of the image as base64 version of a png.

Leave a Reply

cogitations driven by wordpress