Transform images (size, format)
You can serve an entry’s image in a different size or format just by adding parameters to its URL. Transformed images are stored, so from the second time on they are returned right away.
Before you begin
Section titled “Before you begin”- You can read entries that have an image field through the API. For how to read them, see Read entries
- You do not need an API key to open an image URL
Get the image URL
Section titled “Get the image URL”An image field is returned like this. url is the image URL.
"eyecatch": { "url": "https://<media host>/media/k2m9x0a1b2c3/hero.jpg", "width": 1600, "height": 900, "alt": "トップの写真"}The <media host> part depends on the environment. Do not build the URL yourself. Use the returned url value as is. width, height, and alt may be missing.
Anyone who knows the URL of an image or file can open it. This does not depend on whether the entry is published. Do not upload files you do not want others to see.
Change the size and format
Section titled “Change the size and format”Add parameters to the image URL. Make it AVIF, 800 px wide, with quality 60. Replace <image URL> with the returned url value.
<image URL>?w=800&fm=avif&q=60In HTML, use it like this:
<img src="<image URL>?w=800&fm=avif&q=60" width="800" alt="トップの写真"/>You can use the following parameters.
| Name | Value | Default | Meaning |
|---|---|---|---|
w |
An integer from 1 to 8192 | Original size | Width (px) |
h |
An integer from 1 to 8192 | Original size | Height (px) |
fit |
clip max crop scale |
clip |
How to fit into the w and h box |
q |
An integer from 1 to 100 | 75 | Quality. Applies to JPEG, WebP, and AVIF |
fm |
jpg (jpeg also works) png webp avif |
Original format | Output format |
dpr |
A number from 1 to 5. Decimals allowed | 1 | The factor that w and h are multiplied by |
- If you add only one of
wandh, the other is set so that the aspect ratio is kept - If the size multiplied by
dprexceeds 8192, it becomes 8192 - The photo orientation (EXIF) is applied before transforming. Shooting information and location information are removed from transformed images
- Unknown parameters (
auto,blur, and others) are ignored
The names and meanings match imgix.
Fit into a box (fit)
Section titled “Fit into a box (fit)”When you add both w and h, fit decides how the image fits into the box.
| Value | How it fits |
|---|---|
clip (default) |
Keeps the aspect ratio and makes the image as large as fits in the box. It can also make the image larger than the original |
max |
The same as clip, but does not make the image larger than the original |
crop |
Keeps the aspect ratio, fills the box, and crops the overflow from the center |
scale |
Makes the image the size of the box, ignoring the aspect ratio |
To make images squares of the same size, as for thumbnails in a list, use crop.
<image URL>?w=400&h=400&fit=cropMatch the screen density (srcset)
Section titled “Match the screen density (srcset)”For high-resolution screens, you can serve larger images with dpr.
<img src="<image URL>?w=400" srcset="<image URL>?w=400&dpr=2 2x, <image URL>?w=400&dpr=3 3x" width="400" alt="トップの写真"/>If the resulting images are the same, as with ?w=400&dpr=2 and ?w=800, the same image is returned.
Output format
Section titled “Output format”| Case | Output format |
|---|---|
fm=jpg, fm=webp |
That format |
fm=avif |
AVIF. However, if one side of the output image exceeds 1,200 px, WebP |
fm=png |
PNG. Available only when the original is PNG |
No fm, and the original is JPEG, WebP, or PNG |
The same format as the original |
No fm, and the original is GIF |
Not transformed (the original image is returned) |
Transparent areas are kept in PNG, WebP, and AVIF. In JPEG, they become white.
In the following cases, the parameters are ignored and the original image or file is returned as is.
- Files that are not JPEG, PNG, WebP, or GIF (SVG, AVIF, files that are not images, and so on)
- A GIF without
fm
If you add fm=jpg, fm=webp, or fm=avif to a GIF, only the first frame is transformed. The animation is lost.
Check the result
Section titled “Check the result”Open the URL with parameters in your browser. If the image is shown in the size and format you set, the transformation works. With curl, you can check the output format in the response’s Content-Type.
curl -sI "<image URL>?w=800&fm=webp" | grep -i content-typeYou see a line like this:
content-type: image/webpIf a parameter is not valid, you get 400 and the reason.
| What you got | Cause | Fix |
|---|---|---|
400 w: must be an integer between 1 and 8192 |
The value is out of range or not an integer | Use a value in the range in the table. Other parameters return a message in the same form |
400 fm: png is only available for PNG images |
You added fm=png to an image that is not PNG |
Use another format, such as fm=webp |
400 image is too large to transform |
The original image has too many pixels | Make the original image smaller and upload it again. Without parameters, the original image can still be served |
404 |
The image was deleted, or the URL is wrong | Read the entry again and use the url value |