I haven’t wrote here for a while so I decided to write about something more practical. Actually the idea for the post come from one my response in stackoverflow. The post is connected with pure client side image scaling. While you’re reading the example I hope that you’ll see how powerful is the HTML5 API and how with few lines of code you can do something which in other technologies is much more complex.
So firstly I’ll post the source code with the example in JSFiddle, after that I’m going to explain everything step by step.
The HTML form
So let’s begin with the HTML. I’ve created a simple form with a file input, a table which contains two fields required for setting the size of the image, a canvas used for the image scaling and a save button. I don’t think that there’s something more to explain here…
Let’s first look at the handleFileSelect. When a file is selected I get the file (
) and check for it’s type (actually I check it’s mime type). If the file is not an image I simply log an error and stop the function execution. Otherwise (if the file is an image) I create new FileReader. As you might guess from it’s name it’s responsible for reading the file. With the line:
I read the file. Here the file won’t be read in binary but in base64 encoding instead. Because the file loading is asynchronous (imagine we want to load 1 GB file and the loading was synchronous, our application would block until the file is completely loaded…) that’s why I’ve added callback which will be invoked when the file is being read. In this callback I set the currentImage to the loaded image and call the renderImage method.
Now let’s look into the renderImage. The renderImage method already has access to the currentImage so it just creates new img element (that’s because the drawImage method accepts as first argument img element) with src the base64 encoded image. On load of the created img element I render it on the canvas, using it’s 2d context.
Now let’s look at the handleSizeChanged. This method changes the size of the canvas on change of any of the text inputs. On keyup of a text input handleSizeChanged is being fired. In handleSizeChanged I first get the input id (because I’m lazy aand I don’t want to look at two cases - width/height). After that I validate the input id - it’s valid only if it’s value is width or height (who knows may be someone has changed the element’s id). After that if the value of the field is a text (with the regex
I check that) I set the size of the canvas.
The save of the image is that simple:
And that’s all. Simple isn’t it?
I hope that this post was funny and useful. The next post I plan is going to be much more exited and cool…just wait for it…