Skip to content

Commit a451a2b

Browse files
Add JPEG support to FileFactory::image()
1 parent 89fc860 commit a451a2b

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

src/Illuminate/Http/Testing/FileFactory.php

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

33
namespace Illuminate\Http\Testing;
44

5+
use Illuminate\Support\Str;
6+
57
class FileFactory
68
{
79
/**
@@ -28,7 +30,9 @@ public function create($name, $kilobytes = 0)
2830
*/
2931
public function image($name, $width = 10, $height = 10)
3032
{
31-
return new File($name, $this->generateImage($width, $height));
33+
$type = Str::endsWith($name, ['.jpg', '.jpeg']) ? 'jpeg' : 'png';
34+
35+
return new File($name, $this->generateImage($width, $height, $type));
3236
}
3337

3438
/**
@@ -38,12 +42,21 @@ public function image($name, $width = 10, $height = 10)
3842
* @param int $height
3943
* @return resource
4044
*/
41-
protected function generateImage($width, $height)
45+
protected function generateImage($width, $height, $type)
4246
{
43-
return tap(tmpfile(), function ($temp) use ($width, $height) {
47+
return tap(tmpfile(), function ($temp) use ($width, $height, $type) {
4448
ob_start();
4549

46-
imagepng(imagecreatetruecolor($width, $height));
50+
$image = imagecreatetruecolor($width, $height);
51+
52+
switch ($type) {
53+
case 'jpeg':
54+
imagejpeg($image);
55+
break;
56+
case 'png':
57+
imagepng($image);
58+
break;
59+
}
4760

4861
fwrite($temp, ob_get_clean());
4962
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Http;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Illuminate\Http\Testing\FileFactory;
7+
8+
class HttpTestingFileFactoryTest extends TestCase
9+
{
10+
public function testImagePng()
11+
{
12+
$image = (new FileFactory)->image('test.png', 15, 20);
13+
14+
$info = getimagesize($image->getRealPath());
15+
16+
$this->assertSame('image/png', $info['mime']);
17+
$this->assertSame(15, $info[0]);
18+
$this->assertSame(20, $info[1]);
19+
}
20+
21+
public function testImageJpeg()
22+
{
23+
$image = (new FileFactory)->image('test.jpeg', 15, 20);
24+
25+
$info = getimagesize($image->getRealPath());
26+
27+
$this->assertSame('image/jpeg', $info['mime']);
28+
$this->assertSame(15, $info[0]);
29+
$this->assertSame(20, $info[1]);
30+
}
31+
}

0 commit comments

Comments
 (0)