Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Special Characters in Image File Name #2416

Merged
merged 3 commits into from
Dec 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions samples/Basic/05_UnexpectedCharacters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

require __DIR__ . '/../Header.php';
$spreadsheet = require __DIR__ . '/../templates/sampleSpreadsheet2.php';

// Save
$helper->write($spreadsheet, __FILE__);
Binary file added samples/images/terms con#ditions.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added samples/images/サンプル.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
384 changes: 384 additions & 0 deletions samples/templates/sampleSpreadsheet2.php

Large diffs are not rendered by default.

5 changes: 1 addition & 4 deletions src/PhpSpreadsheet/Worksheet/Drawing.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ public function getFilename()
*/
public function getIndexedFilename(): string
{
$fileName = $this->getFilename();
$fileName = str_replace(' ', '_', $fileName);

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

/**
Expand Down
4 changes: 2 additions & 2 deletions src/PhpSpreadsheet/Writer/Xlsx.php
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ public function save($filename, int $flags = 0): void
$imageContents = file_get_contents($imagePath);
}

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

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

Expand Down
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Writer/Xlsx/Rels.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ public function writeDrawingRelationships(\PhpOffice\PhpSpreadsheet\Worksheet\Wo
$objWriter,
$i,
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
'../media/' . str_replace(' ', '', $drawing->getIndexedFilename())
'../media/' . $drawing->getIndexedFilename()
);

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