image() table()

An image column: the path pattern is filled from the row id, the file is checked, and the image links to itself.

table()->image($name, $path, $link = '', $extra = [])

Options

OptionMeaning
$pathPath below the project with %d (row id) and {subdir1} / {subdir2} / {subdir3} (the id split in 3-digit folders).
$linkLink of the image; default the image itself.
width, height, style, classSize and look of the <img>; width default 50px.
fs_path, web_pathFolder the file is checked in (default PROJECT_PATH) and the URL prefix (default WEB_PATH).
img_path_checkfalse skips the file check.
img_path_callback, link_callbackCallbacks fn ($field, $params, $row, $instance_params) returning the path or the link.
default_imagePath used when the pattern gives nothing.
no_linkNo link around the image.

Example

Image Name
Red mug
Green mug

Code

$swatch = fn ($color) => 'data:image/svg+xml,' . rawurlencode('<svg xmlns="http://www.w3.org/2000/svg" width="40" height="40"><rect width="40" height="40" rx="6" fill="' . $color . '"/></svg>');
$rows = [
    1 => ['id' => 1, 'name' => 'Red mug', 'color' => '#d9534f'],
    2 => ['id' => 2, 'name' => 'Green mug', 'color' => '#5cb85c'],
];
return table($rows, ['no_pages' => 1])
    ->image('color', 'uploads/products/{subdir2}/{subdir3}/product_%d.jpg', '', [
        'img_path_callback' => fn ($field) => $swatch($field),
        'img_path_check' => false,
        'web_path' => '',
        'no_link' => 1,
        'width' => '40px',
    ])
    ->text('name');

Notes

  • By default a missing file prints nothing, so a page never shows broken images.
  • The demo draws inline SVG swatches through img_path_callback; a site uses the path pattern of its uploads.

See also: demo:table/filter, form/image