• Stars
    star
    168
  • Rank 225,507 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 13 years ago
  • Updated almost 10 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

On-demand image manipulation middleware for express and connect.

What is imageable doing?

You can put imageable into your express application, where it acts as middleware. Calling specific URLs (look below) will return resized and cropped images.

What will I need?

Besides Node.JS you will need imagemagick and eventually some other libs for image format handling. On a mac this is a fairly easy mission:

brew install imagemagick

How to get it run.

In your express app you will find a section like this:

app.configure(function(){
  app.use(express.bodyParser())
  app.use(express.methodOverride())
  //...
  app.use(app.router)
})

To plug imageable into your app, just add the following ABOVE the router:

app.use(imageable(config, options))

Here is what an app should look like:

app.js

var fs     = require("fs")
  , config = JSON.parse(fs.readFileSync(__dirname + "/config/config.json"))

app.configure(function(){
  app.use(express.bodyParser())
  app.use(express.methodOverride())
  app.use(imageable(config, {
    before: function(stats) { console.log('before') },
    after: function(stats, returnValueOfBefore, err) { console.log('after') }
  }))
  app.use(app.router)
})

Notice that the after-callback will have the returned value of the before-callback as second parameter. Using that mechanism, you can for example track custom statistics. The third parameter is furthermore containing an error if something went wrong. Nevertheless the error gets thrown to end the request.

The middleware will automatically track some statistics, such as request, average processing time etc. You can access them via the stats parameter.

config/config.json

{
  "secret":       "my-very-secret-secret",
  "magicHash":    "magic",
  "namespace":    "",
  "maxListeners": 512,
  "imageSizeLimit": 1024,
  "timeouts": {
    convert: 5000,
    identify: 100,
    download: 1000
  }
  "reporting": {
    "interval": 10,
    "commands": [
      'curl -s http://foo.bar.org/report?avg=%{avg}',
      'curl -s http://foo.bar.org/report?count=%{count}'
    ]
  },
  "whitelist": {
    "allowedHosts": [".*google\.com", ".*facebook\.com"],
    "trustedHosts": [".*google\.com"]
  },

  // -- keepDownloads --
  // Don't delete downloaded files but use them when the same url is requested twice.
  // This can be pretty handy if you don't have a CDN in front of the resizer.
  // Make sure that you always have enough HDD space !
  //
  // default: false
  // added in: v0.10.0
  "keepDownloads": true,

  // -- maxDownloadCacheSize --
  // If you keep downloads you would most probably want to limit the size
  // of the download folder. Otherwise you will screw up your hard drive.
  // Once the specified limit is reached node-imageable will start to delete
  // the oldest files (20% of the existing files) in the download folder.
  // The value is the number of megabytes you want to allow.
  //
  // tl;dr;
  // Depending on the size of the source images, it might be not a good idea
  // to set this value to less than 100. That's based on the fact that the
  // server will only have a few files in the tmp folder and that deleting
  // 20% of them will still result in a greater amount of used hdd space.
  //
  // default: null
  // added in: v0.10.0
  "maxDownloadCacheSize": 1000,

  // -- tmpPathRoot --
  // The folder you want to store the downloaded files in.
  //
  // default: /tmp/node-image-imagick
  // added in: v0.10.0
  "tmpPathRoot": process.cwd() + '/tmp'
}

The reporting config defines an array of commands which will get executed each interval seconds.
The whitelist config allows you furthermore to limit the hosts of image sources (allowedHosts) or to trust specific hosts (trustedHosts), so you don't have to specify the hash of the params (see below).
The imageSizeLimit config can be used to specify a dimension limit. Setting it to 1024 will throw an error for requests like 1500x900.
The timeouts config can be used to cancel slow downloads, long running converts or identify commands. Values are in milliseconds.
You can also take a look at https://github.com/sdepold/node-imageable-server to get a further clue.

Routes and namespacing

Here you can see the routes, imageable reacts on.

