Skip to content
Open
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
36 changes: 36 additions & 0 deletions src/PhpSpreadsheet/Reader/Xlsx.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php

Check warning on line 1 in src/PhpSpreadsheet/Reader/Xlsx.php

View workflow job for this annotation

GitHub Actions / php-cs-fixer

Found violation(s) of type: no_unused_imports

namespace PhpOffice\PhpSpreadsheet\Reader;

Expand Down Expand Up @@ -38,6 +38,7 @@
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use PhpOffice\PhpSpreadsheet\Style\Style;
use PhpOffice\PhpSpreadsheet\Worksheet\HeaderFooterDrawing;
use PhpOffice\PhpSpreadsheet\Worksheet\RichValueDrawing;
use PhpOffice\PhpSpreadsheet\Worksheet\Table\TableDxfsStyle;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use SimpleXMLElement;
Expand Down Expand Up @@ -784,6 +785,19 @@

$charts = $chartDetails = [];

// Add richData (contains relation of in-cell images)
$richData = [];
$relationsFileName = $dir . '/richData/_rels/richValueRel.xml.rels';
if ($zip->locateName($relationsFileName)) {
$relsWorksheet = $this->loadZip($relationsFileName, Namespaces::RELATIONSHIPS);
foreach ($relsWorksheet->Relationship as $elex) {
$ele = self::getAttributes($elex);
if ($ele['Type'] == Namespaces::IMAGE) {
$richData['image'][(string) $ele['Id']] = (string) $ele['Target'];
}
}
}

$sheetCreated = false;
if ($xmlWorkbookNS->sheets) {
foreach ($xmlWorkbookNS->sheets->sheet as $eleSheet) {
Expand Down Expand Up @@ -939,7 +953,29 @@
}

break;
case DataType::TYPE_ERROR:

Check failure on line 956 in src/PhpSpreadsheet/Reader/Xlsx.php

View workflow job for this annotation

GitHub Actions / phpcs

The CASE body must start on the line following the statement

if (isset($cAttr->vm, $richData['image']['rId' . $cAttr->vm]) && !$useFormula) {
$imagePath = $dir . '/' . str_replace('../', '', $richData['image']['rId' . $cAttr->vm]);
$objDrawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
$objDrawing
->setPath(
'zip://' . File::realpath($filename) . '#' . $imagePath,
false,
$zip
)
->setInCell(true)
->setCoordinates($r)
->setResizeProportional(false)
->setWorksheet($docSheet);

$value = $objDrawing;
$cellDataType = DataType::TYPE_NULL;
$c->t = DataType::TYPE_NULL;

break;
}

if (!$useFormula) {
$value = self::castToError($c);
} else {
Expand Down
5 changes: 5 additions & 0 deletions src/PhpSpreadsheet/Reader/Xlsx/Namespaces.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,9 @@ class Namespaces
const DYNAMIC_ARRAY = 'http://schemas.microsoft.com/office/spreadsheetml/2017/dynamicarray';

const DYNAMIC_ARRAY_RICHDATA = 'http://schemas.microsoft.com/office/spreadsheetml/2017/richdata';

/**
* @see https://learn.microsoft.com/en-us/openspecs/office_standards/ms-xlsx/896934fd-8df7-43f4-b154-2d39371c270d
*/
const RICH_VALUE_DATA = 'http://schemas.microsoft.com/office/2017/06/relationships/rdRichValue';
}
21 changes: 21 additions & 0 deletions src/PhpSpreadsheet/Worksheet/Drawing.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php

Check warning on line 1 in src/PhpSpreadsheet/Worksheet/Drawing.php

View workflow job for this annotation

GitHub Actions / php-cs-fixer

Found violation(s) of type: phpdoc_summary

Check warning on line 1 in src/PhpSpreadsheet/Worksheet/Drawing.php

View workflow job for this annotation

GitHub Actions / php-cs-fixer

Found violation(s) of type: no_superfluous_phpdoc_tags

namespace PhpOffice\PhpSpreadsheet\Worksheet;

Expand All @@ -24,6 +24,11 @@
*/
private bool $isUrl;

/**
* check if the drawing is in cell so should be a richData image
*/
private bool $inCell = false;

/**
* Create a new Drawing.
*/
Expand Down Expand Up @@ -246,4 +251,20 @@

return image_type_to_mime_type(self::IMAGE_TYPES_CONVERTION_MAP[$this->type]);
}

