mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-10-04 02:48:26 +00:00
2bb1c9df3c
A dependency checker found the following minor problems: - `TIMEVALUE` uses the wrong case for `DateTime`. - The `filter` extension is used but is not declared as a dependency. - The `exif` extension is used but is not declared as a dependency. Php tolerates the first, but the solution is so trivial that it might as well be applied. You have to go out of your way to not include `filter` - is it almost always builtin. Nevertheless, it shouldn't hurt to explicitly declare it. `exif` is used in only one place, and, if it's not available there, we fall back to `GD`, which *is* a dependency. Getting rid of the `exif` portion should be harmless.
97 lines
3.4 KiB
PHP
97 lines
3.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Worksheet;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Exception;
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class MemoryDrawingTest extends TestCase
|
|
{
|
|
public function testMemoryDrawing(): void
|
|
{
|
|
$name = 'In-Memory image';
|
|
$gdImage = @imagecreatetruecolor(120, 20);
|
|
if ($gdImage === false) {
|
|
self::markTestSkipped('Unable to create GD Image for MemoryDrawing');
|
|
}
|
|
|
|
$textColor = (int) imagecolorallocate($gdImage, 255, 255, 255);
|
|
imagestring($gdImage, 1, 5, 5, 'Created with PhpSpreadsheet', $textColor);
|
|
|
|
$drawing = new MemoryDrawing();
|
|
$drawing->setName($name);
|
|
$drawing->setDescription('In-Memory image 1');
|
|
$drawing->setCoordinates('A1');
|
|
$drawing->setImageResource($gdImage);
|
|
$drawing->setRenderingFunction(MemoryDrawing::RENDERING_PNG);
|
|
$drawing->setMimeType(MemoryDrawing::MIMETYPE_PNG);
|
|
|
|
self::assertIsObject($drawing->getImageResource());
|
|
|
|
self::assertSame(MemoryDrawing::MIMETYPE_DEFAULT, $drawing->getMimeType());
|
|
self::assertSame(MemoryDrawing::RENDERING_DEFAULT, $drawing->getRenderingFunction());
|
|
}
|
|
|
|
public function testMemoryDrawingFromString(): void
|
|
{
|
|
$imageFile = __DIR__ . '/../../data/Worksheet/officelogo.jpg';
|
|
|
|
$imageString = file_get_contents($imageFile);
|
|
if ($imageString === false) {
|
|
self::markTestSkipped('Unable to read Image file for MemoryDrawing');
|
|
}
|
|
$drawing = MemoryDrawing::fromString($imageString);
|
|
|
|
self::assertIsObject($drawing->getImageResource());
|
|
|
|
self::assertSame(MemoryDrawing::MIMETYPE_JPEG, $drawing->getMimeType());
|
|
self::assertSame(MemoryDrawing::RENDERING_JPEG, $drawing->getRenderingFunction());
|
|
}
|
|
|
|
public function testMemoryDrawingFromInvalidString(): void
|
|
{
|
|
$this->expectException(Exception::class);
|
|
$this->expectExceptionMessage('Value cannot be converted to an image');
|
|
|
|
$imageString = 'I am not an image';
|
|
MemoryDrawing::fromString($imageString);
|
|
}
|
|
|
|
public function testMemoryDrawingFromStream(): void
|
|
{
|
|
$imageFile = __DIR__ . '/../../data/Worksheet/officelogo.jpg';
|
|
|
|
$imageStream = fopen($imageFile, 'rb');
|
|
if ($imageStream === false) {
|
|
self::markTestSkipped('Unable to read Image file for MemoryDrawing');
|
|
}
|
|
$drawing = MemoryDrawing::fromStream($imageStream);
|
|
fclose($imageStream);
|
|
|
|
self::assertIsObject($drawing->getImageResource());
|
|
|
|
self::assertSame(MemoryDrawing::MIMETYPE_JPEG, $drawing->getMimeType());
|
|
self::assertSame(MemoryDrawing::RENDERING_JPEG, $drawing->getRenderingFunction());
|
|
}
|
|
|
|
public function testMemoryDrawingFromStreamNoGetImageSize(): void
|
|
{
|
|
$imageFile = __DIR__ . '/../../data/Worksheet/officelogo.jpg';
|
|
|
|
$imageStream = fopen($imageFile, 'rb');
|
|
if ($imageStream === false) {
|
|
self::markTestSkipped('Unable to read Image file for MemoryDrawing');
|
|
}
|
|
$drawing = MemoryDrawing2::fromStream($imageStream);
|
|
fclose($imageStream);
|
|
|
|
self::assertIsObject($drawing->getImageResource());
|
|
|
|
self::assertSame(MemoryDrawing::MIMETYPE_DEFAULT, $drawing->getMimeType());
|
|
self::assertSame(MemoryDrawing::RENDERING_DEFAULT, $drawing->getRenderingFunction());
|
|
}
|
|
}
|