Skip to content

Commit

Permalink
Update Html.php (#3535)
Browse files Browse the repository at this point in the history
* Update Html.php

Add position attr for image whitch driving by MemoryDrawing

* add unit test for pr#3535

* Fix code styles

* Fix code styles

* use a local image instead of  web image

* add image for pr#3535

* code style fix
  • Loading branch information
dubox authored May 12, 2023
1 parent 25cea15 commit 40203ff
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/PhpSpreadsheet/Writer/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 .= '<img alt="' . $filedesc . '" src="' . $dataUri . '" style="max-width:100%;width:' . $drawing->getWidth() . 'px;" />';
$html .= '<img alt="' . $filedesc . '" src="' . $dataUri . '" style="max-width:100%;width:' . $drawing->getWidth() . 'px;left: ' .
$drawing->getOffsetX() . 'px; top: ' . $drawing->getOffsetY() . 'px;position: absolute; z-index: 1;" />';
}
}
}
Expand Down
47 changes: 47 additions & 0 deletions tests/PhpSpreadsheetTests/Writer/Html/MemoryDrawingOffsetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace PhpOffice\PhpSpreadsheetTests\Writer\Html;

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing;
use PhpOffice\PhpSpreadsheet\Writer\Html;
use PHPUnit\Framework\TestCase;

class MemoryDrawingOffsetTest extends TestCase
{
/**
* @dataProvider dataProvider
*/
public function testMemoryDrawingOffset(int $w, int $h, int $x, int $y): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();

$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
->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(): array
{
return [
[33, 19, 0, 20],
[129, 110, 12, -3],
[55, 110, 33, 42],
];
}
}
Binary file added tests/data/Reader/HTML/memoryDrawingTest.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 40203ff

Please sign in to comment.