Skip to content

Commit

Permalink
[image-url] Introducing the image-url package (#352)
Browse files Browse the repository at this point in the history
  • Loading branch information
simen authored and bjoerge committed Nov 13, 2017
1 parent 41ba52c commit bea1926
Show file tree
Hide file tree
Showing 15 changed files with 710 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/@sanity/image-url/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"presets": ["es2015"],
"plugins": ["syntax-object-rest-spread"]
}
2 changes: 2 additions & 0 deletions packages/@sanity/image-url/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/lib
/node_modules
3 changes: 3 additions & 0 deletions packages/@sanity/image-url/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": ["sanity"]
}
4 changes: 4 additions & 0 deletions packages/@sanity/image-url/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
npm-debug.log
package-lock.json
/lib
4 changes: 4 additions & 0 deletions packages/@sanity/image-url/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
test
src
.idea
node_modules
111 changes: 111 additions & 0 deletions packages/@sanity/image-url/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# @sanity/image-url

Quickly generate image urls from Sanity image records.

This helper will by default respect any crops/hotspots specified in the Sanity content provided to it. The most typical use case for this is to give it a sanity image and specify a width, height or both and get a nice, cropped and resized image according to the wishes of the content editor and the specifications of the front end developer.

In addition to the core use case, this library provides a handy builder to access the rich selection of processing options available in the Sanity image pipeline.

## Getting started

npm install --save @sanity/image-url

## Usage

The most common way to use this library in your project is to configure it by passing it your configured sanityClient. That way it will automatically be preconfigured to your current project and dataset:

``` js
import React from 'react'
import myConfiguredSanityClient from './sanityClient'
import imageUrlBuilder from '@sanity/image-url'

const builder = imageUrlBuilder(myConfiguredSanityClient)

function urlFor(source) {
return builder.image(source)
}
```

Then you can use the handy builder syntax to generate your urls:

``` js
<img src={urlFor(author.image).width(200).url()} />
```

This will ensure that the author image is alway 200 pixels wide, automatically applying any crop specified by the editor and cropping towards the hot-spot she drew. You can specify both width and height like this:

``` js
<img src={urlFor(movie.poster).width(500).height(300).url()}>
```

There are a huge number of useful options you can specify, like e.g. blur:

``` js
<img src={urlFor(mysteryPerson.mugshot).width(200).height(200).blur(50).url()}>
```

## Builder methods

### `image(source)`

Specify the image to be rendered. Accepts either a Sanity `image` record, an `asset` record, or just the asset id as a string. In order for hotspot/crop processing to be applied, the `image` record must be supplied.

### `dataset(dataset)`, `projectId(projectId)`

Usually you should preconfigure your builder with dataset and project id, but even whem you did, these let you temporarily override them if you need to render assets from other projects or datasets.

### `width(pixels)`

Specify the width of the rendered image in pixels.

### `height(pixels)`

Specify the height of the rendered image in pixels.

### `size(width, height)`

Specify width and height in one go.

### `focalPoint(x, y)`

Specify a center point to focus on when cropping the image. Values from 0.0 to 1.0 in fractions of the image dimensions. When specified, overrides any crop or hotspot in the image record.

### `minWidth(pixels)`, `maxWidth(pixels)`, `minHeight(pixels)`, `maxHeight(pixels)`

Specifies min/max dimensions when cropping

### `blur(amount)`, `sharpen(amount)`, `invert()`

Apply image processing.

### `rect(left, top, width, height)`

Specify the crop in pixels. Overrides any crop/hotspot in the image record.

### `format(name)`

Specify the image format of the image. 'jpg', 'pjpg', 'png', 'webp'

### `orientation(angle)`

Rotation in degrees. Acceptable values: 0, 90, 180, 270

### `quality(value)`

Compression quality, where applicable. 0-100

### `forceDownload(defaultFileName)`

Make this an url to download the image. Specify the file name that will be suggested to the user.

### `flipHorizontal()`, `flipVertical()`

Flips the image.

### `ignoreImageParams()`

Ignore any specifications from the image record (i.e. crop and hotspot).

### `url()`, `toString()`

