# Photos: sizes, alt text and loading

Source: https://help.ruuster.com/idx/guide-photos

> Use listing photos with their sizes, order and alt text, load them fast without layout shift, and show the full gallery on a listing page.

Every listing comes with its photos as objects you can put straight into an `<img>` tag.

## The photo object

| Field | What it holds |
|---|---|
| `url` | The full photo URL, ready to use |
| `width`, `height` | The photo's size in pixels, or null when the board didn't send it |
| `order` | Its position in the listing's gallery, from the board |
| `alt` | Alt text written for the photo, such as "3 bed condo in Atlanta, GA". It never contains a hidden street address. |

A card carries the listing's first photos in `photos` and the total in `photoCount`. The detail answer
carries the gallery, and `GET /v1/listings/{listingId}/photos` returns every photo of one listing (up
to 100) when you need only the photos, such as for a lightbox.

cURL:

```bash
curl -s -G "https://idx.ruuster.com/v1/listings/aaaaaaaa-0000-4000-8000-000000000001/photos" \
  --data-urlencode limit=24 \
  -H "Authorization: Bearer $IDX_SECRET_KEY"
```

TypeScript:

```typescript
import { createClient } from "@ruuster/idx";

const idx = createClient({ secretKey: process.env.IDX_SECRET_KEY });

const r = await idx.request("/listings/aaaaaaaa-0000-4000-8000-000000000001/photos", {
  limit: "24",
});
if (!r.ok) throw new Error(`${r.error.code}: ${r.error.message}`);
console.log(r.data);
```

Python:

```python
import os

import requests

res = requests.get(
    "https://idx.ruuster.com/v1/listings/aaaaaaaa-0000-4000-8000-000000000001/photos",
    params={
        "limit": "24",
    },
    headers={"Authorization": "Bearer " + os.environ["IDX_SECRET_KEY"]},
    timeout=30,
)
body = res.json()
if not body["ok"]:
    raise RuntimeError(body["error"]["code"] + ": " + body["error"]["message"])
print(body["data"])
```

PHP:

```php
<?php

$query = http_build_query([
    'limit' => '24',
], '', '&', PHP_QUERY_RFC3986);
$ch = curl_init('https://idx.ruuster.com/v1/listings/aaaaaaaa-0000-4000-8000-000000000001/photos' . '?' . $query);
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => ['Authorization: Bearer ' . getenv('IDX_SECRET_KEY')],
    CURLOPT_TIMEOUT => 30,
]);
$body = json_decode(curl_exec($ch), true);
curl_close($ch);
if (!$body['ok']) {
    throw new RuntimeException($body['error']['code'] . ': ' . $body['error']['message']);
}
print_r($body['data']);
```

## Sizes

When a photo's URL is on `photos.ruuster.com`, it also comes in smaller widths: add `?width=480`,
`?width=800` or `?width=1200`. Use them in a `srcset` so phones load small files. Photos on other
hosts are used as they are.

`photoAttrs(photo, { sizes, priority })` from `@ruuster/idx` builds every attribute for you: `src`,
`srcSet` and `sizes` where the host has widths, `width` and `height`, `alt`, `loading` and, for the
first photo on the page, `fetchPriority="high"`.

## Loading fast

- **The first photo on the page loads first.** Give the first card's photo, or the listing's hero
  photo, `fetchpriority="high"` and `loading="eager"`. Every other photo gets `loading="lazy"`. Add
  `<link rel="preconnect" href="https://photos.ruuster.com">` to your page's head.
- **Reserve the space.** Put `width` and `height` on every `<img>` so the page doesn't jump while
  photos load. When they are null, give the photo's box a fixed aspect ratio in CSS, such as
  `aspect-ratio: 3 / 2`.
- **Use the alt text the API returns.** It describes the home without revealing a hidden address,
  and it is what screen readers and image search read.

The recipe components follow all three rules: `ListingCard` takes `priority` for the first card, and
`ListingDetail`'s gallery loads the first photo first and the rest as the visitor scrolls.

<figure class="idx-shot"><img src="/idx-docs/screens/ours/full-gallery-1440.png" alt="The full gallery dialog, closed, on its Photos tab, on a desktop (1440 px wide)" width="1440" height="900" loading="lazy" decoding="async"><figcaption>The full gallery: Photos and Street view tabs, the photos in a mosaic and the price beside them.</figcaption></figure>

<figure class="idx-shot"><img src="/idx-docs/screens/ours/full-gallery-375.png" alt="The full gallery dialog, closed, on its Photos tab, on a phone (375 px wide)" width="375" height="812" loading="lazy" decoding="async"><figcaption>The full gallery: Photos and Street view tabs, the photos in a mosaic and the price beside them.</figcaption></figure>

The markup is `static/html/full-gallery.html` in `@ruuster/idx-recipes`. The [HTML and CSS recipe](https://help.ruuster.com/idx/recipe-html-css) shows how to fill it and which stylesheet and script to load.

## Rules for photos

- Show the photos at the URLs the API returns, and never copy them to your own storage. Your page then
  always shows the photos the board has now, and a removed listing's photos leave with it.
- Keep each board's attribution and disclaimer on the page with its photos, as on any listing page.
