From 5b97b34294eff10cfd9204278bbd266012efbe54 Mon Sep 17 00:00:00 2001 From: Simon Staudenmeir Date: Sun, 15 Jul 2018 22:17:06 +0200 Subject: [PATCH] Add JPEG support to FileFactory::image() --- src/Illuminate/Http/Testing/FileFactory.php | 21 +++++++++++--- tests/Http/HttpTestingFileFactoryTest.php | 31 +++++++++++++++++++++ 2 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 tests/Http/HttpTestingFileFactoryTest.php diff --git a/src/Illuminate/Http/Testing/FileFactory.php b/src/Illuminate/Http/Testing/FileFactory.php index 0a677d7da3f2..9a94be332573 100644 --- a/src/Illuminate/Http/Testing/FileFactory.php +++ b/src/Illuminate/Http/Testing/FileFactory.php @@ -2,6 +2,8 @@ namespace Illuminate\Http\Testing; +use Illuminate\Support\Str; + class FileFactory { /** @@ -28,7 +30,9 @@ public function create($name, $kilobytes = 0) */ public function image($name, $width = 10, $height = 10) { - return new File($name, $this->generateImage($width, $height)); + $type = Str::endsWith($name, ['.jpg', '.jpeg']) ? 'jpeg' : 'png'; + + return new File($name, $this->generateImage($width, $height, $type)); } /** @@ -38,12 +42,21 @@ public function image($name, $width = 10, $height = 10) * @param int $height * @return resource */ - protected function generateImage($width, $height) + protected function generateImage($width, $height, $type) { - return tap(tmpfile(), function ($temp) use ($width, $height) { + return tap(tmpfile(), function ($temp) use ($width, $height, $type) { ob_start(); - imagepng(imagecreatetruecolor($width, $height)); + $image = imagecreatetruecolor($width, $height); + + switch ($type) { + case 'jpeg': + imagejpeg($image); + break; + case 'png': + imagepng($image); + break; + } fwrite($temp, ob_get_clean()); }); diff --git a/tests/Http/HttpTestingFileFactoryTest.php b/tests/Http/HttpTestingFileFactoryTest.php new file mode 100644 index 000000000000..551ab8a81c6d --- /dev/null +++ b/tests/Http/HttpTestingFileFactoryTest.php @@ -0,0 +1,31 @@ +image('test.png', 15, 20); + + $info = getimagesize($image->getRealPath()); + + $this->assertSame('image/png', $info['mime']); + $this->assertSame(15, $info[0]); + $this->assertSame(20, $info[1]); + } + + public function testImageJpeg() + { + $image = (new FileFactory)->image('test.jpeg', 15, 20); + + $info = getimagesize($image->getRealPath()); + + $this->assertSame('image/jpeg', $info['mime']); + $this->assertSame(15, $info[0]); + $this->assertSame(20, $info[1]); + } +}