Resize uploaded avatars using Intervention Image in SettingsController
MODIFY
app/Http/Controllers/SettingsController.php
app/Http/Controllers/SettingsController.php
@@ -5,6 +5,8 @@ namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
+use Image;
+use Storage;
class SettingsController extends Controller {
public function index() {
@@ -27,9 +29,16 @@ class SettingsController extends Controller {
$user = Auth::user();
if ($request->hasFile('avatar')) {
- $request->file('avatar')->store('public/avatars');
+ $file = $request->file('avatar');
- $user->avatar = $request->file('avatar')->hashName();
+ $fileName = $file->hashName();
+
+ $image = Image::make($file)
+ ->fit(500);
+
+ Storage::put('public/avatars/' . $fileName, (string) $image->encode());
+
+ $user->avatar = $fileName;
}
$user->name = $request->input('name');

MODIFY
composer.json
composer.json
"type": "project",
"require": {
"php": ">=5.6.4",
+ "fideloper/proxy": "^4.0",
+ "intervention/image": "^2.4",
"laravel/framework": "5.6.*",
- "laravel/tinker": "~1.0",
- "fideloper/proxy": "^4.0"
+ "laravel/tinker": "~1.0"
},
"require-dev": {
"fzaninotto/faker": "~1.4",

MODIFY
composer.lock
composer.lock
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
- "content-hash": "f86577586219c86e41f06844279c0137",
+ "content-hash": "cd90e30e038eaf1543241ccaf4d035b1",
"packages": [
{
"name": "dnoegel/php-xdg-base-dir",
],
"time": "2018-02-07T20:20:57+00:00"
},
+ {
+ "name": "guzzlehttp/psr7",
+ "version": "1.4.2",
+ "source": {
+ "type": "git",
+ "url": "https://github.com/guzzle/psr7.git",
+ "reference": "f5b8a8512e2b58b0071a7280e39f14f72e05d87c"
+ },
+ "dist": {
+ "type": "zip",
+ "url": "https://api.github.com/repos/guzzle/psr7/zipball/f5b8a8512e2b58b0071a7280e39f14f72e05d87c",
+ "reference": "f5b8a8512e2b58b0071a7280e39f14f72e05d87c",
+ "shasum": ""
+ },
+ "require": {
+ "php": ">=5.4.0",
+ "psr/http-message": "~1.0"
+ },
+ "provide": {
+ "psr/http-message-implementation": "1.0"
+ },
+ "require-dev": {
+ "phpunit/phpunit": "~4.0"
+ },
+ "type": "library",
+ "extra": {
+ "branch-alias": {
+ "dev-master": "1.4-dev"
+ }
+ },
+ "autoload": {
+ "psr-4": {
+ "GuzzleHttp\\Psr7\\": "src/"
+ },
+ "files": [
+ "src/functions_include.php"
+ ]
+ },
+ "notification-url": "https://packagist.org/downloads/",
+ "license": [
+ "MIT"
+ ],
+ "authors": [
+ {
+ "name": "Michael Dowling",
+ "email": "mtdowling@gmail.com",
+ "homepage": "https://github.com/mtdowling"
+ },
+ {
+ "name": "Tobias Schultze",
+ "homepage": "https://github.com/Tobion"
+ }
+ ],
+ "description": "PSR-7 message implementation that also provides common utility methods",
+ "keywords": [
+ "http",
+ "message",
+ "request",
+ "response",
+ "stream",
+ "uri",
+ "url"
+ ],
+ "time": "2017-03-20T17:10:46+00:00"
+ },
+ {
+ "name": "intervention/image",
+ "version": "2.4.2",
+ "source": {
+ "type": "git",
+ "url": "https://github.com/Intervention/image.git",
+ "reference": "e82d274f786e3d4b866a59b173f42e716f0783eb"
+ },
+ "dist": {
+ "type": "zip",
+ "url": "https://api.github.com/repos/Intervention/image/zipball/e82d274f786e3d4b866a59b173f42e716f0783eb",
+ "reference": "e82d274f786e3d4b866a59b173f42e716f0783eb",
+ "shasum": ""
+ },
+ "require": {
+ "ext-fileinfo": "*",
+ "guzzlehttp/psr7": "~1.1",
+ "php": ">=5.4.0"
+ },
+ "require-dev": {
+ "mockery/mockery": "~0.9.2",
+ "phpunit/phpunit": "^4.8 || ^5.7"
+ },
+ "suggest": {
+ "ext-gd": "to use GD library based image processing.",
+ "ext-imagick": "to use Imagick based image processing.",
+ "intervention/imagecache": "Caching extension for the Intervention Image library"
+ },
+ "type": "library",
+ "extra": {
+ "branch-alias": {
+ "dev-master": "2.4-dev"
+ },
+ "laravel": {
+ "providers": [
+ "Intervention\\Image\\ImageServiceProvider"
+ ],
+ "aliases": {
+ "Image": "Intervention\\Image\\Facades\\Image"
+ }
+ }
+ },
+ "autoload": {
+ "psr-4": {
+ "Intervention\\Image\\": "src/Intervention/Image"
+ }
+ },
+ "notification-url": "https://packagist.org/downloads/",
+ "license": [
+ "MIT"
+ ],
+ "authors": [
+ {
+ "name": "Oliver Vogel",
+ "email": "oliver@olivervogel.com",
+ "homepage": "http://olivervogel.com/"
+ }
+ ],
+ "description": "Image handling and manipulation library with support for Laravel integration",
+ "homepage": "http://image.intervention.io/",
+ "keywords": [
+ "gd",
+ "image",
+ "imagick",
+ "laravel",
+ "thumbnail",
+ "watermark"
+ ],
+ "time": "2018-05-29T14:19:03+00:00"
+ },
{
"name": "jakub-onderka/php-console-color",
"version": "0.1",
],
"time": "2017-02-14T16:28:37+00:00"
},
+ {
+ "name": "psr/http-message",
+ "version": "1.0.1",
+ "source": {
+ "type": "git",
+ "url": "https://github.com/php-fig/http-message.git",
+ "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363"
+ },
+ "dist": {
+ "type": "zip",
+ "url": "https://api.github.com/repos/php-fig/http-message/zipball/f6561bf28d520154e4b0ec72be95418abe6d9363",
+ "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363",
+ "shasum": ""
+ },
+ "require": {
+ "php": ">=5.3.0"
+ },
+ "type": "library",
+ "extra": {
+ "branch-alias": {
+ "dev-master": "1.0.x-dev"
+ }
+ },
+ "autoload": {
+ "psr-4": {
+ "Psr\\Http\\Message\\": "src/"
+ }
+ },
+ "notification-url": "https://packagist.org/downloads/",
+ "license": [
+ "MIT"
+ ],
+ "authors": [
+ {
+ "name": "PHP-FIG",
+ "homepage": "http://www.php-fig.org/"
+ }
+ ],
+ "description": "Common interface for HTTP messages",
+ "homepage": "https://github.com/php-fig/http-message",
+ "keywords": [
+ "http",
+ "http-message",
+ "psr",
+ "psr-7",
+ "request",
+ "response"
+ ],
+ "time": "2016-08-06T14:39:51+00:00"
+ },
{
"name": "psr/log",
"version": "1.0.2",

MODIFY
config/app.php
config/app.php
@@ -150,6 +150,7 @@ return [
* Package Service Providers...
*/
Laravel\Tinker\TinkerServiceProvider::class,
+ Intervention\Image\ImageServiceProvider::class,
/*
* Application Service Providers...
@@ -208,7 +209,7 @@ return [
'URL' => Illuminate\Support\Facades\URL::class,
'Validator' => Illuminate\Support\Facades\Validator::class,
'View' => Illuminate\Support\Facades\View::class,
-
+ 'Image' => Intervention\Image\Facades\Image::class
],
];

ADD
config/image.php
+<?php
+
+return [
+
+ /*
+ |--------------------------------------------------------------------------
+ | Image Driver
+ |--------------------------------------------------------------------------
+ |
+ | Intervention Image supports "GD Library" and "Imagick" to process images
+ | internally. You may choose one of them according to your PHP
+ | configuration. By default PHP's "GD Library" implementation is used.
+ |
+ | Supported: "gd", "imagick"
+ |
+ */
+
+ 'driver' => 'gd'
+
+];

Resize uploaded avatars using Intervention Image in SettingsController