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.


Top 10 Visual Studio things which can boost developers coding speed

Visual Studio 2012 provides some coding features by which you can code faster if use them properly. This post will cover top 10 things among them to boost your development speed.


Visual Studio 2008 Shell and TFS integration

Visual Studio 2008 Shell and TFS integration is the problem for all newbies of BIDS and TFS. Here is the solution.


How to call click or any event only once in jQuery

Know how to execute an click event or any event only once for any element in jQuery. Perform action only once and even not required to unbind event.


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!
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