Novis\Spreadsheet\Sheet::addImage
$sheet->addImage(
string $cell,
bytes $data,
?{ width?: float, height?: float, offsetX?: float, offsetY?: float } $options = null,
): SheetPuts the image $data at the cell $cell, and returns the sheet. The image is a PNG, JPEG or GIF file, read from its bytes.
width and height are the size of the image in pixels. Without them, the image keeps its own size. offsetX and offsetY move the image right and down from the top left corner of the cell, in pixels.
Good to know: any other format throws a LogicError, so check an image from an upload before you add it. A file with bytes missing at the end also throws. So does an image that ends after the last column (XFD) or the last row (1,048,576). An image added after the first row stays in memory until the sheet is finished. To use less memory, add many images before the first row. The same image added again is stored in the file once. A CSV file has no images, so addImage does nothing there.
The examples below put a logo at the top of a report, and add a picture for each product. The last one shows the error for a file that is not an image.
Parameters
$cellstring$databytes$options?{ width?: float, height?: float, offsetX?: float, offsetY?: float }default null
Return value
Returns Sheet.
Changelog
| Version | Change |
|---|---|
0.0.6 | Each Novis\Spreadsheet\Sheet and Workbook method that adds to the file says what it adds, for example addRow(), addChart() and addSheet(). setPrintOptions() replaces print(). Chart->withSeries() and combinedWith() return a new chart. |
Examples
A logo at the top of a report
<?nvs// Shows how to put a logo at the top of a report, with its own size.use Novis\Spreadsheet\Reader;use Novis\Spreadsheet\Workbook;
// A PNG image of one pixel, written as base64 text so it fits in this file.bytes $logo = Core\Encoding::fromBase64('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==');
var $book = Workbook::createInMemory();var $sheet = $book->addSheet('Report');
// The image is added before the first row, so it is written at once.// It is 120 by 40 pixels, and it starts 4 pixels right of the corner of D1.$sheet->addImage('D1', $logo, { width: 120.0, height: 40.0, offsetX: 4.0 });$sheet->addRows([['shop' => 'North', 'sales' => 340], ['shop' => 'South', 'sales' => 210]]);bytes $file = $book->finish()->bytes ?? Core\Bytes::fill(0, 0);
foreach (Reader::open($file)->images('Report') as var $image) { echo $image->at, ': ', $image->format, ', ', Core\Bytes::length($image->image), " bytes\n";}D1: png, 70 bytesA picture for each product
<?nvs// Shows how to put a picture beside each product of a list.use Novis\Spreadsheet\Reader;use Novis\Spreadsheet\Workbook;
// A PNG image of one pixel. A real program reads the picture of each product from its storage.bytes $picture = Core\Encoding::fromBase64('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==');array<string> $products = ['Widget', 'Gadget', 'Gizmo'];
var $book = Workbook::createInMemory();var $sheet = $book->addSheet('Products');
// The images are added before the rows, so none of them waits in memory.int $row = 2;foreach ($products as string $name) { $sheet->addImage($sheet->cell('C', $row), $picture, { width: 32.0, height: 32.0 }); $row = $row + 1;}foreach ($products as string $product) { $sheet->addRow(['product' => $product, 'price' => 2.5, 'picture' => '']);}bytes $file = $book->finish()->bytes ?? Core\Bytes::fill(0, 0);
foreach (Reader::open($file)->images('Products') as var $image) { echo 'a ', $image->format, ' image at ', $image->at, "\n";}a png image at C2a png image at C3a png image at C4A file that is not an image
<?nvs// Shows the error for an upload that is not a PNG, JPEG or GIF file.use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();var $sheet = $book->addSheet('Products');
// These bytes are a text file, so `addImage` throws a `LogicError`.bytes $upload = 'This is not a picture.' as bytes;try { $sheet->addImage('C2', $upload);} catch (LogicError $e) { echo $e->message, "\n";}The image at `C2` is not a PNG, JPEG or GIF file. Convert it to one of these formats first.