# resize an image with keeping ratio
http://localhost:3000/resize?url=http%3A%2F%2Fwww.google.com%2Fintl%2Fen_ALL%2Fimages%2Flogo.gif&size=200x200

# resize an image to given size without keeping the ratio
http://localhost:3000/fit?url=http%3A%2F%2Fwww.google.com%2Fintl%2Fen_ALL%2Fimages%2Flogo.gif&size=200x200

# crop a specific area of an image
http://localhost:3000/crop?url=http%3A%2F%2Fwww.google.com%2Fintl%2Fen_ALL%2Fimages%2Flogo.gif&crop=200x200%2B20%2B40

# crop a specific area and resize it with keeping the ratio
http://localhost:3000/crop?url=http%3A%2F%2Fwww.google.com%2Fintl%2Fen_ALL%2Fimages%2Flogo.gif&crop=200x200%2B20%2B40&size=100x50

# if secret is provided in config/config.json, all urls must have a hash following the resize method (see Hashing)
http://localhost:3000/fit/-hash-?url=http%3A%2F%2Fwww.google.com%2Fintl%2Fen_ALL%2Fimages%2Flogo.gif&size=200x200

# use the magic hash code that is valid for all requests, e.g. for testing
http://localhost:3000/fit/-magic-hash-?url=http%3A%2F%2Fwww.google.com%2Fintl%2Fen_ALL%2Fimages%2Flogo.gif&size=200x200

# append any fancy name for nice looking urls and image downloads
http://localhost:3000/fit/-hash-/Fancy-Ignored-Name.gif?url=http%3A%2F%2Fwww.google.com%2Fintl%2Fen_ALL%2Fimages%2Flogo.gif&size=200x200

# this is super edgy, but you can use it if you need it: scale the cropping clip according to another image dimension.
# this will calculate the ratio between the other image dimension and the passed (via url param) image's dimension and scale the cropping information (crop param)
http://localhost:3000/crop?url=http%3A%2F%2Fwww.google.com%2Fintl%2Fen_ALL%2Fimages%2Flogo.gif&crop=200x200%2B20%2B40&size=100x50&cropSourceSize=500x

