Skip to content

Commit b06d396

Browse files
committed
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)
1 parent 8e0568b commit b06d396

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

src/PhpSpreadsheet/Worksheet/MemoryDrawing.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,21 @@ private function cloneResource(): void
134134
$this->imageResource = $clone;
135135
}
136136

137+
/**
138+
* @param resource $imageStream Stream data to be converted to a Memory Drawing
139+
*
140+
* @throws Exception
141+
*/
142+
public static function fromStream($imageStream): self
143+
{
144+
$streamValue = stream_get_contents($imageStream);
145+
if ($streamValue === false) {
146+
throw new Exception('Unable to read data from stream');
147+
}
148+
149+
return self::fromString($streamValue);
150+
}
151+
137152
/**
138153
* @param string $imageString String data to be converted to a Memory Drawing
139154
*
@@ -143,7 +158,7 @@ public static function fromString(string $imageString): self
143158
{
144159
$gdImage = @imagecreatefromstring($imageString);
145160
if ($gdImage === false) {
146-
throw new Exception('String cannot be converted to an image');
161+
throw new Exception('Value cannot be converted to an image');
147162
}
148163

149164
$mimeType = self::identifyMimeType($imageString);

tests/PhpSpreadsheetTests/Worksheet/MemoryDrawingTest.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,32 @@ public function testMemoryDrawingFromString(): void
6565
public function testMemoryDrawingFromInvalidString(): void
6666
{
6767
$this->expectException(Exception::class);
68-
$this->expectExceptionMessage('String cannot be converted to an image');
68+
$this->expectExceptionMessage('Value cannot be converted to an image');
6969

7070
$imageString = 'I am not an image';
7171
MemoryDrawing::fromString($imageString);
7272
}
73+
74+
public function testMemoryDrawingFromStream(): void
75+
{
76+
$imageFile = __DIR__ . '/../../data/Worksheet/officelogo.jpg';
77+
78+
$imageStream = fopen($imageFile, 'rb');
79+
if ($imageStream === false) {
80+
self::markTestSkipped('Unable to read Image file for MemoryDrawing');
81+
}
82+
$drawing = MemoryDrawing::fromStream($imageStream);
83+
fclose($imageStream);
84+
85+
if (version_compare(PHP_VERSION, '8.0.0', '>=') === true) {
86+
self::assertIsObject($drawing->getImageResource());
87+
/** @phpstan-ignore-next-line */
88+
self::assertInstanceOf(GdImage::class, $drawing->getImageResource());
89+
} else {
90+
self::assertIsResource($drawing->getImageResource());
91+
}
92+
93+
self::assertSame(MemoryDrawing::MIMETYPE_JPEG, $drawing->getMimeType());
94+
self::assertSame(MemoryDrawing::RENDERING_JPEG, $drawing->getRenderingFunction());
95+
}
7396
}

0 commit comments

Comments
 (0)