# Function

Three PHP functions render an image for a post from a theme or plugin:
`sleekPixel_get_image_url()`, `sleekPixel_get_image()` and
`sleekPixel_render_image()`. They work whatever the image's
[conditions](/docs/pixel/locations.md) say.

## Parameters

All three take one array with these keys:

- `id` (required): the [image's ID](/docs/pixel/images.md#the-image-id).
- `key` (required): a name for this use of the image, a string or an array of
  strings that Pixel joins with hyphens. Pixel keeps one rendered file per
  image, post and key, so give each use its own key.
- `postId` (optional): the post whose data fills the image. Defaults to the
  current post.
- `data` (optional): an array merged into the image's
  [dynamic data](/docs/pixel/dynamic-data.md). For example,
  `['post' => ['title' => 'Custom title']]` replaces the value of
  `{post.title}`.

Without `id` or `key` the functions return an empty string.

The rendered file is reused until the design, the post, the `data` you pass,
the language or Pixel's version changes; then it is rendered again and the old
file is deleted. Passing different `data` under the same key therefore renders
the image again each time the `data` changes; give each variant its own key to
keep a file for each. With
[asynchronous processing](/docs/pixel/settings/options.md#image-processing), and
always for a visitor who is not signed in, the file is rendered in the
background, `data` included, and the functions return the previous file of the
same key and `data`, or nothing, until it exists; for a signed-in user's REST
response or feed and in WP-CLI they render it at once.

## sleekPixel_get_image_url

Returns the URL of the rendered file. It returns an empty string when the
server has no Imagick extension and `false` while the file does not exist yet.

```php
if (function_exists('sleekPixel_get_image_url')) {
  $imageUrl = sleekPixel_get_image_url([
    'id' => 123,
    'key' => 'post-card',
    'postId' => get_the_ID(),
  ]);

  if ($imageUrl) {
    echo esc_url($imageUrl);
  }
}
```

## sleekPixel_get_image

Returns an `<img>` tag with the canvas's width and height and an empty `alt`,
or an empty string when there is no file yet. The URL and the dimensions are
escaped.

```php
if (function_exists('sleekPixel_get_image')) {
  echo sleekPixel_get_image([
    'id' => 123,
    'key' => 'post-card-custom-title',
    'postId' => get_the_ID(),
    'data' => [
      'post' => [
        'title' => 'Custom title for this card',
      ],
    ],
  ]);
}
```

## sleekPixel_render_image

Echoes the result of `sleekPixel_get_image()`.

```php
if (function_exists('sleekPixel_render_image')) {
  sleekPixel_render_image([
    'id' => 123,
    'key' => 'post-card',
    'postId' => get_the_ID(),
  ]);
}
```
