|
| 1 | +--- |
| 2 | +title: Image |
| 3 | +description: |
| 4 | +icon: lucide:image |
| 5 | +--- |
| 6 | + |
| 7 | +The `Image` component displays AI-generated images from the AI SDK. It accepts a [`Experimental_GeneratedImage`](https://ai-sdk.dev/docs/reference/ai-sdk-core/generate-image#generateimage) object from the AI SDK's `generateImage` function and automatically renders it as an image. |
| 8 | + |
| 9 | +:::ComponentLoader{label="Image" componentName="Image"} |
| 10 | +::: |
| 11 | + |
| 12 | +## Install using CLI |
| 13 | + |
| 14 | +:::tabs{variant="card"} |
| 15 | + ::div{label="ai-elements-vue"} |
| 16 | + ```sh |
| 17 | + npx ai-elements-vue@latest add image |
| 18 | + ``` |
| 19 | + :: |
| 20 | + ::div{label="shadcn-vue"} |
| 21 | + |
| 22 | + ```sh |
| 23 | + npx shadcn-vue@latest add https://registry.ai-elements-vue.com/image.json |
| 24 | + ``` |
| 25 | + :: |
| 26 | +::: |
| 27 | + |
| 28 | +## Install Manually |
| 29 | + |
| 30 | +Copy and paste the following files into the same folder. |
| 31 | + |
| 32 | +:::code-group |
| 33 | + ```vue [Image.vue] |
| 34 | + <script setup lang="ts"> |
| 35 | + import type { Experimental_GeneratedImage } from 'ai' |
| 36 | + import { cn } from '@repo/shadcn-vue/lib/utils' |
| 37 | + import { computed, useAttrs } from 'vue' |
| 38 | +
|
| 39 | + interface Props extends Experimental_GeneratedImage { |
| 40 | + class?: string |
| 41 | + alt?: string |
| 42 | + } |
| 43 | +
|
| 44 | + const props = defineProps<Props>() |
| 45 | + const attrs = useAttrs() |
| 46 | +
|
| 47 | + const classes = computed(() => cn( |
| 48 | + 'h-auto max-w-full overflow-hidden rounded-md', |
| 49 | + props.class, |
| 50 | + )) |
| 51 | +
|
| 52 | + const src = computed(() => `data:${props.mediaType};base64,${props.base64}`) |
| 53 | + </script> |
| 54 | +
|
| 55 | + <template> |
| 56 | + <img |
| 57 | + :alt="props.alt" |
| 58 | + :class="classes" |
| 59 | + :src="src" |
| 60 | + v-bind="attrs" |
| 61 | + > |
| 62 | + </template> |
| 63 | + ``` |
| 64 | + |
| 65 | + ```ts [index.ts] |
| 66 | + export { default as Image } from './Image.vue' |
| 67 | + ``` |
| 68 | +::: |
| 69 | + |
| 70 | +## Usage |
| 71 | + |
| 72 | +```vue |
| 73 | +<script setup lang="ts"> |
| 74 | +import { Image } from '@/components/ai-elements/image' |
| 75 | +
|
| 76 | +const exampleImage = { |
| 77 | + base64: 'valid-base64-string', |
| 78 | + mediaType: 'image/jpeg', |
| 79 | + uint8Array: new Uint8Array([]), |
| 80 | +} |
| 81 | +</script> |
| 82 | +
|
| 83 | +<template> |
| 84 | + <Image |
| 85 | + v-bind="exampleImage" |
| 86 | + alt="Example generated image" |
| 87 | + class="h-[150px] aspect-square border" |
| 88 | + /> |
| 89 | +</template> |
| 90 | +``` |
| 91 | + |
| 92 | +## Features |
| 93 | + |
| 94 | +- Accepts `Experimental_GeneratedImage` objects directly from the AI SDK |
| 95 | +- Automatically creates proper data URLs from base64-encoded image data |
| 96 | +- Supports all standard HTML image attributes |
| 97 | +- Responsive by default with `max-w-full h-auto` styling |
| 98 | +- Customizable with additional CSS classes |
| 99 | +- Includes proper TypeScript types for AI SDK compatibility |
| 100 | + |
| 101 | +## Props |
| 102 | + |
| 103 | +### `<Image />` |
| 104 | + |
| 105 | +:::field-group |
| 106 | + ::field{name="alt" type="string" optional} |
| 107 | + Alternative text for the image. |
| 108 | + :: |
| 109 | + |
| 110 | + ::field{name="class" type="string" optional} |
| 111 | + Additional CSS classes applied to the `<img>` element. |
| 112 | + :: |
| 113 | + |
| 114 | + ::field{name="[...props]" type="Experimental_GeneratedImage" optional} |
| 115 | + The image data to display, as returned by the AI SDK. |
| 116 | + :: |
| 117 | +::: |
0 commit comments