Skip to content

Commit

Permalink
Add JPEG support to FileFactory::image()
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonStaudenmeir committed Jul 15, 2018
1 parent 89fc860 commit a451a2b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/Illuminate/Http/Testing/FileFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Illuminate\Http\Testing;

use Illuminate\Support\Str;

class FileFactory
{
/**
Expand All @@ -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));
}

/**
Expand All @@ -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());
});
Expand Down
31 changes: 31 additions & 0 deletions tests/Http/HttpTestingFileFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Illuminate\Tests\Http;

use PHPUnit\Framework\TestCase;
use Illuminate\Http\Testing\FileFactory;

class HttpTestingFileFactoryTest extends TestCase
{
public function testImagePng()
{
$image = (new FileFactory)->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]);
}
}

0 comments on commit a451a2b

Please sign in to comment.