mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-31 04:28:51 +00:00
Provide functionality allowing creation of an in-memory image from stream data, (note that it does create a temporary local file as part of the process)
This commit is contained in:
@@ -134,6 +134,21 @@ class MemoryDrawing extends BaseDrawing
|
||||
$this->imageResource = $clone;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param resource $imageStream Stream data to be converted to a Memory Drawing
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function fromStream($imageStream): self
|
||||
{
|
||||
$streamValue = stream_get_contents($imageStream);
|
||||
if ($streamValue === false) {
|
||||
throw new Exception('Unable to read data from stream');
|
||||
}
|
||||
|
||||
return self::fromString($streamValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $imageString String data to be converted to a Memory Drawing
|
||||
*
|
||||
@@ -143,7 +158,7 @@ class MemoryDrawing extends BaseDrawing
|
||||
{
|
||||
$gdImage = @imagecreatefromstring($imageString);
|
||||
if ($gdImage === false) {
|
||||
throw new Exception('String cannot be converted to an image');
|
||||
throw new Exception('Value cannot be converted to an image');
|
||||
}
|
||||
|
||||
$mimeType = self::identifyMimeType($imageString);
|
||||
|
||||
@@ -65,9 +65,32 @@ class MemoryDrawingTest extends TestCase
|
||||
public function testMemoryDrawingFromInvalidString(): void
|
||||
{
|
||||
$this->expectException(Exception::class);
|
||||
$this->expectExceptionMessage('String cannot be converted to an image');
|
||||
$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());
|
||||
/** @phpstan-ignore-next-line */
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user