Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I generate a thumbnail client-side in a modern browser?

Tags:

html

fileapi

I'm looking for an elegant way to generate a thumbnail for use with the FileAPI. Currently I get a DataURL representing an image. Problem is, if the image is very large, than moving it around and rerendering it becomes CPU intensive. I can see 2 options to get around this.

  • Generate a thumbnail on the client
  • Generate a thumbnail on the server, send the thumbnail back to the client (AJAX).

With HTML5 we have a canvas element? Does anyone know how to use it to generate thumbnails from pictures? They don't have to be perfect -- sampling quality is acceptable. Is there a jQuery plugin that will do this for me? Are there any other way to speed up the clientside use of large images?

I'm using HTML5, and Firefox 3.6+: there is no need to support anything other than Firefox 3.6+, please don't provide suggestions for IE 6.0

like image 412
NO WAR WITH RUSSIA Avatar asked Jul 12 '10 19:07

NO WAR WITH RUSSIA


People also ask

How do I get the thumbnail image from a website?

you can use something like urlbox.io - screenshot service API. Its got easy to integrate API and generates thumbnails as per your requirement.

What is thumbnail generation?

You can create thumbnail images for content elements either by requesting an image for a particular content element or by using event-based generation or sweep-based generation to have images generated automatically. You can store, generate, and manage document thumbnails.

How does thumbnail help in creating Web pages?

A thumbnail gallery is similar to a slideshow in that it displays multiple images on one page, alternating between them in a way that highlights one image at a time. The difference is the visitor gets a preview of the images in a horizontal strip of thumbnails underneath the main image.


2 Answers

Here’s what you can do:

function getThumbnail(original, scale) {
  var canvas = document.createElement("canvas");

  canvas.width = original.width * scale;
  canvas.height = original.height * scale;

  canvas.getContext("2d").drawImage(original, 0, 0, canvas.width, canvas.height);

  return canvas
}

Now, to create thumbnails, you simply do the equivalent of this:

var image = document.getElementsByTagName("img")[0];
var thumbnail = getThumbnail(image, 1/5);

document.body.appendChild(thumbnail);

Note: Remember to make sure that the image is loaded (using onload) before trying to make a thumbnail of it.

like image 180
Daniel Brockman Avatar answered Sep 19 '22 13:09

Daniel Brockman


Okay, the way I can see this working is drawing the image to the canvas at a smaller size, then exporting the canvas. Say you want a 64px thumbnail:

var thumbSize = 64;
var canvas = document.getElementById("canvas");
canvas.width = thumbSize;
canvas.height = thumbSize;
var c = canvas.getContext("2d");
var img = new Image();
img.onload = function(e) {
    c.drawImage(this, 0, 0, thumbSize, thumbSize);
    document.getElementById("thumb").src = canvas.toDataURL("image/png");
};
img.src = fileDataURL;

With this code, an image element with the id "thumb" is used as the thumbnail element. fileDataURL is the data URL that you got from the file API.

More information on drawing images to the canvas: http://diveintohtml5.info/canvas.html#images

And on exporting canvas data: http://msdn.microsoft.com/en-us/library/ie/ff975241(v=vs.85).aspx

like image 34
Colin Atkinson Avatar answered Sep 17 '22 13:09

Colin Atkinson