/**
* @param bool $inCell
* @return Drawing
*/
public function setInCell(bool $inCell): static

Check failure on line 259 in src/PhpSpreadsheet/Worksheet/Drawing.php

View workflow job for this annotation

GitHub Actions / phpstan

PHPDoc tag `@return` with type PhpOffice\PhpSpreadsheet\Worksheet\Drawing is not subtype of native type static(PhpOffice\PhpSpreadsheet\Worksheet\Drawing).
{
$this->inCell = $inCell;

return $this;
}

public function getInCell(): bool
{
return $this->inCell;
}
}
2 changes: 2 additions & 0 deletions src/PhpSpreadsheet/Writer/Xlsx.php
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,8 @@ public function save($filename, int $flags = 0): void
if (($drawingCount > 0) || ($chartCount > 0)) {
// Drawing relationships
$zipContent['xl/drawings/_rels/drawing' . ($i + 1) . '.xml.rels'] = $this->getWriterPartRels()->writeDrawingRelationships($this->spreadSheet->getSheet($i), $chartRef1, $this->includeCharts);
// richData relationships
$zipContent['xl/richData/_rels/richValueRel.xml.rels'] = $this->getWriterPartRels()->writeDrawingRelationships($this->spreadSheet->getSheet($i), $chartRef1, $this->includeCharts);

// Drawings
$zipContent['xl/drawings/drawing' . ($i + 1) . '.xml'] = $this->getWriterPartDrawing()->writeDrawings($this->spreadSheet->getSheet($i), $this->includeCharts);
Expand Down
36 changes: 36 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/Xlsx/DrawingInCellTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;

use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PHPUnit\Framework\TestCase;

class DrawingInCellTest extends TestCase
{
public function testPictureInCell(): void
{
$file = 'tests/data/Reader/XLSX/drawing_in_cell.xlsx';
$reader = new Xlsx();
$spreadsheet = $reader->load($file);
$sheet = $spreadsheet->getActiveSheet();
$drawingCollection = $sheet->getDrawingCollection();
self::assertCount(1, $drawingCollection);

if ($drawingCollection[0] === null) {
self::fail('Unexpected null drawing');
} else {
self::assertSame(IMAGETYPE_PNG, $drawingCollection[0]->getType());
self::assertSame('B2', $drawingCollection[0]->getCoordinates());
self::assertSame(0, $drawingCollection[0]->getOffsetX());
self::assertSame(0, $drawingCollection[0]->getOffsetY());
self::assertSame(296, $drawingCollection[0]->getWidth());
self::assertSame(154, $drawingCollection[0]->getHeight());
self::assertSame(296, $drawingCollection[0]->getImageWidth());
self::assertSame(154, $drawingCollection[0]->getImageHeight());
}

$spreadsheet->disconnectWorksheets();
}
}
33 changes: 33 additions & 0 deletions tests/PhpSpreadsheetTests/Writer/Xlsx/RichValueDrawingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace PhpOffice\PhpSpreadsheetTests\Writer\Xlsx;

use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;

class RichValueDrawingTest extends AbstractFunctional
{
/**
* Test save and load XLSX file with drawing on 2nd worksheet.
*/
public function testSaveAfterInsertingRows(): void
{
// Read spreadsheet from file
$inputFilename = 'tests/data/Writer/XLSX/drawing_in_cell.xlsx';
$reader = new Xlsx();
$spreadsheet = $reader->load($inputFilename);
$sheet = $spreadsheet->getActiveSheet();
$drawingCollection = $sheet->getDrawingCollection();

// Save spreadsheet to file and read it back
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx');
$spreadsheet->disconnectWorksheets();

$rsheet = $reloadedSpreadsheet->getActiveSheet();
$drawingCollection2 = $rsheet->getDrawingCollection();

$reloadedSpreadsheet->disconnectWorksheets();
}
}
Binary file added tests/data/Reader/XLSX/drawing_in_cell.xlsx
Binary file not shown.
3 changes: 3 additions & 0 deletions tests/data/Reader/XLSX/drawing_in_cell.xlsx:Zone.Identifier
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
HostUrl=https://github.com/
Binary file added tests/data/Writer/XLSX/drawing_in_cell.xlsx
Binary file not shown.
Loading