Skip to content

Commit 89a5468

Browse files
[10.x] Throw LogicException when calling FileFactory@image() if mimetype is not supported (#46859)
* throw exception if FileFactory does not support mimetype * style * formatting * rely on $functionName --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 9cfd7e1 commit 89a5468

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/Illuminate/Http/Testing/FileFactory.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Illuminate\Http\Testing;
44

5+
use LogicException;
6+
57
class FileFactory
68
{
79
/**
@@ -49,6 +51,8 @@ public function createWithContent($name, $content)
4951
* @param int $width
5052
* @param int $height
5153
* @return \Illuminate\Http\Testing\File
54+
*
55+
* @throws \LogicException
5256
*/
5357
public function image($name, $width = 10, $height = 10)
5458
{
@@ -64,9 +68,15 @@ public function image($name, $width = 10, $height = 10)
6468
* @param int $height
6569
* @param string $extension
6670
* @return resource
71+
*
72+
* @throws \LogicException
6773
*/
6874
protected function generateImage($width, $height, $extension)
6975
{
76+
if (! function_exists('imagecreatetruecolor')) {
77+
throw new LogicException('GD extension is not installed.');
78+
}
79+
7080
return tap(tmpfile(), function ($temp) use ($width, $height, $extension) {
7181
ob_start();
7282

@@ -76,7 +86,13 @@ protected function generateImage($width, $height, $extension)
7686

7787
$image = imagecreatetruecolor($width, $height);
7888

79-
call_user_func("image{$extension}", $image);
89+
if (! function_exists($functionName = "image{$extension}")) {
90+
ob_get_clean();
91+
92+
throw new LogicException("{$functionName} function is not defined and image cannot be generated.");
93+
}
94+
95+
call_user_func($functionName, $image);
8096

8197
fwrite($temp, ob_get_clean());
8298
});

tests/Http/HttpTestingFileFactoryTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,29 @@ public function testCreateWithoutMimeType()
116116
);
117117
}
118118

119+
/** @dataProvider generateImageDataProvider */
120+
public function testCallingCreateWithoutGDLoadedThrowsAnException(string $fileExtension, string $driver)
121+
{
122+
if ($this->isGDSupported($driver)) {
123+
$this->markTestSkipped("Requires no {$driver}");
124+
}
125+
126+
$this->expectException(\LogicException::class);
127+
(new FileFactory)->image("test.{$fileExtension}");
128+
}
129+
130+
public static function generateImageDataProvider(): array
131+
{
132+
return [
133+
'jpeg' => ['jpeg', 'JPEG Support'],
134+
'png' => ['png', 'PNG Support'],
135+
'gif' => ['gif', 'GIF Create Support'],
136+
'webp' => ['webp', 'WebP Support'],
137+
'wbmp' => ['wbmp', 'WBMP Support'],
138+
'bmp' => ['bmp', 'BMP Support'],
139+
];
140+
}
141+
119142
/**
120143
* @param string $driver
121144
* @return bool

0 commit comments

Comments
 (0)