JavaScript Image Resize: Rescale images smoothly using HTML canvas objects

Recommend this page to a friend!
  Info   Example   Demos   Screenshots   View files Files   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 188 All time: 269 This week: 4Up
Version License JavaScript version Categories
resizer 1.0.3MIT/X Consortium ...3.0HTML, Graphics
Description 

Author

This object can rescale images smoothly using HTML canvas objects.

It can take an image given the image object or a selector string of the image element to rescale.

The object can create a canvas to render the image rescaled to a given ratio or to a given size optionally preserving the original image proportions.

The image can be rescaled smoothly if the browser supports the imageSmoothingEnabled property. The smoothing level is configurable.

The object calls a given callback function when the rescaling is done passing the canvas object that contains the rescaled image.

Innovation Award
JavaScript Programming Innovation award winner
June 2015
Winner


Prize: One downloadable e-book of choice by O'Reilly
One way to rescale images on a browser is to use canvas objects and create copies of the original images at different sites.

Modern browsers support a property named imageSmoothingEnabled that can be used to create smooth versions of rescaled images. That property may have a slightly different name depending on the browser type.

This object can create smooth rescaled images on several different browsers that support that feature.

Manuel Lemos
Picture of Andras Toth
  Performance   Level  
Name: Andras Toth <contact>
Classes: 22 packages by
Country: Hungary Hungary
Age: 51
All time rank: 31 in Hungary Hungary
Week rank: 1 Up
Innovation award
Innovation award
Nominee: 15x

Winner: 9x

Example

<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="description" content="Best quality image Downscaling with Canvas"> <meta name="author" content="Tóth András"> <title>Resize</title> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <h1 class="page-header text-center">Best quality image Downscaling with Canvas</h1> <div class="container" style="width: 100%;"> <div class="row text-center"> <div class="col-sm-12 col-md-4"> <div class="thumbnail"> <div class="well" id="source"> <img width="300" height="450" src="img.jpg"> </div> <div class="caption"> <h3>Original image</h3> <p class="text-left">Browser resized</p> </div> </div> </div> <div class="col-sm-12 col-md-4"> <div class="thumbnail"> <div class="well" id="normal"> <canvas></canvas> </div> <div class="caption"> <h3>Resized canvas</h3> <p class="text-left">Normal resized</p> </div> </div> </div> <div class="col-sm-12 col-md-4"> <div class="thumbnail"> <div class="well" id="target"> </div> <div class="caption"> <h3>Resized canvas</h3> <p class="text-left">Resizer resized - level Auto</p> <hr> <p><a class="btn btn-primary" role="button" onclick="downloadImage(document.querySelector('#target').querySelector('canvas'), 'down.png', 'image/png');">Download</a></p> </div> </div> </div> </div> </div> <script src="resizer.js"></script> <script type="text/javascript"> var args = { source: '#source img', // string (querySelector) or html img element keepRatio: true, // boolean height: 450, // int width: 450, // int level: 0, // int 0-9 /* 0: AUTO, 1: SHARP_HIGH, 2: SHARP_MEDIUM, 3: SHARP_LOW, 4: OPTIMAL_LOW, 5: OPTIMAL_MEDIUM, 6: OPTIMAL_HIGH, 7: SMOOTH_LOW, 8: SMOOTH_MEDIUM, 9: SMOOTH_HIGH */ smoothEnabled: true, // boolean }; function resultCallBack(res) { // res.source - source img element // res.canvas - resized canvas // res.dataURL - image data // res.width - resized width // res.height - resized height // res.startTime // res.endTime var img = document.querySelector('#source img'); img.width = res.width; img.height = res.height; document.querySelector('#target').appendChild(res.canvas); // normal canvas resizeing var can = document.querySelector('#normal canvas'); can.width = res.width; can.height = res.height; var cx = can.getContext("2d"); cx.drawImage(img, 0, 0, res.width, res.height); }; function downloadImage(item, filename, type, quality) { item = typeof item === 'string' ? document.querySelector(item) : item; type = !!type ? type : 'image/png'; quality = !!quality ? quality : 1.0; var canvas = item; if (item.tagName.toLowerCase() !== 'canvas') { canvas = document.createElement('canvas'); canvas.width = item.naturalWidth; canvas.height = item.naturalHeight; canvas.getContext('2d').drawImage(item, 0, 0, item.naturalWidth, item.naturalHeight); } var a = window.document.createElement('a'); a.href = canvas.toDataURL(type, quality); a.style.display = 'none'; a.download = filename; document.querySelector('body').appendChild(a); a.click(); a && a.parentNode && a.parentNode.removeChild(a); } window.onload = function (){ new Resizer().Init(args, resultCallBack); }; </script> </body> </html>

  demoExternal page  
Screenshots (2)  
  • arion_12000x9000.png
  • Capture.png
  Files folder image Files (5)  
File Role Description
Accessible without login Image file img.jpg Icon image
Accessible without login Plain text file index.html Example presentation
Plain text file resizer.js Class class

 Version Control Unique User Downloads Download Rankings  
 0%
Total:188
This week:0
All time:269
This week:4Up