forked from parcel-bundler/website
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Document image transformer (parcel-bundler#782)
- Loading branch information
1 parent
d18f1f7
commit c29bd1e
Showing
4 changed files
with
68 additions
and
2 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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,61 @@ | ||
--- | ||
layout: layout.njk | ||
eleventyNavigation: | ||
key: recipes-image | ||
title: <img src="/assets/lang-icons/image.svg" alt=""/> Image | ||
order: 2 | ||
--- | ||
|
||
Image assets can be imported and processed like any other asset in Parcel. As with any other asset you can import this asset from any other asset, so you can import images from html, css, js, ts, ... | ||
|
||
## Parcel image transformer | ||
|
||
Using the Parcel transformer `@parcel/transformer-image` you can resize, change the format and quality of an image. To do this we added the possibility to define query parameters. | ||
|
||
To do these image transformations we rely on the image transformation library [Sharp](https://sharp.pixelplumbing.com/), therefore we require you to install it locally using `npm install sharp -D` or `yarn install sharp -D`. | ||
|
||
The query parameters you can use are: | ||
|
||
- `width`: The width to resize the image to | ||
- `height`: The height to resize the image to | ||
- `quality`: The image quality percentage you want, for example `?quality=75` | ||
- `as`: File format to use, for example: `?as=webp` | ||
|
||
Supported image formats: `jpeg` / `jpg`, `png`, `webp`, `tiff`, `heic` / `heif` and `raw` | ||
|
||
A JavaScript example: | ||
|
||
{% sample %} | ||
{% samplefile "main.js" %} | ||
|
||
```js | ||
import imageUrl from "url:./image.jpeg?as=webp&width=250"; | ||
``` | ||
|
||
{% endsamplefile %} | ||
{% endsample %} | ||
|
||
An HTML example: | ||
|
||
{% sample %} | ||
{% samplefile "index.html" %} | ||
|
||
```html | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<title>HTML Example</title> | ||
</head> | ||
<body> | ||
<picture> | ||
<source src="url:./image.jpeg?as=webp&width=800" type="image/webp" /> | ||
<source src="url:./image.jpeg?width=800" type="image/jpeg" /> | ||
<img src="url:./image.jpeg?width=200" alt="test image" /> | ||
</picture> | ||
</body> | ||
</html> | ||
``` | ||
|
||
{% endsamplefile %} | ||
{% endsample %} |
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