# Images

Every `media` value and `media_list` item carries its `url`, its pixel size, a `focal` point and ready-made `variants` (see [Media values](/api/objects/index#media-values)). `@borkol/content` turns that into image URLs and crops that respect the point the merchant set in **Edit focus**. Images are resized by [wsrv.nl](https://wsrv.nl), served as WebP and never upscaled past the original.

## borkolImage

```tsx title="A hero image"
import { borkolImage } from "@borkol/content";

const src = borkolImage(data.media, { w: 1600, h: 900 });
return src ? <img src={src} alt={data.media?.alt ?? ""} width={1600} height={900} /> : null;
```

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `media` | media value or `null` | Yes | A `media` value or a `media_list` item. Only `url` and `focal` are read. |
| `w` | number | Yes | Width in pixels, rounded to a whole pixel. |
| `h` | number | No | Height in pixels. With `h` the image is cropped to cover `w` by `h`; without it the image keeps its own proportions. |

The URL it returns:

| Case | URL |
| --- | --- |
| No `h` | `https://wsrv.nl/?url={url}&w={w}&we=1&output=webp&q=80` |
| `h`, no focal point | `...&w={w}&we=1&h={h}&fit=cover&output=webp&q=80` (centered crop) |
| `h` and a focal point | `...&w={w}&we=1&h={h}&fit=cover&a=focal&fpx={x}&fpy={y}&output=webp&q=80` |

`{url}` is URL-encoded. `fpx` and `fpy` are the value's `focal.x` and `focal.y` as given (fractions from `0` to `1`, from the left and from the top). wsrv centers the crop on that point and moves it back inside the image when the point is near an edge, so the crop never shows empty space. Without `h` there is no crop, so no focal parameters either. `borkolImage` returns `null` when the value is `null`, has no `url`, or a size is not a positive number; a focal point outside `0` to `1` is ignored (centered crop).

## next/image

`borkolImageLoader({ src, width, quality })` is a [`next/image` loader](https://nextjs.org/docs/app/api-reference/components/image#loader): it builds the same wsrv URL at each width Next asks for, with the image's `quality` prop (1 to 100). Without a `quality` prop the loader uses 80, like `borkolImage`, not Next's own default of 75. It is exported from `@borkol/content` and `@borkol/content/next` and imports nothing from Next itself. A `variants` URL passed as `src` is unwrapped to its original first.

Which sources it sends to wsrv:

| `src` | Result |
| --- | --- |
| An absolute `http://` or `https://` URL | A wsrv URL. This holds for **any** host, not only Borkol media: wsrv fetches the image from that host, so it must be public. For an image on a private host, give that `<Image>` the `unoptimized` prop. |
| A local path (`/logo.png`), a static import (`/_next/static/...`), `data:` or `blob:` | Unchanged: wsrv cannot fetch it, so the browser loads it as is. In development Next warns that the loader "does not implement width" for such an image; give it `unoptimized` to serve it as is without the warning. |

A loader is a function, so it runs in a Client Component. Set it once for the whole site with a loader file:

```ts title="borkol-image-loader.ts"
"use client";

export { borkolImageLoader as default } from "@borkol/content";
```

```ts title="next.config.ts"
const nextConfig = {
  images: { loader: "custom", loaderFile: "./borkol-image-loader.ts" },
};
```

Then `<Image src={media.url} alt={media.alt ?? ""} width={media.width ?? 1600} height={media.height ?? 900} sizes="100vw" />` serves every width from wsrv. A custom loader needs no `remotePatterns`. The loader resizes only; crops are left to CSS, below.

## Crops in CSS: focalStyle and cropPosition

When the page crops with CSS (`object-fit: cover`, for example `next/image` with `fill`), place the image with `focalStyle`:

```tsx title="A 16:9 card"
import Image from "next/image";
import { focalStyle } from "@borkol/content";

<div className="relative aspect-video overflow-hidden">
  <Image src={media.url} alt={media.alt ?? ""} fill sizes="480px" className="object-cover" style={focalStyle(media, 16 / 9)} />
</div>;
```

| Function | Returns |
| --- | --- |
| `focalStyle(media, cropAspect?)` | `{ objectPosition }`, for example `{ objectPosition: "70% 50%" }`. With the box's `cropAspect` (width divided by height) and the media's `width` and `height` it is the exact cut wsrv makes with `a=focal`. Without them it is the focal point itself (`"25% 70%"`), which keeps the point in view but does not center on it. No focal point: `"50% 50%"`. |
| `cropPosition(point, srcAspect, cropAspect)` | `{ x, y }` in percent (two decimals) for `object-position`: the crop centered on `point`, clamped to the image. `srcAspect` and `cropAspect` are width divided by height. The axis without overflow answers `50`. Invalid aspects return the point itself, clamped to `0` to `100`. |

The dashboard's **Edit focus** dialog draws its crop previews with the same function, so what the merchant sees there is what the site shows.

```ts title="cropPosition"
cropPosition({ x: 0.6, y: 0.5 }, 2, 1); // { x: 70, y: 50 }: a 2:1 image in a square
cropPosition({ x: 0.25, y: 0.5 }, 2, 1); // { x: 0, y: 50 }: clamped at the left edge
```

## When the focal point changes

A changed default focal point or alt text sends the webhook event [`media.updated`](/api/webhooks/index#mediaupdated) when a published record shows the image; `webhookHandler()` flushes the `borkol` cache tag, so the next render builds the new URLs. A focal point set for one use of the image is part of the record's values and goes live when the record is published.
