Skip to content

Commit 13c3ec9

Browse files
authored
Merge pull request #4180 from oleibman/issue4179
Xlsx Writer Duplicate ContentTypes Entry for Background Image
2 parents 44014f8 + db7d4a7 commit 13c3ec9

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

src/PhpSpreadsheet/Writer/Xlsx/ContentTypes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public function writeContentTypes(Spreadsheet $spreadsheet, bool $includeCharts
196196
$bgImage = $spreadsheet->getSheet($i)->getBackgroundImage();
197197
$mimeType = $spreadsheet->getSheet($i)->getBackgroundMime();
198198
$extension = $spreadsheet->getSheet($i)->getBackgroundExtension();
199-
if ($bgImage !== '' && !isset($aMediaContentTypes[$mimeType])) {
199+
if ($bgImage !== '' && !isset($aMediaContentTypes[$extension])) {
200200
$this->writeDefaultContentType($objWriter, $extension, $mimeType);
201201
}
202202
}

tests/PhpSpreadsheetTests/Writer/Xlsx/ArrayFunctionsTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ class ArrayFunctionsTest extends TestCase
1515
{
1616
private string $outputFile = '';
1717

18+
protected function tearDown(): void
19+
{
20+
if ($this->outputFile !== '') {
21+
unlink($this->outputFile);
22+
$this->outputFile = '';
23+
}
24+
}
25+
1826
public function testArrayOutput(): void
1927
{
2028
$spreadsheet = new Spreadsheet();
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpOffice\PhpSpreadsheetTests\Writer\Xlsx;
6+
7+
use PhpOffice\PhpSpreadsheet\Shared\File;
8+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
9+
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
10+
use PhpOffice\PhpSpreadsheet\Writer\Xlsx as XlsxWriter;
11+
use PHPUnit\Framework\TestCase;
12+
use ZipArchive;
13+
14+
class Issue4179Test extends TestCase
15+
{
16+
private string $outputFile = '';
17+
18+
protected function tearDown(): void
19+
{
20+
if ($this->outputFile !== '') {
21+
unlink($this->outputFile);
22+
$this->outputFile = '';
23+
}
24+
}
25+
26+
public function testIssue4179(): void
27+
{
28+
// duplicate entry in ContentTypes
29+
$spreadsheet = new Spreadsheet();
30+
$sheet = $spreadsheet->getActiveSheet();
31+
$imageFile = 'tests/data/Writer/XLSX/backgroundtest.png';
32+
$image = (string) file_get_contents($imageFile);
33+
$sheet->setBackgroundImage($image);
34+
$drawing = new Drawing();
35+
$drawing->setName('Blue Square');
36+
$drawing->setPath('tests/data/Writer/XLSX/blue_square.png');
37+
$drawing->setCoordinates('A1');
38+
$drawing->setWorksheet($sheet);
39+
$writer = new XlsxWriter($spreadsheet);
40+
$this->outputFile = File::temporaryFilename();
41+
$writer->save($this->outputFile);
42+
$spreadsheet->disconnectWorksheets();
43+
44+
$zip = new ZipArchive();
45+
$open = $zip->open($this->outputFile, ZipArchive::RDONLY);
46+
$pngCount = 0;
47+
if ($open !== true) {
48+
self::fail("zip open failed for {$this->outputFile}");
49+
} else {
50+
$contents = (string) $zip->getFromName('[Content_Types].xml');
51+
$subCount = substr_count($contents, '"png"');
52+
self::assertSame(1, $subCount);
53+
for ($i = 0; $i < $zip->numFiles; ++$i) {
54+
$filename = (string) $zip->getNameIndex($i);
55+
if (preg_match('~^xl/media/\\w+[.]png$~', $filename) === 1) {
56+
++$pngCount;
57+
}
58+
}
59+
}
60+
self::assertSame(2, $pngCount);
61+
}
62+
}

0 commit comments

Comments
 (0)