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
21 changes: 13 additions & 8 deletions src/PhpSpreadsheet/Reader/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PhpOffice\PhpSpreadsheet\Reader;

use Composer\Pcre\Preg;
use DOMAttr;
use DOMDocument;
use DOMElement;
Expand Down Expand Up @@ -180,7 +181,7 @@ public function canRead(string $filename): bool
return false;
}

$beginning = preg_replace(self::STARTS_WITH_BOM, '', $this->readBeginning()) ?? '';
$beginning = Preg::replace(self::STARTS_WITH_BOM, '', $this->readBeginning());

$startWithTag = self::startsWithTag($beginning);
$containsTags = self::containsTags($beginning);
Expand Down Expand Up @@ -457,7 +458,7 @@ private function processDomElementSpanEtc(Worksheet $sheet, int &$row, string &$
}
if (isset($attributeArray['style'])) {
$alignStyle = $attributeArray['style'];
if (preg_match('/\btext-align:\s*(left|right|center|justify)\b/', (string) $alignStyle, $matches) === 1) {
if (Preg::isMatch('/\btext-align:\s*(left|right|center|justify)\b/', (string) $alignStyle, $matches)) {
$sheet->getComment($column . $row)->setAlignment($matches[1]);
}
}
Expand Down Expand Up @@ -772,7 +773,7 @@ protected function processDomElement(DOMNode $element, Worksheet $sheet, int &$r
{
foreach ($element->childNodes as $child) {
if ($child instanceof DOMText) {
$domText = (string) preg_replace('/\s+/', ' ', trim($child->nodeValue ?? ''));
$domText = Preg::replace('/\s+/', ' ', trim($child->nodeValue ?? ''));
if ($domText === "\u{a0}") {
$domText = '';
}
Expand Down Expand Up @@ -883,7 +884,7 @@ private static function loadProperties(DOMDocument $dom, Spreadsheet $spreadshee

break;
default:
if (preg_match('/^custom[.](bool|date|float|int|string)[.](.+)$/', $metaName, $matches) === 1) {
if (Preg::isMatch('/^custom[.](bool|date|float|int|string)[.](.+)$/', $metaName, $matches)) {
match ($matches[1]) {
'bool' => $properties->setCustomProperty($matches[2], (bool) $metaContent, Properties::PROPERTY_TYPE_BOOLEAN),
'float' => $properties->setCustomProperty($matches[2], (float) $metaContent, Properties::PROPERTY_TYPE_FLOAT),
Expand All @@ -910,13 +911,12 @@ private static function replaceNonAscii(array $matches): string
/** @internal */
protected static function replaceNonAsciiIfNeeded(string $convert): ?string
{
if (preg_match(self::STARTS_WITH_BOM, $convert) !== 1 && preg_match(self::DECLARES_CHARSET, $convert) !== 1) {
if (!Preg::isMatch(self::STARTS_WITH_BOM, $convert) && !Preg::isMatch(self::DECLARES_CHARSET, $convert)) {
$lowend = "\u{80}";
$highend = "\u{10ffff}";
$regexp = "/[$lowend-$highend]/u";
/** @var callable $callback */
$callback = [self::class, 'replaceNonAscii'];
$convert = preg_replace_callback($regexp, $callback, $convert);
// use native preg because of "u" modifier
$convert = preg_replace_callback($regexp, self::replaceNonAscii(...), $convert);
}

return $convert;
Expand Down Expand Up @@ -1274,6 +1274,11 @@ private function insertImage(Worksheet $sheet, string $column, int $row, array $
$drawing->setOpacity((int) ($opacity * 100000));
}
}
/** @var string */
$transform = $styleArray['transform'] ?? '';
if (Preg::isMatch('/rotate[(](-?\d{1,3})deg[)]$/', $transform, $matches)) {
$drawing->setRotation((int) $matches[1]);
}
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/PhpSpreadsheet/Writer/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,11 @@ private function writeImageInCell(string $coordinates): string
$opacity = "opacity:$opacityValue; ";
}
}
$rotationValue = $drawing->getRotation();
if ($rotationValue !== 0) {
$rotation = "transform: rotate({$rotationValue}deg); ";
$opacity .= $rotation;
}
$filedesc = $drawing->getDescription();
$filedesc = $filedesc ? htmlspecialchars($filedesc, ENT_QUOTES) : 'Embedded image';
if ($drawing instanceof Drawing && $drawing->getPath() !== '') {
Expand Down
38 changes: 38 additions & 0 deletions tests/PhpSpreadsheetTests/Writer/Html/ImageRotateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace PhpOffice\PhpSpreadsheetTests\Writer\Html;

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;

class ImageRotateTest extends AbstractFunctional
{
public function testImageCopyXls(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();

$drawing = new Drawing();
$drawing->setName('Blue Square');
$drawing->setDescription('Blue_Square');
$drawing->setPath('samples/images/blue_square.png');
$drawing->setCoordinates('C5');
$drawing->setRotation(45);
$drawing->setWorksheet($sheet);

$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Html');
$spreadsheet->disconnectWorksheets();

$rsheet = $reloadedSpreadsheet->getActiveSheet();
$drawings = $rsheet->getDrawingCollection();
self::assertCount(1, $drawings);
$drawing = $drawings[0];
self::assertNotNull($drawing);
self::assertSame(45, $drawing->getRotation());

$reloadedSpreadsheet->disconnectWorksheets();
}
}
Loading