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

Update Html.php #3535

Merged
merged 10 commits into from
May 12, 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
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.