If you specify the namespace in your config (let's say to 'imageable'), all routes will be scoped to /imageable/fit... or /imageable/crop and so on.

Hashing

To make sure nobody missuses your image-server you can enable hashing in the config/config.json and all requests must be hashed.

# Node
var crypto    = require("crypto")
  , srcImgUrl = encodeURIComponent("http://www.google.com/intl/en_ALL/images/logo.gif")
  , query     = 'url=' + srcImgUrl + '&size=200x200'
  , hash      = crypto.createHash('md5').update(query + config.secret).digest("hex").slice(0,8)
  , url       = "http://localhost:3000/resize/" + hash + "/Very-Nice-Image.gif?" + query

res.send('<img src="' + url + '" />')


# RUBY
require 'digest/md5'

def resized_image_tag(url, size)
  query_options = {:url => url, :size => size}
  digest        = Digest::MD5.new
  hash          = digest.hexdigest(query_options.to_query + CFG[:node_imageable_secret])[0..7]

  image_tag "http://localhost:3000/resize/#{hash}/Very-Nice-Image.gif?#{query_options.to_query}"
end

resized_image_tag("http://www.google.com/intl/en_ALL/images/logo.gif", "200x200")

Running the tests

npm test

TODO

  • add 'expand' which would add whitespace to fill missing image areas

Authors/Contributors

More Repositories

1

jquery-rss

An easy-to-use rss plugin for jquery with templating.
JavaScript
451
star
2

rsvm

Rust Version Manager
Shell
128
star
3

feedrapp

A service for parsing RSS and Atom feeds.
JavaScript
44
star
4

node-imageable-server

On-demand image manipulation server in node.js
JavaScript
40
star
5

sequel-paranoid

A plugin for the Ruby ORM Sequel, that allows soft deletion of database entries.
Ruby
28
star
6

vanilla-rss

An easy-to-use vanilla JavaScript library to read and render RSS feeds.
JavaScript
22
star
7

octo.erl

Erlang wrapper for the Github API.
Erlang
16
star
8

sequel-bit_fields

A sequel plugin that allows the use of integer columns to represent a big number of flags.
Ruby
13
star
9

connect-form

urlencoded / multipart form parsing middleware for Connect
JavaScript
11
star
10

asteroids-workshop

Building Asteroids in JavaScript
JavaScript
7
star
11

release-tools

A tiny collection of release helpers.
JavaScript
7
star
12

jquery.google-search

Execute google searches within your client-side javascript
JavaScript
6
star
13

cubi

JavaScript
6
star
14

noegnud

Entry for 2019's js13kGames
JavaScript
5
star
15

contentful-ghost-importer

A CLI that imports data from ghost to contentful.
JavaScript
5
star
16

contentful_blog

Demo project that shows how to use Contentful as backend for a blog.
Ruby
5
star
17

jquery.skeleton

A skeleton builder for jquery plugins. Included is BusterJS for testing and Google Closure Compiler for minification.
JavaScript
5
star
18

simpleSli.de

a jQuery slideshow plugin
4
star
19

Autocompleter.Local.MultiContent

Extension of the script.aculo.us Autocompleter.Loca for connecting a selection with a data set.
JavaScript
3
star
20

gulp-busterjs

BusterJS runner for gulp
JavaScript
3
star
21

myspaceid-sdk

Improved copy of the myspaceid-ruby-sdk
Ruby
3
star
22

node-nmagick

A node wrapper for ImageMagick
3
star
23

node-security-brown-bag

Scenarios for the brown bag session about secure node applications
JavaScript
3
star
24

bugs-bunny

A round-robin approach for daily bug cleanup.
JavaScript
2
star
25

express-view-helpers

JavaScript
2
star
26

kollekt-node

An experiment about udp throughput in node.js.
JavaScript
2
star
27

jquery.char-counter

A jQuery plugin for counting and limiting the characters in a textarea.
JavaScript
2
star
28

node-aws

AWS seed for node.js
JavaScript
2
star
29

js-basics-tdd

JavaScript
2
star
30

PixelCloud-Client-Node

JavaScript
2
star
31

NoSpec

Spec framework for node.js
JavaScript
2
star
32

javascript-basics

Learning materials for getting started with JavaScript
JavaScript
2
star
33

nodeable

Prototype inspired helper package for node.js
JavaScript
2
star
34

identify.js

Parse identify (image-magick) output into JS object.
2
star
35

node-image-test

1
star
36

dashboard

JavaScript
1
star
37

com.depold.helpers

JavaHelpers
Java
1
star
38

buster-user-agent-parser

JavaScript
1
star
39

pivotal-time-travel

JavaScript
1
star
40

pixel-quest

MMORPG for js13kgames.
JavaScript
1
star
41

gulp-markdown-code-blocks

Gulp task for running markdown-code-blocks.
JavaScript
1
star
42

jaspin-test-repo

a test repo for jaspin
1
star
43

npm-runner

Shell
1
star
44

js-basics-graphql

JavaScript
1
star
45

PixelCloud-Client-Ruby

1
star
46

file-usage-analyzer

finds unused files in a directory according to a nginx log file
1
star
47

jaspin

Node.JS based Dropbox inspired solution for inhouse networks.
1
star
48

buster-test

JavaScript
1
star
49

jquery.templar

A super simple and dynamic templating engine.
JavaScript
1
star
50

js-basics-cookies-sessions

How to manage sessions and cookies in JavaScript
JavaScript
1
star
51

workshop-docker-k8s

Source code for our Docker + Kubernetes workshop
JavaScript
1
star
52

delete-duplicates

Deletion script that interprets Synology's duplication reports and deletes the duplicate files.
JavaScript
1
star
53

rateable_attributes

A reimplementation of acts_as_rateable but with support for multiple attributes for a specific model.
Ruby
1
star