Skip to content

Commit

Permalink
Xlsx Support Flipping of Image (#3801)
Browse files Browse the repository at this point in the history
* Xlsx Support Flipping of Image

Fix #731. Opened over 5 years ago, probably the second oldest problem I've worked on. Images attached to an Xlsx spreadsheet can be rotated, which is supported by PhpSpreadsheet. They can also be flipped along their horizontal and/or vertical axes, and that has not been supported. This PR adds that support.

* Update CHANGELOG.md
  • Loading branch information
oleibman authored Nov 30, 2023
1 parent 009e009 commit 9fcfa4b
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ and this project adheres to [Semantic Versioning](https://semver.org).
- Writer ODS : Write Border Style for cells [Issue #3690](https://github.com/PHPOffice/PhpSpreadsheet/issues/3690) [PR #3693](https://github.com/PHPOffice/PhpSpreadsheet/pull/3693)
- Sheet Background Images [Issue #1649](https://github.com/PHPOffice/PhpSpreadsheet/issues/1649) [PR #3795](https://github.com/PHPOffice/PhpSpreadsheet/pull/3795)
- Check if Coordinate is Inside Range [PR #3779](https://github.com/PHPOffice/PhpSpreadsheet/pull/3779)
- Flipping Images [Issue #731](https://github.com/PHPOffice/PhpSpreadsheet/issues/731) [PR #3801](https://github.com/PHPOffice/PhpSpreadsheet/pull/3801)
- Chart Dynamic Title and Font Properties [Issue #3797](https://github.com/PHPOffice/PhpSpreadsheet/issues/3797) [PR #3800](https://github.com/PHPOffice/PhpSpreadsheet/pull/3800)

### Changed

Expand Down
4 changes: 4 additions & 0 deletions src/PhpSpreadsheet/Reader/Xlsx.php
Original file line number Diff line number Diff line change
Expand Up @@ -1424,6 +1424,8 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
$objDrawing->setHeight(Drawing::EMUToPixels(self::getArrayItem(self::getAttributes($oneCellAnchor->ext), 'cy')));
if ($xfrm) {
$objDrawing->setRotation((int) Drawing::angleToDegrees(self::getArrayItem(self::getAttributes($xfrm), 'rot')));
$objDrawing->setFlipVertical((bool) self::getArrayItem(self::getAttributes($xfrm), 'flipV'));
$objDrawing->setFlipHorizontal((bool) self::getArrayItem(self::getAttributes($xfrm), 'flipH'));
}
if ($outerShdw) {
$shadow = $objDrawing->getShadow();
Expand Down Expand Up @@ -1518,6 +1520,8 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
$objDrawing->setWidth(Drawing::EMUToPixels(self::getArrayItem(self::getAttributes($xfrm->ext), 'cx')));
$objDrawing->setHeight(Drawing::EMUToPixels(self::getArrayItem(self::getAttributes($xfrm->ext), 'cy')));
$objDrawing->setRotation(Drawing::angleToDegrees(self::getArrayItem(self::getAttributes($xfrm), 'rot')));
$objDrawing->setFlipVertical((bool) self::getArrayItem(self::getAttributes($xfrm), 'flipV'));
$objDrawing->setFlipHorizontal((bool) self::getArrayItem(self::getAttributes($xfrm), 'flipH'));
}
if ($outerShdw) {
$shadow = $objDrawing->getShadow();
Expand Down
28 changes: 28 additions & 0 deletions src/PhpSpreadsheet/Worksheet/BaseDrawing.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ class BaseDrawing implements IComparable
*/
protected $rotation = 0;

protected bool $flipVertical = false;

protected bool $flipHorizontal = false;

/**
* Shadow.
*/
Expand Down Expand Up @@ -542,4 +546,28 @@ public function setSrcRect($srcRect): self

return $this;
}

public function setFlipHorizontal(bool $flipHorizontal): self
{
$this->flipHorizontal = $flipHorizontal;

return $this;
}

public function getFlipHorizontal(): bool
{
return $this->flipHorizontal;
}

public function setFlipVertical(bool $flipVertical): self
{
$this->flipVertical = $flipVertical;

return $this;
}

public function getFlipVertical(): bool
{
return $this->flipVertical;
}
}
9 changes: 9 additions & 0 deletions src/PhpSpreadsheet/Writer/Xlsx/Drawing.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ public function writeDrawing(XMLWriter $objWriter, BaseDrawing $drawing, $relati
// a:xfrm
$objWriter->startElement('a:xfrm');
$objWriter->writeAttribute('rot', (string) SharedDrawing::degreesToAngle($drawing->getRotation()));
self::writeAttributeIf($objWriter, $drawing->getFlipVertical(), 'flipV', '1');
self::writeAttributeIf($objWriter, $drawing->getFlipHorizontal(), 'flipH', '1');
if ($isTwoCellAnchor) {
$objWriter->startElement('a:ext');
$objWriter->writeAttribute('cx', self::stringEmu($drawing->getWidth()));
Expand Down Expand Up @@ -579,4 +581,11 @@ private static function stringEmu(int $pixelValue): string
{
return (string) SharedDrawing::pixelsToEMU($pixelValue);
}

private static function writeAttributeIf(XMLWriter $objWriter, ?bool $condition, string $attr, string $val): void
{
if ($condition) {
$objWriter->writeAttribute($attr, $val);
}
}
}
38 changes: 38 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/Xlsx/Issue731Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;

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

class Issue731Test extends AbstractFunctional
{
private static string $testbook = 'tests/data/Reader/XLSX/issue.731.xlsx';

public function testRotateAndFlipImages(): void
{
$reader = new Xlsx();
$spreadsheet = $reader->load(self::$testbook);
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx');
$spreadsheet->disconnectWorksheets();
$reloadedSheet = $reloadedSpreadsheet->getActiveSheet();
$expected = [
[0, false, false],
[90, false, false],
[270, false, false],
[0, false, true],
[0, true, false],
[20, false, false],
[20, false, true],
[0, true, true],
];
$actual = [];
foreach ($reloadedSheet->getDrawingCollection() as $drawing) {
$actual[] = [$drawing->getRotation(), $drawing->getFlipHorizontal(), $drawing->getFlipVertical()];
}
self::assertSame($expected, $actual);
$reloadedSpreadsheet->disconnectWorksheets();
}
}
Binary file added tests/data/Reader/XLSX/issue.731.xlsx
Binary file not shown.

0 comments on commit 9fcfa4b

Please sign in to comment.