mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-03 05:57:46 +00:00
58c6e51992
* Sync composer.lock/.json and Phpstan Upgrade For some reason, a version newly cloned from master receives a complaint from composer that the lock and json files aren't in sync. So I ran composer update. The only thing that needs special attention is, as usual, Phpstan. There are an unusually large number of new differences when Phpstan is run with Php7.4 vs Php8.1. These are handled easily enough. But Reader/Xlsx seems very fragile; a change which would have eliminated one of the new errors seems to have caused Phpstan to go into a loop, as does an annotation without a code change, and, indeed, as does just about any change to that member. This bears watching, and it's a reason I will delay installing this. * Deleting Phpstan Cache Helps It lets me change Reader/Xlsx as I wished. Not a great resolution, but probably good enough. I will continue to think about it for a couple of days. * Minor DocBlock Changes Correct some PhpDocumentor problems.
94 lines
3.5 KiB
PHP
94 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Worksheet;
|
|
|
|
use GdImage;
|
|
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);
|
|
|
|
if (version_compare(PHP_VERSION, '8.0.0', '>=') === true) {
|
|
self::assertIsObject($drawing->getImageResource());
|
|
self::assertInstanceOf(GdImage::class, $drawing->getImageResource());
|
|
} else {
|
|
self::assertIsResource($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);
|
|
|
|
if (version_compare(PHP_VERSION, '8.0.0', '>=') === true) {
|
|
self::assertIsObject($drawing->getImageResource());
|
|
self::assertInstanceOf(GdImage::class, $drawing->getImageResource());
|
|
} else {
|
|
self::assertIsResource($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);
|
|
|
|
if (version_compare(PHP_VERSION, '8.0.0', '>=') === true) {
|
|
self::assertIsObject($drawing->getImageResource());
|
|
self::assertInstanceOf(GdImage::class, $drawing->getImageResource());
|
|
} else {
|
|
self::assertIsResource($drawing->getImageResource());
|
|
}
|
|
|
|
self::assertSame(MemoryDrawing::MIMETYPE_JPEG, $drawing->getMimeType());
|
|
self::assertSame(MemoryDrawing::RENDERING_JPEG, $drawing->getRenderingFunction());
|
|
}
|
|
}
|