Skip to content

Conversation

@raziel057
Copy link

@raziel057 raziel057 commented Oct 3, 2025

Support for read & write of Image in Cell - Related to #4014

I must admit it was really complex to understand the relations between all xml (richData) files that have to be created / updated. I lost a lot of time comparing the structure with files edited in Excel and specifically figuring out the metadata.xml file had to be largely changed as soon as images are added in cells.

This branch is based on #4664

This is:

  • a bugfix
  • a new feature
  • refactoring
  • additional unit tests

Checklist:

  • Changes are covered by unit tests
    • Changes are covered by existing unit tests
    • New unit tests have been added
  • Code style is respected
  • Commit message explains why the change is made (see https://github.com/erlang/otp/wiki/Writing-good-commit-messages)
  • CHANGELOG.md contains a short summary of the change and a link to the pull request if applicable
  • Documentation is updated as necessary

Support for read and write of Image in Cell - Related to #4014

Why this change is needed?

When an image is inserted in a cell in an XLSX document, PhpSpreadsheet is currently not able to read it. This PR allow to collect those images in order to find them when calling $objWorksheet->getInCellDrawingCollection(). It's also possible to get the Drawing object from from the related Cell.

The code is covered by tests and have been tested in real application - As shown bellow:
image

When I import the file with image inserted in the Cell, I have it now in $objWorksheet->getInCellDrawingCollection():

image

@raziel057 raziel057 mentioned this pull request Oct 3, 2025
11 tasks
Comment on lines 966 to 967
$objDrawing->setOffsetX(0);
$objDrawing->setOffsetY(0);
Copy link

@rapaelector rapaelector Oct 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

those can be remove as it is by default as 0

@oleibman
Copy link
Collaborator

oleibman commented Nov 2, 2025

I am sorry I have not been able to give this the attention it needs till now. At any rate, for starters, what is the difference between the two new files named drawing_in_cell.xlsx? If there is no difference, can you remove one of them? If there is a difference, can you rename one of them? We allow name conflicts like this, but, on a big change like this one, I find it too confusing.

I will also mention that I am getting an "invalid image" message on one of my systems when I try to open those files. It seems to open okay anyhow, and, when I try it on a machine with different software, the error message doesn't happen. So, the error is far more likely to be with my system than with your file, but I at least need to ask if you are aware of anything "funny" about the images you are using? Can you tell me what they are, and where I am supposed to find them on the spreadsheets?

Do you allow for the possibility of embedded and non-embedded images on the same worksheet? What about using both embedded and non-embedded for the same image on the same worksheet?

@oleibman
Copy link
Collaborator

oleibman commented Nov 2, 2025

You have already some comments to issue #4280. Please take a look at one of the spreadsheets uploaded there.
https://github.com/user-attachments/files/18397406/Simulateur_B_to_C_8.1_SANS_MACRO_2025.-.copieV2.xlsx
When I load and save this spreadsheet using your code, both the in-cell (e.g. sheet Accueil cell B8) and out-of-cell (e.g. sheet "Rénovez votre maison") images appear to be handled correctly. This is a very good development. However, Excel has some problems with the copy:

<recoveryLog>
<logFileName>error205120_01.xml</logFileName>
<summary>
Errors were detected in file 'C:\git\issue.4280.nopw.3.xlsx'
</summary>
<repairedRecords>
<repairedRecord>
Repaired Records: Cell information from /xl/worksheets/sheet40.xml part
</repairedRecord>
<repairedRecord>
Repaired Records: Cell information from /xl/worksheets/sheet46.xml part
</repairedRecord>
<repairedRecord>
Repaired Records: Cell information from /xl/worksheets/sheet48.xml part
</repairedRecord>
<repairedRecord>
Repaired Records: Cell information from /xl/worksheets/sheet49.xml part
</repairedRecord>
</repairedRecords>
</recoveryLog>

I am not sure offhand what sheets 40, 46, 48, and 49 are. I will try to do more research. But we do not see these messages when using the production version of PhpSpreadsheet to load and save, so it is possible that your code has introduced these problems. The spreadsheet is very complicated, so resolving these problems isn't necessarily something I need your change to be able to do, but it might be.

@oleibman
Copy link
Collaborator

oleibman commented Nov 2, 2025

Here are the affected sheets:
Sheet40 is "Produisez votre électricité", which has no in-cell pictures.
Sheet46 is "Annexe 3", which has an in-cell picture in B6. The sheet is protected.
Sheet48 is "Paramètres", which has no in-cell pictures. The sheet is hidden.
Sheet49 is "Paramètres Lesosai", which has in-cell pictures at A5 N5 A22 A40. The sheet is hidden.
I have asked @kboumedal if we can get a version of the spreadsheet where protection and hidden is turned off for these sheets.

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.

3 participants