All Projects → ytiurin → downscale

ytiurin / downscale

Licence: MIT License
Better image downscale with canvas.

Programming Languages

javascript
184084 projects - #8 most used programming language
HTML
75241 projects
Makefile
30231 projects

Projects that are alternatives of or similar to downscale

Sharp
High performance Node.js image processing, the fastest module to resize JPEG, PNG, WebP, AVIF and TIFF images. Uses the libvips library.
Stars: ✭ 21,131 (+26313.75%)
Mutual labels:  resize, png, jpeg, crop, sharp
Gimage
A PHP library for easy image handling. 🖼
Stars: ✭ 148 (+85%)
Mutual labels:  resize, png, scale, crop
Bimg
Go package for fast high-level image processing powered by libvips C library
Stars: ✭ 1,394 (+1642.5%)
Mutual labels:  resize, png, jpeg, crop
Ucrop
Image Cropping Library for Android
Stars: ✭ 11,003 (+13653.75%)
Mutual labels:  scale, crop, photo
pixl
🚀 Lightweight image processing library in C++11
Stars: ✭ 31 (-61.25%)
Mutual labels:  resize, png, jpeg
jimp-compact
✏️ Lightweight version of Jimp -- An image processing library written entirely in JavaScript for Node.js
Stars: ✭ 55 (-31.25%)
Mutual labels:  resize, png, jpeg
Ananas
An easy image editor integration for your Android apps.
Stars: ✭ 186 (+132.5%)
Mutual labels:  canvas, crop, photo
Gallery shell
📷 Bash Script to generate static responsive image web galleries.
Stars: ✭ 198 (+147.5%)
Mutual labels:  thumbnails, jpeg, photo
Flyimg
Dockerized PHP7 application runs as a Microservice to resize and crop images on the fly. Get optimised images with MozJPEG, WebP or PNG using ImageMagick. Includes face detection, cropping, face blurring, image rotation and many other options. Abstract storage based on FlySystem in order to store images on any provider (local, AWS S3...).
Stars: ✭ 762 (+852.5%)
Mutual labels:  png, jpeg, crop
Mort
Storage and image processing server written in Go
Stars: ✭ 420 (+425%)
Mutual labels:  resize, png, jpeg
Php Image Resize
PHP library to resize, scale and crop images. Cloud solution available at:
Stars: ✭ 955 (+1093.75%)
Mutual labels:  resize, scale, crop
Lilliput
Resize images and animated GIFs in Go
Stars: ✭ 1,690 (+2012.5%)
Mutual labels:  png, jpeg, crop
Lipo
👄 Free image manipulation API service built on top of Sharp (an alternative to Jimp, Graphics Magic, Image Magick, and PhantomJS)
Stars: ✭ 101 (+26.25%)
Mutual labels:  resize, scale, crop
wa-sticker-formatter
Sticker Creator for WhatsApp
Stars: ✭ 60 (-25%)
Mutual labels:  png, sharp
scale.js
High-quality scale function for canvas and image element. If scale factor < 1, then algorithm for downscale (area average) is used, other than that, bicubic algorithm is used.
Stars: ✭ 45 (-43.75%)
Mutual labels:  scale, downscale
PNG-Upscale
AI Super - Resolution
Stars: ✭ 116 (+45%)
Mutual labels:  resize, png
vue-responsive-text
↔ Vue component that scales its child node in relation to its parent node's width
Stars: ✭ 23 (-71.25%)
Mutual labels:  resize, scale
zImageOptimizer
Simple image optimizer for JPEG, PNG and GIF images on Linux, MacOS and FreeBSD.
Stars: ✭ 108 (+35%)
Mutual labels:  png, jpeg
stbi-sharp
C# wrapper around stb_image.h and qoi.h
Stars: ✭ 17 (-78.75%)
Mutual labels:  png, jpeg
image-php
很多PHP框架竟然没有图片设定宽高居中剪裁的功能,比如CodeIgniter,所以我自己封装了一个图片处理类:可设定宽高居中剪裁、设定宽高等比缩放、创建缩略图
Stars: ✭ 17 (-78.75%)
Mutual labels:  resize, crop

Better image downscale with canvas (demo)

This function downscales images in the browser, producing a better quality result, than the traditional CanvasRenderingContext2D.scale() method. It neutralises the "fuzzy" look caused by the native canvas downsampling, when processing relatively large images like photos taken with a smartphone. Check the demo page.

Better image downscale demo

Motivation

While other image resizing libraries are based on complex interpolation algorithms such as Lanczos resampling, image downscaling usually doesn't require that complexity, because there is no interpolation happening (in other words we don't create new pixels).

On the other hand, browsers implement very fast HTMLCanvasElement downsampling, when the pixel from source position is directly transfered to the destination position, loosing all the neighbouring pixels information. The resulting image may often look very noisy.

To resolve this problem, the proposed function does a simple area-average downsampling, producing preferable results with relatively small processing time.

