Skip to content

Commit 71b2435

Browse files
committed
Ignore Fractional Part of Drawing Shadow Alpha
Fix #4415. We store the rarely-used property Drawing/Shadow/Alpha as an integer representing the percentage. Excel also stores it as an integer, but multiplies it by 1,000, so we divide by 1,000 when we read this value. This can, and in the case of the issue at hand does, leave a fractional portion. Php has deprecated passing a float with a fractional portion to an int argument, so the reporter saw a deprecation message. This is easily fixed.
1 parent 74dca30 commit 71b2435

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

src/PhpSpreadsheet/Reader/Xlsx.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,7 +1492,13 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
14921492
$shadow->setAlignment(self::getArrayItemString(self::getAttributes($outerShdw), 'algn'));
14931493
$clr = $outerShdw->srgbClr ?? $outerShdw->prstClr;
14941494
$shadow->getColor()->setRGB(self::getArrayItemString(self::getAttributes($clr), 'val'));
1495-
$shadow->setAlpha(self::getArrayItem(self::getAttributes($clr->alpha), 'val') / 1000); // @phpstan-ignore-line
1495+
if ($clr->alpha) {
1496+
$alpha = StringHelper::convertToString(self::getArrayItem(self::getAttributes($clr->alpha), 'val'));
1497+
if (is_numeric($alpha)) {
1498+
$alpha = (int) ($alpha / 1000);
1499+
$shadow->setAlpha($alpha);
1500+
}
1501+
}
14961502
}
14971503

14981504
$this->readHyperLinkDrawing($objDrawing, $oneCellAnchor, $hyperlinks);
@@ -1597,7 +1603,13 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
15971603
$shadow->setAlignment(self::getArrayItemString(self::getAttributes($outerShdw), 'algn'));
15981604
$clr = $outerShdw->srgbClr ?? $outerShdw->prstClr;
15991605
$shadow->getColor()->setRGB(self::getArrayItemString(self::getAttributes($clr), 'val'));
1600-
$shadow->setAlpha(self::getArrayItem(self::getAttributes($clr->alpha), 'val') / 1000); // @phpstan-ignore-line
1606+
if ($clr->alpha) {
1607+
$alpha = StringHelper::convertToString(self::getArrayItem(self::getAttributes($clr->alpha), 'val'));
1608+
if (is_numeric($alpha)) {
1609+
$alpha = (int) ($alpha / 1000);
1610+
$shadow->setAlpha($alpha);
1611+
}
1612+
}
16011613
}
16021614

16031615
$this->readHyperLinkDrawing($objDrawing, $twoCellAnchor, $hyperlinks);
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
6+
7+
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class Issue4415Test extends TestCase
11+
{
12+
private static string $file = 'tests/data/Reader/XLSX/issue.4415.xlsx';
13+
14+
public function testPreliminaries(): void
15+
{
16+
$file = 'zip://';
17+
$file .= self::$file;
18+
$file .= '#xl/drawings/drawing1.xml';
19+
$data = file_get_contents($file) ?: '';
20+
$expected = '<a:alpha val="72600"/>';
21+
self::assertStringContainsString($expected, $data);
22+
$expected = '<a:alpha val="90100"/>';
23+
self::assertStringContainsString($expected, $data);
24+
self::assertSame(2, substr_count($data, '<a:alpha '), 'number of drawings with alpha');
25+
self::assertSame(2, substr_count($data, '<xdr:oneCellAnchor>'), 'first 2 drawings');
26+
self::assertSame(1, substr_count($data, '<xdr:twoCellAnchor>'), 'third drawings');
27+
}
28+
29+
public function testFractionalAlpha(): void
30+
{
31+
$file = self::$file;
32+
$reader = new XlsxReader();
33+
$spreadsheet = $reader->load($file);
34+
$sheet = $spreadsheet->getActiveSheet();
35+
$drawings = $sheet->getDrawingCollection();
36+
self::assertCount(3, $drawings);
37+
self::assertNotNull($drawings[0]);
38+
self::assertNotNull($drawings[1]);
39+
self::assertNotNull($drawings[2]);
40+
self::assertSame(50, $drawings[0]->getShadow()->getAlpha());
41+
self::assertSame(72, $drawings[1]->getShadow()->getAlpha());
42+
self::assertSame('', $drawings[1]->getCoordinates2(), 'one cell anchor');
43+
self::assertSame(90, $drawings[2]->getShadow()->getAlpha());
44+
self::assertNotEquals('', $drawings[2]->getCoordinates2(), 'two cell anchor');
45+
$spreadsheet->disconnectWorksheets();
46+
}
47+
}
28.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)