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 5 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
50 changes: 50 additions & 0 deletions tests/PhpSpreadsheetTests/Writer/Html/MemoryDrawingOffsetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

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;

class MemoryDrawingOffsetTest extends TestCase
{
/**
* @dataProvider dataProvider
*/
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');
oleibman marked this conversation as resolved.
Show resolved Hide resolved
}
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);
$spreadsheet->disconnectWorksheets();
unset($spreadsheet);
}

public function dataProvider(): array
{
return [
[33, 19, 0, 20],
[129, 110, 12, -3],
[55, 110, 33, 42],
];
}
}