npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cf-image-url-builder

v2.1.2

Published

Build URLs for your cf entity's images

Downloads

545

Readme

cf-image-url-builder

Build URLs for your cf entity's images

From v1.0.0 this module will be incompatible with old-style cf entities that store contexts at entity.crops. Use the pre-v1.0.0 branch or some version < 1.0.0 if your entities do that. From now on, this module assumes that context selection is annotated on each image object itself, e.g:

{ images:
  [ { name: 'image.jpeg'
    /* other properties omitted for brevity */
    , selectedContexts: [ 'Main', 'Thumbnail' ]
    }
  ]
}

Installation

npm install cf-image-url-builder
var createUrlBuilder = require('cf-image-url-builder')

Usage

Instantiate like so:

var createImageUrlBuilder = require('cf-image-url-builder')
var urlBuilder = createImageUrlBuilder('http://dr.io', 'darkroom-key', images)
// or provide your own darkroom url builder
var urlBuilder = createImageUrlBuilder(req.darkroomUrlBuilder, images)

Where:

  • images is an array of raw image widget models (e.g. on the backend article.images.widgets, and on the frontend article.get('images').widgets.map(function (widget) { return widget.toJSON() }))

Examples

Article list with thumbnails

In your js view:

var template = renderJade(__dirname + '/article-list.jade')
  , createImageUrlBuilder = require('cf-image-url-builder')

function render() {

  var articles = [...] // Get the articles somehow

  articles.forEach(function (article) {
    var urlBuilder = createImageUrlBuilder('http://dr.io', 'darkroom-key', images)
      , image = urlBuilder.getImage('Thumbnail')

    if (image) {
      article.thumbnail =
        { url: image.crop('Landscape').constrain(300).url()
        , caption: image.properties.name
        }
    }

  })

  template({ articles: articles })

}

In your template:

each article in articles
  .article
    img(src=article.thumnail.url, alt=article.thumbnail.caption)
    h1 #{article.shortTitle}

Multi image slideshow

@todo: complete this stubbed out example

urlBuilder.getImages('Hero').forEach(function (img) {
  img.crop('Wide').constrain().url()
})

API

The full API docs follow. Unless you need to figure something tricky out, it's best to look at the examples. The API was designed to chain well, and for that reason, it looks a lot more complex than it needs to be in this formal view.

var urlBuilder = createUrlBuilder(darkroomUrl, darkroomKey, imageWidgets)

Creates a URL builder instance from a cf entity. All arguments are required.

var images = urlBuilder.getImages(String:context)

Returns an array of images for the given context. If no images exist for this context, the array is empty.

var image = urlBuilder.getImage(String:context)

Returns a single image for the given context. This calls getImages() internally and is just a convenience function the common use case where only a single image is desired. If there are no images for the given context, the return value is undefined.

var crop = image.crop(String:name)

Get the URL for the crop named name. Returns a crop entity, detailed below.

image.properties

A reference back to the image for accessing metadata. Useful if image captions or alt tags are required.

crop.url()

Generate a url for the original image.

var constrained = crop.constrain(Number:width, Number:height)

Apply the width or height constrains to the crop. Both are optional.

constrained.url()

Generate a url for the constrained image.

Errors

This module is designed to be used in templates, so throwing exceptions is avoided. Error messages are returned in place of the image URLs, if errors occur.

Tests

Run the tests with npm test and generate coverage report with npm test --coverage.

Credits

Built by developers at Clock.

Licence

Licensed under the New BSD License