Skip to content

Commit cfe5cc1

Browse files
committed
Special Characters in Image File Name
Fix PHPOffice#2415. Fix PHPOffice#1470. If path name of image contains anything other than ASCII, or if it contains # or space or probably other exceptions, PhpSpreadsheet creates a file that Excel cannot, for whatever reason, read (it is valid xml). When adding an image to a spreadsheet, Excel does not retain the original path name; PhpSpreadsheet does, but probably shouldn't. It is changed to save the image file in the zip as the MD5 hash of the original path name. This produces a file that Excel can read. In addition, it ensures that, if the image is used in multiple places, it is saved in the Excel file only once. Because this error becomes evident only when opening the file in Excel, it is difficult to write a test case. I have instead duplicated sample Basic/05... using image files whose names match the reported error conditions.
1 parent 290c18e commit cfe5cc1

File tree

7 files changed

+395
-4
lines changed

7 files changed

+395
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
require __DIR__ . '/../Header.php';
4+
$spreadsheet = require __DIR__ . '/../templates/sampleSpreadsheet2.php';
5+
6+
// Save
7+
$helper->write($spreadsheet, __FILE__);

samples/images/terms con#ditions.jpg

528 Bytes
Loading

samples/images/サンプル.png

7.17 KB
Loading

samples/templates/sampleSpreadsheet2.php

+384
Large diffs are not rendered by default.

src/PhpSpreadsheet/Worksheet/Drawing.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function getIndexedFilename(): string
5151
$fileName = $this->getFilename();
5252
$fileName = str_replace(' ', '_', $fileName);
5353

54-
return str_replace('.' . $this->getExtension(), '', $fileName) . $this->getImageIndex() . '.' . $this->getExtension();
54+
return md5($this->path) . '.' . $this->getExtension();
5555
}
5656

5757
/**

src/PhpSpreadsheet/Writer/Xlsx.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ public function save($filename, int $flags = 0): void
486486
$imageContents = file_get_contents($imagePath);
487487
}
488488

489-
$zipContent['xl/media/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename())] = $imageContents;
489+
$zipContent['xl/media/' . $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()] = $imageContents;
490490
} elseif ($this->getDrawingHashTable()->getByIndex($i) instanceof MemoryDrawing) {
491491
ob_start();
492492
call_user_func(
@@ -496,7 +496,7 @@ public function save($filename, int $flags = 0): void
496496
$imageContents = ob_get_contents();
497497
ob_end_clean();
498498

499-
$zipContent['xl/media/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename())] = $imageContents;
499+
$zipContent['xl/media/' . $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()] = $imageContents;
500500
}
501501
}
502502

src/PhpSpreadsheet/Writer/Xlsx/Rels.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ public function writeDrawingRelationships(\PhpOffice\PhpSpreadsheet\Worksheet\Wo
328328
$objWriter,
329329
$i,
330330
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
331-
'../media/' . str_replace(' ', '', $drawing->getIndexedFilename())
331+
'../media/' . $drawing->getIndexedFilename()
332332
);
333333

334334
$i = $this->writeDrawingHyperLink($objWriter, $drawing, $i);

0 commit comments

Comments
 (0)