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

Xlsx Support Flipping of Image #3801

Merged
merged 3 commits into from
Nov 30, 2023
Merged
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
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.