Skip to content

Commit bdc4680

Browse files
committed
More Tests
1 parent 5cd6b88 commit bdc4680

File tree

7 files changed

+66
-14
lines changed

7 files changed

+66
-14
lines changed

src/PhpSpreadsheet/Reader/Html.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,9 @@ private function insertImage(Worksheet $sheet, string $column, int $row, array $
10561056

10571057
$drawing = new Drawing();
10581058
$drawing->setPath($src);
1059+
if ($drawing->getPath() === '') {
1060+
return;
1061+
}
10591062
$drawing->setWorksheet($sheet);
10601063
$drawing->setCoordinates($column . $row);
10611064
$drawing->setOffsetX(0);

src/PhpSpreadsheet/Reader/Xlsx.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,6 +1449,9 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
14491449
if (isset($images[$linkImageKey])) {
14501450
$url = str_replace('xl/drawings/', '', $images[$linkImageKey]);
14511451
$objDrawing->setPath($url);
1452+
if ($objDrawing->getPath() === '') {
1453+
continue;
1454+
}
14521455
}
14531456
}
14541457
$objDrawing->setCoordinates(Coordinate::stringFromColumnIndex(((int) $oneCellAnchor->from->col) + 1) . ($oneCellAnchor->from->row + 1));
@@ -1543,6 +1546,9 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
15431546
if (isset($images[$linkImageKey])) {
15441547
$url = str_replace('xl/drawings/', '', $images[$linkImageKey]);
15451548
$objDrawing->setPath($url);
1549+
if ($objDrawing->getPath() === '') {
1550+
continue;
1551+
}
15461552
}
15471553
}
15481554
$objDrawing->setCoordinates(Coordinate::stringFromColumnIndex(((int) $twoCellAnchor->from->col) + 1) . ($twoCellAnchor->from->row + 1));

