File tree 2 files changed +40
-2
lines changed
src/PhpSpreadsheet/Worksheet
tests/PhpSpreadsheetTests/Worksheet
2 files changed +40
-2
lines changed Original file line number Diff line number Diff line change @@ -134,6 +134,21 @@ private function cloneResource(): void
134
134
$ this ->imageResource = $ clone ;
135
135
}
136
136
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
+
137
152
/**
138
153
* @param string $imageString String data to be converted to a Memory Drawing
139
154
*
@@ -143,7 +158,7 @@ public static function fromString(string $imageString): self
143
158
{
144
159
$ gdImage = @imagecreatefromstring ($ imageString );
145
160
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 ' );
147
162
}
148
163
149
164
$ mimeType = self ::identifyMimeType ($ imageString );
Original file line number Diff line number Diff line change @@ -65,9 +65,32 @@ public function testMemoryDrawingFromString(): void
65
65
public function testMemoryDrawingFromInvalidString (): void
66
66
{
67
67
$ this ->expectException (Exception::class);
68
- $ this ->expectExceptionMessage ('String cannot be converted to an image ' );
68
+ $ this ->expectExceptionMessage ('Value cannot be converted to an image ' );
69
69
70
70
$ imageString = 'I am not an image ' ;
71
71
MemoryDrawing::fromString ($ imageString );
72
72
}
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
+ }
73
96
}
You can’t perform that action at this time.
0 commit comments