Skip to content

Novis\Spreadsheet\Reader::images

$reader->images(tainted string|int $sheet): array<Image>$reader->images(
    tainted string|int $sheet,
): array<Image>

Returns the pictures of one sheet.

$sheet is the sheet name, or its position from 0. Each picture has at, the cell of its top-left corner, such as "C2", image, the bytes of the picture file, and format, its file type, such as "png". A linked picture is a picture that Excel loads from an address. It has empty image bytes, and link is its address. For every other picture, link is null.

Good to know: reading a file never loads a linked picture. The bytes come from the file, so they are tainted bytes. Check the format and the size before you save a picture or show it on a web page. A CSV file has no pictures, so the list is empty. A sheet that does not exist throws a LogicError. images reads xlsx, xlsm, xlsb, xls and ods files.

The examples below list the pictures of a sheet, find the picture of each row, and check that the bytes are the same as the picture that was put in the file.

Parameters

  • $sheettainted string|int

Return value

Returns array<Image>.

Examples

List the pictures of a sheet

<?nvs
// Shows how to list the pictures of a sheet, with the cell, the format and the size of each one.
use Novis\Spreadsheet\Reader;
use Novis\Spreadsheet\Workbook;
// A PNG picture and a GIF picture, each one pixel.
bytes $png = Core\Encoding::fromBase64('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==');
bytes $gif = Core\Encoding::fromBase64('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7');
var $book = Workbook::createInMemory();
$book->addSheet('Products')->addImage('C2', $png)->addImage('C3', $gif)->addRows([['Item', 'Price', 'Photo'], ['Widget', 2.5], ['Gadget', 4.0]]);
var $reader = Reader::open($book->finish()->bytes ?? Core\Bytes::fill(0, 0));
// `at` is the top-left cell of the picture, and `format` is its file type.
foreach ($reader->images('Products') as var $image) {
echo $image->at, ': ', $image->format, ', ', Core\Bytes::length($image->image), " bytes\n";
}
Output
C2: png, 70 bytes
C3: gif, 42 bytes

The picture of each row

<?nvs
// Shows how to find the product photo of each row, by the cell of each picture.
use Novis\Spreadsheet\Reader;
use Novis\Spreadsheet\Workbook;
bytes $png = Core\Encoding::fromBase64('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==');
var $book = Workbook::createInMemory();
$book->addSheet('Products')->addImage('B3', $png)->addRows([['Item', 'Photo'], ['Widget'], ['Gadget']]);
var $reader = Reader::open($book->finish()->bytes ?? Core\Bytes::fill(0, 0));
// This map has the bytes of each picture by its cell, such as `B3`.
array<tainted bytes> $photos = [];
foreach ($reader->images(0) as var $image) {
$photos[$image->at] = $image->image;
}
// The photo of a row is in column B of that row.
foreach ($reader->rows(0, { header: true }) as var $row) {
tainted bytes $photo = $photos["B{$row->number}"] ?? Core\Bytes::fill(0, 0);
echo $row->values['Item'], ': ', Core\Bytes::length($photo) > 0 ? 'has a photo' : 'no photo', "\n";
}
Output
Widget: no photo
Gadget: has a photo

The same bytes come back

<?nvs
// Shows that `image` has the same bytes as the picture that was put in the file.
use Novis\Spreadsheet\Reader;
use Novis\Spreadsheet\Workbook;
bytes $logo = Core\Encoding::fromBase64('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7');
var $book = Workbook::createInMemory();
$book->addSheet('Cover')->addImage('A1', $logo)->addRow(['Shop price list']);
var $reader = Reader::open($book->finish()->bytes ?? Core\Bytes::fill(0, 0));
// The bytes are `tainted`, because they come from the file. Check them before you save or show them.
foreach ($reader->images('Cover') as var $image) {
echo $image->format, ' at ', $image->at, ': ', $image->image == $logo ? 'the same bytes' : 'other bytes', "\n";
echo 'link: ', $image->link ?? 'none', "\n";
}
Output
gif at A1: the same bytes
link: none