src/PhpSpreadsheet/Worksheet/Drawing.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,17 @@ public function setPath(string $path, bool $verifyFile = true, ?ZipArchive $zip
103103
$this->path = $path;
104104
// Implicit that it is a URL, rather store info than running check above on value in other places.
105105
$this->isUrl = true;
106-
$imageContents = file_get_contents($path);
107-
$filePath = tempnam(sys_get_temp_dir(), 'Drawing');
108-
if ($filePath) {
109-
file_put_contents($filePath, $imageContents);
110-
if (file_exists($filePath)) {
111-
$this->setSizesAndType($filePath);
112-
unlink($filePath);
106+
$imageContents = @file_get_contents($path);
107+
if ($imageContents === false) {
108+
$this->path = '';
109+
} else {
110+
$filePath = tempnam(sys_get_temp_dir(), 'Drawing');
111+
if ($filePath) {
112+
file_put_contents($filePath, $imageContents);
113+
if (file_exists($filePath)) {
114+
$this->setSizesAndType($filePath);
115+
unlink($filePath);
116+
}
113117
}
114118
}
115119
} elseif (file_exists($path)) {
@@ -122,7 +126,8 @@ public function setPath(string $path, bool $verifyFile = true, ?ZipArchive $zip
122126
$this->setSizesAndType($path);
123127
}
124128
} else {
125-
throw new PhpSpreadsheetException("File $path not found!");
129+
//throw new PhpSpreadsheetException("File $path not found!");
130+
$this->path = '';
126131
}
127132
} else {
128133
$this->path = $path;

src/PhpSpreadsheet/Writer/Xlsx/ContentTypes.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PhpOffice\PhpSpreadsheet\Shared\File;
77
use PhpOffice\PhpSpreadsheet\Shared\XMLWriter;
88
use PhpOffice\PhpSpreadsheet\Spreadsheet;
9+
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
910
use PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing;
1011
use PhpOffice\PhpSpreadsheet\Writer\Exception as WriterException;
1112

@@ -132,15 +133,20 @@ public function writeContentTypes(Spreadsheet $spreadsheet, bool $includeCharts
132133
$extension = '';
133134
$mimeType = '';
134135

135-
if ($this->getParentWriter()->getDrawingHashTable()->getByIndex($i) instanceof \PhpOffice\PhpSpreadsheet\Worksheet\Drawing) {
136-
$extension = strtolower($this->getParentWriter()->getDrawingHashTable()->getByIndex($i)->getExtension());
137-
$mimeType = $this->getImageMimeType($this->getParentWriter()->getDrawingHashTable()->getByIndex($i)->getPath());
138-
} elseif ($this->getParentWriter()->getDrawingHashTable()->getByIndex($i) instanceof MemoryDrawing) {
139-
$extension = strtolower($this->getParentWriter()->getDrawingHashTable()->getByIndex($i)->getMimeType());
136+
$drawing = $this->getParentWriter()->getDrawingHashTable()->getByIndex($i);
137+
if ($drawing instanceof Drawing) {
138+
$extension = strtolower($drawing->getExtension());
139+
if ($drawing->getIsUrl()) {
140+
$mimeType = image_type_to_mime_type($drawing->getType());
141+
} else {
142+
$mimeType = $this->getImageMimeType($drawing->getPath());
143+
}
144+
} elseif ($drawing instanceof MemoryDrawing) {
145+
$extension = strtolower($drawing->getMimeType());
140146
$extension = explode('/', $extension);
141147
$extension = $extension[1];
142148

143-
$mimeType = $this->getParentWriter()->getDrawingHashTable()->getByIndex($i)->getMimeType();
149+
$mimeType = $drawing->getMimeType();
144150
}
145151

146152
if (!isset($aMediaContentTypes[$extension])) {

tests/PhpSpreadsheetTests/Reader/Html/HtmlImage2Test.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,24 @@ public function testCanInsertImageGoodProtocol(): void
3131
self::assertEquals('A1', $drawing->getCoordinates());
3232
}
3333

34+
public function testCantInsertImageNotFound(): void
35+
{
36+
if (getenv('SKIP_URL_IMAGE_TEST') === '1') {
37+
self::markTestSkipped('Skipped due to setting of environment variable');
38+
}
39+
$imagePath = 'https://phpspreadsheet.readthedocs.io/en/latest/topics/images/xxx01-03-filter-icon-1.png';
40+
$html = '<table>
41+
<tr>
42+
<td><img src="' . $imagePath . '" alt="test image voilà"></td>
43+
</tr>
44+
</table>';
45+
$filename = HtmlHelper::createHtml($html);
46+
$spreadsheet = HtmlHelper::loadHtmlIntoSpreadsheet($filename, true);
47+
$firstSheet = $spreadsheet->getSheet(0);
48+
$drawingCollection = $firstSheet->getDrawingCollection();
49+
self::assertCount(0, $drawingCollection);
50+
}
51+
3452
public function testCannotInsertImageBadProtocol(): void
3553
{
3654
$this->expectException(SpreadsheetException::class);

tests/PhpSpreadsheetTests/Reader/Xlsx/URLImageTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,20 @@ public function testURLImageSource(): void
4343
}
4444
}
4545

46+
public function testURLImageSourceNotFound(): void
47+
{
48+
if (getenv('SKIP_URL_IMAGE_TEST') === '1') {
49+
self::markTestSkipped('Skipped due to setting of environment variable');
50+
}
51+
$filename = realpath(__DIR__ . '/../../../data/Reader/XLSX/urlImage.notfound.xlsx');
52+
self::assertNotFalse($filename);
53+
$reader = IOFactory::createReader('Xlsx');
54+
$spreadsheet = $reader->load($filename);
55+
$worksheet = $spreadsheet->getActiveSheet();
56+
$collection = $worksheet->getDrawingCollection();
57+
self::assertCount(0, $collection);
58+
}
59+
4660
public function testURLImageSourceBadProtocol(): void
4761
{
4862
$filename = realpath(__DIR__ . '/../../../data/Reader/XLSX/urlImage.bad.xlsx');
9.88 KB
Binary file not shown.

0 commit comments

Comments
 (0)