-
Notifications
You must be signed in to change notification settings - Fork 411
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[image-url] Introducing the image-url package #352
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
515fcc5
[imagetool] Added function for generating url based on an image recor…
simen 225749a
[image-url] Introducing the image-url package
simen fb21262
[image-url] Documentation + configuration compliance
simen 955fed1
[image-url] README amendments
simen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"presets": ["es2015"], | ||
"plugins": ["syntax-object-rest-spread"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/lib | ||
/node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": ["sanity"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
node_modules | ||
npm-debug.log | ||
package-lock.json | ||
/lib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
test | ||
src | ||
.idea | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('./lib/builder').default |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe write a bit more on this part? :)