Subscribe - It's FREE!!

Stay Connected Here

Stay Updated With Us Here



Google

How to create thumbnail image using HTML5 Canvas,Javascript


Share with WhatsApp


IMInfo.in - Example of creating Thumbnail Image using HTML5 Canvas


I have recently started using HTML5 Canvas in one of my project and at the task of creating thumbnail images. As you know there are many articles regarding the same but the problem was my PM was in demand that when we actually save the created thumbnail image it must get saved as thumbnail image only , not the origional.
In many of these examples we are getting thumbnails but it just minimizes height and width. So As I already spent more hours to get this done I thought better to share this example with all of you. Hope this will help you.

Recommended Book "JavaScript and JQuery: Interactive Front-End Web Development" to learn how to make your websites more interactive and your interfaces more interesting and intuitive (From Basic to Advanced)

HTML :

<!DOCTYPE HTML>
<html>
<head>
<title>Get Thumbnail Image : Iminfo.in</title> 
</head> 
<body> 
<label> Thumbanil Size: </label> 
<input id="txtThumbSize" type="text" style="width: 40px" value="64"/> px<hr />
<label> Select File To Create Thumbnail : </label>
<input type="file" id="input" multiple onchange="thumb(this.files)"> <hr/> 
</body>
</html>  

Add following script just before closing body tag in above code.

<script type="text/javascript">
    function thumb(files) {

        if (files == null || files == undefined) {
            document.write("This Browser has no support for HTML5 FileReader yet!");
            return false;
        }

        for (var i = 0; i < files.length; i++) {
            var file = files[i];
            var imageType = /image.*/;

            if (!file.type.match(imageType)) {
                continue;

            }

            var reader = new FileReader();

            if (reader != null) {

                reader.onload = GetThumbnail;
                reader.readAsDataURL(file);
            }


        }
    }
    
    function GetThumbnail(e) {
        var myCan = document.createElement('canvas');
        var img = new Image();
        img.src = e.target.result;
        img.onload = function () {

            myCan.id = "myTempCanvas";
            var tsize = document.getElementById("txtThumbSize").value;
            myCan.width = Number(tsize);
            myCan.height = Number(tsize);
            if (myCan.getContext) {
                var cntxt = myCan.getContext("2d");
                cntxt.drawImage(img, 0, 0, myCan.width, myCan.height);
                var dataURL = myCan.toDataURL();


                if (dataURL != null && dataURL != undefined) {
                    var nImg = document.createElement('img');
                    nImg.src = dataURL;
                    document.body.appendChild(nImg);

                }
                else
                    alert('unable to get context');

            }

        }

    }
</script>

 

In above example, I have used HTML5's FileReader which actually calls "GetThumbnail" function and shows created thumbnail image.

To create thumbnail canvas element gets created on the runtime with a desired thumbnail size and origional image gets drawn on it with thumbnail size. And then we have created new image object and assigned newly drawn canvas image to it which is the actual thumbnail image.

Its done now. we have our thumbnail image. you can check it by right click on it and "View Image".
 

Check live demo below :




You will see we have not added canvas element to our DOM. we have just used it to create thumbnail image.

Hope you all have benefited by reading this post. Feel free to share your views and any other efficient way of doing the same in comment section below.

 



If you enjoyed this post take 5 seconds to share it! Be Socialable. :-)

Share with WhatsApp

Posts To Read Next

Advanced .NET Interview Questions - Garbage Collection - Part 1

This post contains some advanced .NET Interview Questions on the topic Garbage Collection.


Advanced .NET Interview Questions - Garbage Collection - Part 2

This post contains some advanced .NET Interview Questions on the topic Garbage Collection.


Typewriter - Automatic TypeScript Template generation from C# classes in Visual Studio

Typewriter, an extension for visual studio that generates TypeScript files from C# code files using TypeScript templates. Convert your C# model classes to TypeScript automatically.


Swagger - Powerful Opensource Framework for RESTful API

With a Swagger-enabled API, you get interactive documentation, client SDK generation and discoverability. Swagger Editor allows you to generate code, documentation with ease.


Assembla - Free and private repository to manage your source code online with SVN subversion hosting

With Assembla you can share source code with others online. Free & Private source code repository with SVN Subversion, Git & Perforce Hosting.


Your opinion is valuable for us! Comments, suggetions are welcome.


Submit your Email Id to stay updated with us and get notified with our new posts. It's FREE!
Vu 32'' TV Vu 24'' TV
We know this popup is disturbing you!
But We would greatly appreciate if you share us with your friends below!

It will not take more than 2 seconds but will motivate us greatly to write more,share more!

x