Return the url as a string.
1 change: 1 addition & 0 deletions packages/@sanity/image-url/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./lib/builder').default
40 changes: 40 additions & 0 deletions packages/@sanity/image-url/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "@sanity/image-url",
"version": "0.120.0",
"description": "Tools to generate image urls from Sanity content",
"main": "index.js",
"scripts": {
"build": "npm run clean && npm run compile",
"compile": "babel --source-maps --copy-files -d lib/ src/",
"clean": "rimraf lib",
"postpublish": "npm run clean",
"posttest": "npm run lint",
"lint": "eslint .",
"pre-commit-check":
"npm test && (eslint . || (echo 'Warning: project has lint errors. Please fix and re-commit with `git commit --amend`' && echo))",
"test": "mocha --recursive --require babel-register test"
},
"author": "Sanity.io <hello@sanity.io>",
"license": "MIT",
"dependencies": {},
"devDependencies": {
"babel-register": "^6.26.0",
"eslint": "^4.11.0",
"eslint-config-sanity": "^3.1.0",
"mocha": "^3.2.0",
"rimraf": "^2.6.2",
"should": "^11.1.0"
},
"repository": {
"type": "git",
"url": "git+https://github.com/sanity-io/sanity.git"
},
"bugs": {
"url": "https://github.com/sanity-io/sanity/issues"
},
"homepage": "https://www.sanity.io/",
"directories": {
"test": "test"
},
"keywords": ["sanity", "cms", "headless", "realtime", "content", "image-url"]
}
145 changes: 145 additions & 0 deletions packages/@sanity/image-url/src/builder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import urlForImage from './urlForImage'

class ImageUrlBuilder {
constructor(parent, options) {
if (parent) {
this.options = Object.assign({}, parent.options, options || {})
} else {
this.options = options || {}
}
}

_withOptions(options) {
return new ImageUrlBuilder(this, options)
}

// The image to be represented. Accepts a Sanity 'image'-document, 'asset'-document or
// _id of asset. To get the benefit of automatic hot-spot/crop integration with the content
// studio, the 'image'-document must be provided.
image(source) {
return this._withOptions({source})
}

// Specify the dataset
dataset(dataset) {
return this._withOptions({dataset})
}

// Specify the projectId
projectId(projectId) {
return this._withOptions({projectId})
}

// Specify the width of the image in pixels
width(width) {
return this._withOptions({width})
}

// Specify the height of the image in pixels
height(height) {
return this._withOptions({height})
}

// Specify focal point in fraction of image dimensions. Each component 0.0-1.0
focalPoint(x, y) {
return this._withOptions({focalPoint: {x, y}})
}

maxWidth(maxWidth) {
return this._withOptions({maxWidth})
}

minWidth(minWidth) {
return this._withOptions({minWidth})
}

maxHeight(maxHeight) {
return this._withOptions({maxHeight})
}

minHeight(minHeight) {
return this._withOptions({minHeight})
}

// Specify width and height in pixels
size(width, height) {
return this._withOptions({width, height})
}

// Specify blur between 0 and 100
blur(blur) {
return this._withOptions({blur})
}

sharpen(sharpen) {
return this._withOptions({sharpen})
}

// Specify the desired rectangle of the image
rect(left, top, width, height) {
return this._withOptions({rect: {left, top, width, height}})
}

// Specify the image format of the image. 'jpg', 'pjpg', 'png', 'webp'
format(format) {
return this._withOptions({format})
}

invert(invert) {
return this._withOptions({invert})
}

// Rotation in degrees 0, 90, 180, 270
orientation(orientation) {
return this._withOptions({orientation})
}

// Compression quality 0-100
quality(quality) {
return this._withOptions({quality})
}

// Make it a download link. Parameter is default filename.
forceDownload(download) {
return this._withOptions({download})
}

// Flip image horizontally
flipHorizontal() {
return this._withOptions({flipHorizontal: true})
}

// Flip image verically
flipVertical() {
return this._withOptions({flipVertical: true})
}

// Ignore crop/hotspot from image record, even when present
ignoreImageParams() {
return this._withOptions({ignoreImageParams: true})
}

// Gets the url based on the submitted parameters
url() {
return urlForImage(this.options)
}

// Synonym for url()
toString() {
return this.url()
}
}

export default function urlBuilder(options) {
// Did we get a SanityClient?
if (options && typeof options.clientConfig == 'object') {
// Inherit config from client
return new ImageUrlBuilder(null, {
projectId: options.clientConfig.projectId,
dataset: options.clientConfig.dataset,
})
}

// Or just accept the options as given
return new ImageUrlBuilder(null, options)
}
Loading

0 comments on commit bea1926

Please sign in to comment.