Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including emozjpeg with all npm packages installed. Try it out:

var emozjpeg = require("emozjpeg")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

emozjpeg v0.0.3

Emscripten mozjpeg port

Emscripten mozjpeg port

Port of MozJPEG project to Emscripten.

Browser usage

Example using Browserify tool:

# import encoder
{encode} = require "emozjpeg"

# create image object
image = new Image

image.onload = ->
  # get image size
  {width, height} = image

  # create canvas and set size
  canvas = document.createElement "canvas"
  canvas.width = width
  canvas.height = height

  # get drawing context
  context = canvas.getContext "2d"
  # draw image to canvas
  context.drawImage image, 0, 0, width, height

  # get raw image data
  {data} = context.getImageData 0, 0, width, height

  # encode image
  result = encode {data, width, height, quality, sampling, flags}

  # load data to blob
  blob = new Blob [result], type: 'text/html'
  # create object url
  url = URL.createObjectURL blob

  # load encoded image
  image.src = url

# load original image
image.src = "path/to/originalImage.jpg"

NodeJS usage

npm install emozjpeg
# import encoder
{encode} = require "emozjpeg"

# import file utils
{readFileSync, writeFileSync} = require "fs"

# import canvas library
Canvas = require "canvas"
{Image} = Canvas

# create image
image = new Image
# load image
image.src = readFileSync "originalImage.jpg"

# get image size
{width, height} = image

# create canvas and set size
canvas = new Canvas 100, 100
canvas.width = width
canvas.height = height

# get drawing context
context = canvas.getContext "2d"
# draw image to canvas
context.drawImage image, 0, 0, width, height

# get raw image data
{data} = context.getImageData 0, 0, width, height

# encode image
result = encode {data, width, height, quality, sampling, flags}

# write result to file
writeFileSync "convertedImage.jpg", result

Command-line tool

usage: convert [options] <source>

 <source>                                                source image

 -f, --flags=BOTTOMUP,FASTUPSAMPLE,FASTDCT,ACCURATEDCT   coma-separated conversion flags (default: none)
 -h, --help                                              display help & usage
 -o, --output=IMAGE                                      output image
 -q, --quality=PERCENT                                   jpeg quality (default: 70)
 -s, --sampling=444,422,420,GRAY,440,411                 conversion sampling (default: 444)
 -v, --version                                           display cli name & version

 Convert images using mozjpeg

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free