Performance

This function uses the technique, proposed by Paul Rouget in his article about pixel manipulation with Typed Arrays. His method reduces the number of read/write operations to the ArrayBuffer of the ImageData returned by the CanvasRenderingContext2D.getImageData() method. This saves overall processing time when you want to iterate through every pixel of the source image.

Also, the usage of Math.round() method is avoided in favour of Bitwise operators, giving a significant boost in performance in some browsers.

Image cropping

Image cropping is very often used in pair with resizing, but both can be very naturally combined. As we don't need to iterate through pixels in cropped areas, the function does both downscaling and cropping in range of the same processing loop. This saves some memory and processing time.

By default, the source image is cropped in the way, that the center of the source image is transfered to the resulting image.

Rollback to canvas resizing

The function also uses basic canvas resizing method when the scale factor of the resulting image is greater than 0.5x. So the better downscaling happen only when the resulting image is at least 2 times smaller than the initial image. In other cases basic canvas resizing gives better image quality result.

Install

npm install downscale

Syntax

Promise<DOMString> downscale(source, width, height[, options]);

Parameters

source
Defines the source of the image data to downscale. This can either be:
width
A Number indicating width of the resulting image. If the value is 0, the width is adapted to keep the same aspect ratio as in the source image.
height
A Number indicating height of the resulting image. If the value is 0, the height is adapted to keep the same aspect ratio as in the source image.
options (optional)
An object with properties representing optional function parameters:
  • imageType
    A DOMString indicating image format. Possible values are jpeg, png, webp. The default format type is jpeg.
  • quality
    A Number between 0 and 1 indicating image quality if the requested imageType is jpeg or webp. The default value is 0.85.
  • returnBlob
    A Boolean indicating if the returned Promise should resolve with Blob object representing the resulting image. The default value is false.
  • returnCanvas
    A Boolean indicating if the returned Promise should resolve with HTMLCanvasElement containing the resulting image. The default value is false.
  • sourceX
    A Number indicating distance from the left side of the source image to draw into the destination context. This allows to crop the source image from the left side. The default value is calculated to centralize the destination rectangle relatively to the source canvas.
  • sourceY
    A Number indicating distance from the top side of the source image to draw into the destination context. This allows to crop the source image from the top side. The default value is calculated to centralize the destination rectangle relatively to the source canvas.

Return value

A Promise that resolves to a DOMString containing the resulting image in data URI format.

Examples

Send image data with <form>

This is just a simple code snippet which uses the form file input as a source of the image data.

HTML

<input type="file" accept="image/*" onchange="filesChanged(this.files)" multiple />
<form method="post"><input type="submit"/></form>

Javascript

function filesChanged(files)
{
  for (var i = 0; i < files.length; i++) {
    downscale(files[i], 400, 400).
    then(function(dataURL) {
      var destInput = document.createElement("input");
      destInput.type = "hidden";
      destInput.name = "image[]";
      destInput.value = dataURL;
      // Append image to form as hidden input
      document.forms[0].appendChild(destInput);
      // Preview image
      var destImg = document.createElement("img");
      destImg.src = dataURL;
      document.body.appendChild(destImg);
    })
  }
}

Send image data with FormData

You can use even cleaner FormData interface to send pure blob data to the server.

HTML

<input type="file" accept="image/*" onchange="filesChanged(this.files)" multiple />
<button onclick="submitForm()">Submit form data</button>

<div id="previews"></div>

Javascript

var formData = new FormData();
var URL = window.URL || window.webkitURL;

function filesChanged(files)
{
  for (let i = 0; i < files.length; i++) {
    downscale(files[i], 400, 400, {returnBlob: 1}).
    then(function(blob) {
      // Append image to form as a blob data
      formData.append("userpic[]", blob, files[i].name);
      // Preview image
      var destImg = document.createElement("img");
      destImg.src = URL.createObjectURL(blob);
      document.body.appendChild(destImg);
    })
  }
}

function submitForm()
{
  var request = new XMLHttpRequest();
  request.open("POST", "http://foo.com/submitform.php");
  request.send(formData);
}

Resize <img> element

Processing an <img> element is quite simple. The function will wait for image load, so you don't have to worry about it.

HTML

<img id="source" src="../public/1.jpg" />

Javascript

var sourceImg = document.getElementById('source');

downscale(sourceImg, 400, 400).
then(function(dataURL) {
  var destImg = document.createElement('img');
  destImg.src = dataURL;
  document.body.appendChild(destImg);
})

Load image from URL

The function can upload the source image from the given URL with no extra code needed. Keep in mind that the image should share origin with the code file.

var imageURL = "/public/1.jpg";

downscale(imageURL, 400, 400).
then(function(dataURL) {
  var destImg = document.createElement('img');
  destImg.src = dataURL;
  document.body.appendChild(destImg);
})

Other libraries

Check out other great in-browser image resizing libraries:

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].