Images are dumb - make them smarter.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Images are dumb - make them smarter with flexible dynamic image replacement

Demo on


SmarterImages aims to be the most flexible dynamic image replacement jQuery plugin. Think CDN, but without the server.

The plugin watches the browser viewport width on load() and resize(), and when the zone changes the plugin rebuilds a new src URL. If the object in question is an <img/> element, it gets a new src; if the object in question is anything else (such as a <div/>) it will get an updated background-image style property.


  • Works with any custom image processor URL or
  • Define your own custom breakpoints: as many or few as required ("small / medium / large" wasn't exactly flexible)
  • Works with inline images and background images
  • Option to upsize and downsize images, or upsize only to reduce server calls
  • Option to maintain image aspect ratio or resize-only
  • Define image URL protocols
  • Callback function for custom code image replacements after

Inline Images and Placeholder.gif

To fire SmarterImages on inline <img/> elements, they are required to have both an src attribute and a data-si-src attribute. Even though the "real" src is stored inside data-si-src, without a "true" src the image won't load correctly. To get around this, the fastest-known solution named placeholder.gif is included - a 43byte 1px x 1px transparent GIF, more on that here. Please note that in some cases it's more appropriate for user experience to implement a "loading" graphic, like a nice FontAwesome animated icon.

Definitions: Breakpoints vs. Zones

This document will refer to terms such as "breakpoint" and "zone" - here's what they mean:

A pixel integer value at which the script will initiate an image swap. These markers encapsulate the zones. They're defined by passing in an array of integers to options.breakpoints.

The range in which a breakpoint-specific sized image is visible. These are the areas between the breakpoints, and are automatically calculated.

0		360			480			768			1050		1300		1600

		0 (min)			   1		   2		   3		   4		5 (max)


Processors are any services, CDNs, APIs, or custom functions which handle image sizing and/or serving. Additional options for processors are always welcome and appreciated, please open an issue with suggestions!

<> is the easiest way to resize, store, and deliver your images to your customers.     —

To use (paid service) set options.useCloudImageIO to true, and pass in your URL prefix to options.cloudImageIO. That's it!

Custom Processor

To use any other custom setup, set options.useCustomURL to true and pass in a custom URL to options.customURL. Two replacement variables are available to help build the correct URL - check out the code examples section later in this document.

Custom Processor URL Replacement Variables

%%size%% This string will be replaced with the current breakpoint size dynamically.

Example: Output:

%%source%% This string will be replaced with the current image's source attribute (data-si-src).

Example: Output:


To use placeholders for mock-ups or testing purposes (for science), set options.usePlaceholders to true. Note that this will ignore the data-si-src option completely and will populate the targeted object(s) with placeholder images served from


Option Type Default Description
breakpoints array [360, 480, 768, 1050, 1300, 1600] Defines the breakpoints at which targeted images will be replaced.
upsizeOnly boolean false If true, images will only load larger sizes to reduce server calls; if false, images with load larger and smaller sizes.
maintainAspect boolean false If true, images will maintain their original width / height aspect ratio.
useCloudImageIO boolean false Switch to turn on / off the image processor.
cloudImageIO string N/A URL account prefix.
useCustomURL boolean false Switch to turn on / off the custom URL image processor.
customURL string N/A Custom image processor URL pattern.
usePlaceholders boolean false Turn on placeholder images for testing.
protocol string http:// Defines the protocol for the target image URLs (http://, https://, //).
logging boolean false Turns on / off console messages for debugging / testing.
onImgSwap function false Callback function which fires after each replacement.


Basic Examples

Inline Image

<!-- HTML -->
<img src="/path/to/placeholder.gif" data-si-src=""></div>
// jQuery
// when the document is ready
$(document).ready(function() {

	// call SmarterImages

Background Image

<!-- HTML -->
<div class="smarter-image" data-si-src=""></div>
// jQuery
$('.smarter-image').smarterImages(); Example

// jQuery
	useCloudImageIO: true,
	cloudImageIO: 'abcdef'

Custom URL Example

// jQuery
	useCustomURL: true,
	customURL: ''

Callback Function Example

$this is exposed so that each <img/> can be targeted individually.

thisBreakpoint holds the current zone's size, in pixels.

// jQuery
	useCloudImageIO: true,
	cloudImageIO: 'abcdef',

	onImgSwap: function($this, thisBreakpoint) {
		// your callback function code here
		// ...

Kitchen Sink Example

		// basic things
		breakpoints: [360, 480, 768, 1050, 1300, 1600],
		upsizeOnly: true,
		maintainAspect: false,

		// setup
		useCloudImageIO: false,
		cloudImageIO: '',

		// custom url setup
		useCustomURL: false,
		customURL: '',

		// placeholders (for science)
		usePlaceholders: false,

		// nuts + bolts
		protocol: 'http://',
		logging: false,

		// callback function
		onImgSwap: function($this, thisBreakpoint) {
			// ...



Initial release.


Mike Zarandona |