Skip to content

Commit 077f0f0

Browse files
committed
Name an image file whose path does not name itself
`getExtension()` asks `pathinfo()` and nothing else, so a path carrying no extension answers with an empty string. `getIndexedFilename()` then builds `logo1.` -- a media part ending in a bare dot, which the writers register under no content type at all. The bytes know what the name does not: `getimagesizefromstring()` already backs `getMimeType()` a few lines above, and its type constant converts straight to an extension. The lookup happens only when `pathinfo()` came back empty and the file is there to be read, so a path with an extension costs exactly what it did before -- no file access. An extensionless SVG stays extensionless: it has no pixel dimensions for `getimagesize()` to report, and there is nothing else in the file this decision could rest on.
1 parent c01ebbd commit 077f0f0

3 files changed

Lines changed: 25 additions & 1 deletion

File tree

docs/changes/1.3.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
- ODPresentation Writer : Fixed the background of a master slide being dropped on save by [@dkulyk](http://github.com/dkulyk) in [#912](https://github.com/PHPOffice/PHPPresentation/pull/912)
2323
- PowerPoint2007 Reader : Fixed a shape that asked for no fill being read as having no fill at all, which crashes the ODPresentation Writer on save, by [@dkulyk](http://github.com/dkulyk) in [#915](https://github.com/PHPOffice/PHPPresentation/pull/915)
2424
- Fixed a file with no measurable size (an SVG, a video) warning on `setPath()`, and a fresh object being looked up in the hash table under a null key, by [@dkulyk](http://github.com/dkulyk) in [#920](https://github.com/PHPOffice/PHPPresentation/pull/920)
25+
- Fixed the media part of an image whose path carries no extension being named with a trailing dot, and written under no content type, by [@dkulyk](http://github.com/dkulyk) in [#922](https://github.com/PHPOffice/PHPPresentation/pull/922)
2526

2627
## BC Breaks
2728
- `\PhpOffice\PhpPresentation\Slide\SlideMaster` is constructed without a background. A deck that relied on the white fill it used to write sets one with `setBackground()`, as [the documentation](https://github.com/PHPOffice/PHPPresentation/blob/develop/docs/usage/slides/introduction.md) shows.

src/PhpPresentation/Shape/Drawing/File.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,15 @@ public function getContents(): string
7575

7676
public function getExtension(): string
7777
{
78-
return pathinfo($this->getPath(), PATHINFO_EXTENSION);
78+
$extension = pathinfo($this->getPath(), PATHINFO_EXTENSION);
79+
if ('' !== $extension || !CommonFile::fileExists($this->getPath())) {
80+
return $extension;
81+
}
82+
83+
// a path is free to carry no extension; the contents still say what the image is
84+
$image = getimagesizefromstring(CommonFile::fileGetContents($this->getPath()));
85+
86+
return is_array($image) ? (string) image_type_to_extension($image[2], false) : '';
7987
}
8088

8189
public function getMimeType(): string

tests/PhpPresentation/Tests/Shape/Drawing/FileTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,21 @@ public function testPathWithoutVerifyFile(): void
5555
self::assertEmpty($object->getPath());
5656
}
5757

58+
public function testPathWithoutAnExtension(): void
59+
{
60+
$path = (string) tempnam(sys_get_temp_dir(), 'PhpPresentation');
61+
copy(dirname(__DIR__, 4) . '/resources/images/PhpPresentationLogo.png', $path);
62+
63+
$object = new File();
64+
$object->setPath($path);
65+
66+
// the name says nothing, so the contents have to
67+
self::assertEquals('png', $object->getExtension());
68+
self::assertStringEndsWith($object->getImageIndex() . '.png', $object->getIndexedFilename());
69+
70+
unlink($path);
71+
}
72+
5873
public function testPathWithRealFile(): void
5974
{
6075
$object = new File();

0 commit comments

Comments
 (0)