> ## Documentation Index
> Fetch the complete documentation index at: https://docs.instructorphp.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Image to data

## Overview

This is an example of how to extract structured data from an image using
Instructor. The image is loaded from a file and converted to base64 format
before sending it to OpenAI API.

The response model is a PHP class that represents the structured receipt
information with data of vendor, items, subtotal, tax, tip, and total.

## Scanned image

Here's the image we're going to extract data from.

<img src="https://mintcdn.com/cognesy-093fe941/dlz2lUeh9hhvQK0v/images/receipt.png?fit=max&auto=format&n=dlz2lUeh9hhvQK0v&q=85&s=cee38db228490f2147130fcd96ec6b1d" alt="Receipt" width="277" height="398" data-path="images/receipt.png" />

## Example

```php theme={null}
<?php
require 'examples/boot.php';

use Cognesy\Addons\Image\Image;
use Cognesy\Instructor\StructuredOutput;

class Vendor {
    public ?string $name = '';
    public ?string $address = '';
    public ?string $phone = '';
}

class ReceiptItem {
    public string $name;
    public ?int $quantity = 1;
    public float|int $price;
}

class Receipt {
    public Vendor $vendor;
    /** @var ReceiptItem[] */
    public array $items = [];
    public float|int|null $subtotal;
    public float|int|null $tax;
    public float|int|null $tip;
    public float|int $total;
}

$receipt = StructuredOutput::using('openai')->with(
    messages: Image::fromFile(__DIR__ . '/receipt.png')->toMessage(),
    responseModel: Receipt::class,
    prompt: 'Extract structured data from the receipt.',
    options: ['max_tokens' => 4096]
)->get();

dump($receipt);

assert(is_numeric($receipt->total));
assert(number_format((float) $receipt->total, 2, '.', '') === '169.82');
assert(count($receipt->items) > 0);
?>
```
