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

Image does not appear in Excel file #3843

Closed
1 of 8 tasks
1X9X8X4 opened this issue Dec 27, 2023 · 6 comments · Fixed by #3846
Closed
1 of 8 tasks

Image does not appear in Excel file #3843

1X9X8X4 opened this issue Dec 27, 2023 · 6 comments · Fixed by #3846

Comments

@1X9X8X4
Copy link

1X9X8X4 commented Dec 27, 2023

This is:

- [x] a bug report
- [ ] **not** a usage question (ask them on https://stackoverflow.com/questions/tagged/phpspreadsheet or https://gitter.im/PHPOffice/PhpSpreadsheet)

What is the expected behavior?

Image appears in Excel file.

What is the current behavior?

Image does not appear in Excel file.

What are the steps to reproduce?

The phenomenon occurs when the following code is executed
I would like to display an image in excel loaded using phpspreadsheet but it does not show up.
I have confirmed that the text is displayed.
I would appreciate it if you could show me how to get the images to display.
"template.xlsx" is not default Excel file.
template.xlsx

<?php

require __DIR__ . '/vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Writer\Xlsx as XlsxWriter;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;

$filePath = __DIR__ . '/template.xlsx';
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($filePath);
$worksheet = $spreadsheet->getSheetByName('Sheet1');

$worksheet->setCellValue('A1', 'test');

$image_path = __DIR__ . '/images/test.jpg';
$drawing = new Drawing();
$drawing->setPath($image_path);
$drawing->setCoordinates('A2');
$drawing->setWidth(100);
$drawing->setHeight(100);
$drawing->setResizeProportional(false);
$drawing->setWorksheet($worksheet);
	
$fileName = 'test.xlsx';
$savePath = __DIR__ . '/test.xlsx';
$writer = new XlsxWriter($spreadsheet);
$writer->save($savePath);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;');
header("Content-Disposition: attachment; filename=\"{$fileName}\"");
header('Cache-Control: max-age=0');
readfile($savePath);
exit;

What features do you think are causing the issue

  • Reader
  • Writer
  • Styles
  • Data Validations
  • Formula Calculations
  • Charts
  • AutoFilter
  • Form Elements

Which versions of PhpSpreadsheet and PHP are affected?

PhpSpreadsheet : 1.29.0
PHP : 7.4.33

@1X9X8X4 1X9X8X4 changed the title Images do not appear in Excel file Image does not appear in Excel file Dec 27, 2023
@oleibman
Copy link
Collaborator

Unable to duplicate. I used your code with a template and image file of my own, and the resulting spreadsheet contained the image. Perhaps if you upload your file here, I can try to see what might be wrong.

@1X9X8X4
Copy link
Author

1X9X8X4 commented Dec 28, 2023

Sorry, there was one mistake.
template.xlsx is a non-default file.
Please check it.
template.xlsx

And test.jpg is here.
test

@oleibman
Copy link
Collaborator

Thank you for uploading the file. This is a strange case. The xml in your template file has references to some drawings (possibly textbox form controls) which apparently are not part of the worksheet, and, when we add a new drawing, PhpSpreadsheet is getting confused about which drawings are needed. While I study what needs to be done, you might be able to use this edited template, which does not have the unused drawings, in place of your template:
issue.3843a.template.edited.xlsx

@1X9X8X4
Copy link
Author

1X9X8X4 commented Dec 29, 2023

Thank you for the issue.3843a.template.edited.xlsx.
I can confirm that the images are displayed.
What is the difference between that file and the template.xlsx I attached?

@oleibman
Copy link
Collaborator

I removed the (unused) data that was being handled incorrectly.

oleibman added a commit to oleibman/PhpSpreadsheet that referenced this issue Dec 29, 2023
Fix PHPOffice#3843. Spreadsheet XML had a lingering Xml drawing tag despite having no drawings. When a new drawing was added, PhpSpreadsheet became confused about which drawings to keep. This PR ensures that it keeps the new and drops the old.
@1X9X8X4
Copy link
Author

1X9X8X4 commented Dec 29, 2023

I see.
It was very helpful.
Thank you very much.

@1X9X8X4 1X9X8X4 closed this as completed Dec 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants