forked from PHPOffice/PhpSpreadsheet
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix PHPOffice#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.
- Loading branch information
Showing
10 changed files
with
211 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
tests/PhpSpreadsheetTests/Writer/Html/BackgroundImageTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?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(); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
tests/PhpSpreadsheetTests/Writer/Xlsx/BackgroundImageTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?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(); | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.