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

51 lines
2.0 KiB
PHP

<?php
declare(strict_types=1);
namespace PhpOffice\PhpSpreadsheetTests\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
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());
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx');
$spreadsheet->disconnectWorksheets();
$reloadedWorksheet = $reloadedSpreadsheet->getActiveSheet();
self::assertSame($image, $reloadedWorksheet->getBackgroundImage());
self::assertSame('image/png', $reloadedWorksheet->getBackgroundMime());
self::assertSame('png', $reloadedWorksheet->getBackgroundExtension());
self::assertSame(2, $reloadedWorksheet->getCell('B1')->getValue());
$reloadedSpreadsheet->disconnectWorksheets();
}
public function testInvalidImage(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->getCell('A1')->setValue(1);
$imageFile = __FILE__;
$image = (string) file_get_contents($imageFile);
self::assertNotSame('', $image);
$sheet->setBackgroundImage($image);
self::assertSame('', $sheet->getBackgroundImage());
self::assertSame('', $sheet->getBackgroundMime());
self::assertSame('', $sheet->getBackgroundExtension());
$spreadsheet->disconnectWorksheets();
}
}