Files
oleibman 5a60ba45ab Sheet Background Images (#3795)
* Sheet Background Images

Fix #1649, a 3-year-old issue long marked "stale". Excel supports background images on sheets; now PhpSpreadsheet will as well. Support is limited to Xlsx (read and write) and Html (write only). As far as I can tell, Excel Xml and Gnumeric do not support this, nor, of course, do Csv and Slk; Excel Xls does, but, as usual, how to handle it in BIFF format is a mystery; LibreOffice ODS supports it differently than Excel, and this is just another of many ODS style properties not currently supported by PhpSpreadsheet.

* Update CHANGELOG.md
2023-11-21 07:06:12 -08:00

32 lines
1.1 KiB
PHP

<?php
declare(strict_types=1);
namespace PhpOffice\PhpSpreadsheetTests\Writer\Html;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Html;
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;
class BackgroundImageTest extends AbstractFunctional
{
public function testBackgroundImage(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->getCell('A1')->setValue(1);
$sheet->getCell('B1')->setValue(2);
$sheet->getCell('A2')->setValue(3);
$sheet->getCell('B2')->setValue(4);
$imageFile = 'tests/data/Writer/XLSX/backgroundtest.png';
$image = (string) file_get_contents($imageFile);
$sheet->setBackgroundImage($image);
self::assertSame('image/png', $sheet->getBackgroundMime());
self::assertSame('png', $sheet->getBackgroundExtension());
$writer = new Html($spreadsheet);
$header = $writer->generateHTMLHeader(true);
self::assertStringContainsString('table.sheet0 { background-image:url(data:image/png;base64,', $header);
$spreadsheet->disconnectWorksheets();
}
}