- Posted On 29 August 2013
- By
- In Programming
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.
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.
- Tags :
- ASP.NET
- jQueryJavascript
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.
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.