Skip to content

Shape\Drawing\File : Name an image file whose path does not name itself - #922

Open
dkulyk wants to merge 1 commit into
PHPOffice:masterfrom
sapientpro:fix/extensionless-image
Open

Shape\Drawing\File : Name an image file whose path does not name itself#922
dkulyk wants to merge 1 commit into
PHPOffice:masterfrom
sapientpro:fix/extensionless-image

Conversation

@dkulyk

@dkulyk dkulyk commented Aug 22, 2026

Copy link
Copy Markdown
Contributor

Closes #603.

Description

What is wrong

Shape\Drawing\File::getExtension() derives the extension from the path alone:

return pathinfo($this->getPath(), PATHINFO_EXTENSION);

A path is free to carry no extension — an upload stored under its hash, a file from tempnam(), an image served from a route — and then the answer is ''. getIndexedFilename() builds the name of the media part out of it, so the part is called logo1., ending in a bare dot. Both writers register content types by extension, so that part is written under none.

The change

The contents say what the name does not, and the same call already backs getMimeType() a few lines below:

$image = getimagesizefromstring(CommonFile::fileGetContents($this->getPath()));

return is_array($image) ? (string) image_type_to_extension($image[2], false) : '';

It runs only when pathinfo() came back empty and the file exists, so a path that has an extension costs exactly what it cost before — no file is opened.

What it does not do

An extensionless SVG stays extensionless. getimagesizefromstring() reports nothing for it, and there is nothing else in the file for the decision to rest on. Neighbouring reports #741 and #819 are about the same family of images-loaded-from-content problems and are not touched here.

Checklist:

  • My CI is 🟢
  • I have covered by unit tests my new code (check build/coverage for coverage report)

    FileTest::testPathWithoutAnExtension — the repository's own PNG copied to a tempnam() path, so no binary fixture is added. Checked by putting the one-line getExtension() back: the test fails.

  • I have updated the documentation to describe the changes

    No documented API changed.

  • I have updated the changelog

@dkulyk
dkulyk force-pushed the fix/extensionless-image branch 2 times, most recently from 9de08f5 to 212105d Compare August 22, 2026 19:01
@coveralls

coveralls commented Aug 22, 2026

Copy link
Copy Markdown

Coverage Status

coverage: 92.161% (+0.002%) from 92.159% — sapientpro:fix/extensionless-image into PHPOffice:master

`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.
@dkulyk
dkulyk force-pushed the fix/extensionless-image branch from 212105d to 077f0f0 Compare August 23, 2026 12:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

CreateDrawingShape cannot insert image without file extension

3 participants