From 6aad54f441d127c54b7ebe88c13a336c2b2c66dc Mon Sep 17 00:00:00 2001 From: dubox Date: Sun, 23 Apr 2023 05:32:31 +0000 Subject: [PATCH 1/7] Update Html.php Add position attr for image whitch driving by MemoryDrawing --- src/PhpSpreadsheet/Writer/Html.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/PhpSpreadsheet/Writer/Html.php b/src/PhpSpreadsheet/Writer/Html.php index c30bb30acf..575197aecb 100644 --- a/src/PhpSpreadsheet/Writer/Html.php +++ b/src/PhpSpreadsheet/Writer/Html.php @@ -693,7 +693,8 @@ private function writeImageInCell(Worksheet $worksheet, $coordinates) // max-width: 100% ensures that image doesnt overflow containing cell // width: X sets width of supplied image. // As a result, images bigger than cell will be contained and images smaller will not get stretched - $html .= '' . $filedesc . ''; + $html .= '' . $filedesc . ''; } } } From 1b0d63b4d20e5c77bc6d424e3eacf5b44ab17f6f Mon Sep 17 00:00:00 2001 From: dubox Date: Thu, 11 May 2023 04:42:17 +0000 Subject: [PATCH 2/7] add unit test for pr#3535 --- .../Writer/Html/MemoryDrawingOffsetTest.php | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 tests/PhpSpreadsheetTests/Writer/Html/MemoryDrawingOffsetTest.php diff --git a/tests/PhpSpreadsheetTests/Writer/Html/MemoryDrawingOffsetTest.php b/tests/PhpSpreadsheetTests/Writer/Html/MemoryDrawingOffsetTest.php new file mode 100644 index 0000000000..240a028bad --- /dev/null +++ b/tests/PhpSpreadsheetTests/Writer/Html/MemoryDrawingOffsetTest.php @@ -0,0 +1,42 @@ +getActiveSheet(); + $drawing = new MemoryDrawing(); + $drawing->setImageResource(\imagecreatefromstring(file_get_contents("https://avatars.githubusercontent.com/u/1836015"))) //->setPath($this->getPath())// + ->setResizeProportional(false) //是否保持比例 + ->setWidthAndHeight($w, $h) //图片宽高,原始尺寸 100*100 + ->setOffsetX($x) + ->setOffsetY($y) + ->setWorksheet($sheet); + + $writer = new Html($spreadsheet); + $html = $writer->generateHtmlAll(); + self::assertStringContainsString('width:' . $w . 'px;left: ' .$x . 'px; top: ' . $y . 'px;position: absolute;', $html); + $spreadsheet->disconnectWorksheets(); + unset($spreadsheet); + } + + public function dataProvider() + { + return [ + [33, 19, 0 ,20], + [129, 110, 12,-3], + [55, 110, 33,42], + ]; + } +} From 9a68d59da9f208f83eb5bbb244aa29d0e4efec4e Mon Sep 17 00:00:00 2001 From: dubox Date: Thu, 11 May 2023 06:06:04 +0000 Subject: [PATCH 3/7] Fix code styles --- .../Writer/Html/MemoryDrawingOffsetTest.php | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/tests/PhpSpreadsheetTests/Writer/Html/MemoryDrawingOffsetTest.php b/tests/PhpSpreadsheetTests/Writer/Html/MemoryDrawingOffsetTest.php index 240a028bad..709d22739c 100644 --- a/tests/PhpSpreadsheetTests/Writer/Html/MemoryDrawingOffsetTest.php +++ b/tests/PhpSpreadsheetTests/Writer/Html/MemoryDrawingOffsetTest.php @@ -11,32 +11,37 @@ class MemoryDrawingOffsetTest extends TestCase { /** * @dataProvider dataProvider - */ - public function testMemoryDrawingOffset($w ,$h ,$x ,$y): void + */ + public function testMemoryDrawingOffset(int $w, int $h, int $x, int $y): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $drawing = new MemoryDrawing(); - $drawing->setImageResource(\imagecreatefromstring(file_get_contents("https://avatars.githubusercontent.com/u/1836015"))) //->setPath($this->getPath())// + if (($image = file_get_contents("https://avatars.githubusercontent.com/u/1836015")) === false) + throw new \Exception('image err1'); + if (($image = imagecreatefromstring($image)) === false) + throw new \Exception('image err2'); + + $drawing->setImageResource($image) ->setResizeProportional(false) //是否保持比例 ->setWidthAndHeight($w, $h) //图片宽高,原始尺寸 100*100 ->setOffsetX($x) ->setOffsetY($y) ->setWorksheet($sheet); - + $writer = new Html($spreadsheet); $html = $writer->generateHtmlAll(); - self::assertStringContainsString('width:' . $w . 'px;left: ' .$x . 'px; top: ' . $y . 'px;position: absolute;', $html); + self::assertStringContainsString('width:' . $w . 'px;left: ' . $x . 'px; top: ' . $y . 'px;position: absolute;', $html); $spreadsheet->disconnectWorksheets(); unset($spreadsheet); } - public function dataProvider() + public function dataProvider(): array { return [ - [33, 19, 0 ,20], - [129, 110, 12,-3], - [55, 110, 33,42], + [33, 19, 0, 20], + [129, 110, 12, -3], + [55, 110, 33, 42], ]; } } From 3e560bae114975f0412f35a20179668171bc6282 Mon Sep 17 00:00:00 2001 From: dubox Date: Thu, 11 May 2023 06:38:55 +0000 Subject: [PATCH 4/7] Fix code styles --- .../Writer/Html/MemoryDrawingOffsetTest.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/tests/PhpSpreadsheetTests/Writer/Html/MemoryDrawingOffsetTest.php b/tests/PhpSpreadsheetTests/Writer/Html/MemoryDrawingOffsetTest.php index 709d22739c..29d9206e22 100644 --- a/tests/PhpSpreadsheetTests/Writer/Html/MemoryDrawingOffsetTest.php +++ b/tests/PhpSpreadsheetTests/Writer/Html/MemoryDrawingOffsetTest.php @@ -2,10 +2,11 @@ namespace PhpOffice\PhpSpreadsheetTests\Writer\Html; +use Exception; use PhpOffice\PhpSpreadsheet\Spreadsheet; +use PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing; use PhpOffice\PhpSpreadsheet\Writer\Html; use PHPUnit\Framework\TestCase; -use PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing; class MemoryDrawingOffsetTest extends TestCase { @@ -17,10 +18,12 @@ public function testMemoryDrawingOffset(int $w, int $h, int $x, int $y): void $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $drawing = new MemoryDrawing(); - if (($image = file_get_contents("https://avatars.githubusercontent.com/u/1836015")) === false) - throw new \Exception('image err1'); - if (($image = imagecreatefromstring($image)) === false) - throw new \Exception('image err2'); + if (($image = file_get_contents('https://avatars.githubusercontent.com/u/1836015')) === false) { + throw new Exception('image err1'); + } + if (($image = imagecreatefromstring($image)) === false) { + throw new Exception('image err2'); + } $drawing->setImageResource($image) ->setResizeProportional(false) //是否保持比例 From 95109facd4344e23a61d41806ea9978bed2d2c66 Mon Sep 17 00:00:00 2001 From: dubox Date: Fri, 12 May 2023 06:48:34 +0000 Subject: [PATCH 5/7] use a local image instead of web image --- .../Writer/Html/MemoryDrawingOffsetTest.php | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/tests/PhpSpreadsheetTests/Writer/Html/MemoryDrawingOffsetTest.php b/tests/PhpSpreadsheetTests/Writer/Html/MemoryDrawingOffsetTest.php index 29d9206e22..959db2c5ab 100644 --- a/tests/PhpSpreadsheetTests/Writer/Html/MemoryDrawingOffsetTest.php +++ b/tests/PhpSpreadsheetTests/Writer/Html/MemoryDrawingOffsetTest.php @@ -17,14 +17,12 @@ public function testMemoryDrawingOffset(int $w, int $h, int $x, int $y): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); - $drawing = new MemoryDrawing(); - if (($image = file_get_contents('https://avatars.githubusercontent.com/u/1836015')) === false) { - throw new Exception('image err1'); - } - if (($image = imagecreatefromstring($image)) === false) { - throw new Exception('image err2'); - } + $image = file_get_contents(__DIR__ . '/../../../data/Reader/HTML/memoryDrawingTest.jpg'); + self::assertNotFalse($image, 'unable to read file'); + $image = imagecreatefromstring($image); + self::assertNotFalse($image, 'unable to create image from string'); + $drawing = new MemoryDrawing(); $drawing->setImageResource($image) ->setResizeProportional(false) //是否保持比例 ->setWidthAndHeight($w, $h) //图片宽高,原始尺寸 100*100 From 587449d35461ad5cd60a2abdcbeafc441b4c4dd4 Mon Sep 17 00:00:00 2001 From: dubox Date: Fri, 12 May 2023 06:56:11 +0000 Subject: [PATCH 6/7] add image for pr#3535 --- tests/data/Reader/HTML/memoryDrawingTest.jpg | Bin 0 -> 1765 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 tests/data/Reader/HTML/memoryDrawingTest.jpg diff --git a/tests/data/Reader/HTML/memoryDrawingTest.jpg b/tests/data/Reader/HTML/memoryDrawingTest.jpg new file mode 100644 index 0000000000000000000000000000000000000000..48afbc553115468258852063a2bd180f67667d24 GIT binary patch literal 1765 zcmb7EX;jl!9({k3e~1uB!V(~GSOh^r5fYXVt3gEwM8P2nB`nrJV44cj6m3BT=~Uy4 zB7=ZI4TK|M1gvpE1eHz7q5))y14@)O6|e-bg@i@f(r~mNrv1{n@0|DLzWcks`{6$A zbFC8Km_Z>y0D=Gr)*YZ-1>SqUILu4o9Zlrg$7k3x{}#wd;T_`Hdu<|nZz5ARQOPt< zGS$=9-ZP!%>675)=^GEUFF*i5=)nkB51|86AAy9?FuZOZY^bk~LK_(x8DR`D7-Os% z-WY3w#bEH3coTDig@uI?&WdPBAes>@2x~(ieIyb_!UiyGKrqG_6aH^#9|AlYZ~+$q ztp|E|2!V&RZNNqs5CVv`>v!nG5DJYzuG6t}8~`W)q4kj{!w)p1r%Qm+$D5cD93l$P zrt8W3z9}&RlRD^{HMLFOFx>{fT#xEvDQ z{4YQF_^jZ)%8K$;qae{DC=WB7B{>uDbGgrZ)!KNkjSsxDlP2eESaw?-KB#t=+~iye z>@P=9-|qoFJlwKO8&=yxvkaRITm}w6qqf_CeqIIuwma?m08rbj0ib4XTMLaC+t3ls zNqwAAL-|L~e_XjIE4mkOJ@XeVtfk8dCoX34F~oWjFTx^XW^|x4#9_W9xtJ$+Unv)l z{J?%Bq?Ded@U6>}7`!XWSo&~iS#nQW$c zjQ$GJ^`ByCY`AJ4CGs63M|gB#LxW82v>={2SJ3@&p3ALBBJFJ&bYtKx{=tf`MAVfL z940*fwTfr=qBwAG@OND;2?BbP79cAt53d|w`C@jfnI>mFrF^p{B)3pil~m0{H_iOg z?Z08?#3iY;v)+$|`}U29h5O~-^9$H5v-P{tn(FBE%cKkWm9?FL8ty3){dG;koNUjr zqgm{QG&iEjl5-sx^PWz1QAPc?1>lNLTCo$$Mo0QA0*{*N!2L;oKP9M`N6_ZgX z1;u^gg#5$*yz_cXo7pZO);&e+t%=2vH&diCpB++audQ&YL8EtW_h$43|>96-?_ZaYtrh=i2~_-T19CXHM+#YJ1e*U5V^?I^ztcYqeoJy zvVpR3B4tSWl)+SmEK$cis9C*TS`a0ZO@sT;)sd=)2{)-VCQWyO z7gQ=jQ7?Ul-E{g>&Mx0Fg=1=DXkz$iZFSG-n8-1gDN}g#sUwqhj!l#JxHZ4D)dDh` zbFC`P+*RFLsuJc#>^>2YlBQY;B=p1?T#soTG(FgpJU*F;f5}JP#}p+p`#$L?o>+V& zN+yj@H#1|!EwYwE?yCr<0*(2o$2BN`y|`LgM^!j2*R711+_da6JaG2f;&}R5%rjZi zUF1v0nEh(fu|`E#P3bAeV))ebe!H*szD+RRagM|$+?rq=%zWkXBR@`Jf0_8YD!#~B zy!AwGlen2QtJPvW4p( zmHeb4=k%m`wo%NP2hVwZF*27S(H<-8ptToo#9^`h03!0$jauSif;wdWe=NY7gWxY1 a7(7Ht+vAeG$K6MDUyg0|AD8#n4*dq;HRPB8 literal 0 HcmV?d00001 From 652f42d5aeee3a4db5ac17e1a866dba51e56b7a5 Mon Sep 17 00:00:00 2001 From: dubox Date: Fri, 12 May 2023 07:13:46 +0000 Subject: [PATCH 7/7] code style fix --- .../PhpSpreadsheetTests/Writer/Html/MemoryDrawingOffsetTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/PhpSpreadsheetTests/Writer/Html/MemoryDrawingOffsetTest.php b/tests/PhpSpreadsheetTests/Writer/Html/MemoryDrawingOffsetTest.php index 959db2c5ab..2cb2373c98 100644 --- a/tests/PhpSpreadsheetTests/Writer/Html/MemoryDrawingOffsetTest.php +++ b/tests/PhpSpreadsheetTests/Writer/Html/MemoryDrawingOffsetTest.php @@ -2,7 +2,6 @@ namespace PhpOffice\PhpSpreadsheetTests\Writer\Html; -use Exception; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing; use PhpOffice\PhpSpreadsheet\Writer\Html;