Skip to content

Commit

Permalink
Test resizing uploaded image
Browse files Browse the repository at this point in the history
  • Loading branch information
unclexo committed Dec 26, 2022
1 parent a6e2137 commit fe489a5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
17 changes: 17 additions & 0 deletions app/Http/Controllers/MediaUploaderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use App\Http\Requests\UploadMultipleFilesRequest;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Intervention\Image\Facades\Image;

class MediaUploaderController extends Controller
{
Expand Down Expand Up @@ -54,4 +56,19 @@ public function uploadMultipleFiles(UploadMultipleFilesRequest $request)

return $paths;
}

public function resize()
{
\request()->validate([
'image' => ['required', 'image']
]);

$newImageName = 'new-image-name.jpg';

$path = \request()->file('image')->storeAs('resize', $newImageName, 'public');

Image::make(Storage::disk('public')->path($path))->resize(300, 200)->save();

return ['path' => $path];
}
}
2 changes: 2 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,7 @@
Route::post('/upload/validation', 'validateUpload')->name('upload.validation');

Route::post('/upload/multiple-files', 'uploadMultipleFiles')->name('upload.multiple');

Route::post('/upload/resize', 'resize')->name('upload.resize');
});
});
19 changes: 19 additions & 0 deletions tests/Feature/UploadModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;
use Tests\TestCase;

class UploadModuleTest extends TestCase
Expand Down Expand Up @@ -84,4 +85,22 @@ public function it_can_upload_multiple_files()

Storage::disk('public')->assertExists($response->json());
}

/** @test */
public function it_can_resize_uploaded_image()
{
$this->actingAs(User::factory()->create());

Storage::fake('public');

$response = $this->post(route('upload.resize'), [
'image' => $file = UploadedFile::fake()->image('image-name.jpg', 500, 500),
]);

Storage::disk('public')->assertExists($response->json('path'));

$image = Image::make(Storage::disk('public')->path($response->json('path')));
$this->assertEquals(300, $image->width());
$this->assertEquals(200, $image->height());
}
}

0 comments on commit fe489a5

Please sign in to comment.