Simplify Glide image handling

This commit is contained in:
Jonathan Reinink 2021-05-10 16:18:33 -04:00
parent 273239f8ba
commit 463f622d0a
7 changed files with 127 additions and 45 deletions

View File

@ -2,12 +2,22 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use League\Glide\Server; use Illuminate\Http\Request;
use League\Glide\ServerFactory;
use Illuminate\Contracts\Filesystem\Filesystem;
use League\Glide\Responses\LaravelResponseFactory;
class ImagesController extends Controller class ImagesController extends Controller
{ {
public function show(Server $glide) public function show(Filesystem $filesystem, Request $request, $path)
{ {
return $glide->fromRequest()->response(); $server = ServerFactory::create([
'response' => new LaravelResponseFactory($request),
'source' => $filesystem->getDriver(),
'cache' => $filesystem->getDriver(),
'cache_path_prefix' => '.glide-cache',
]);
return $server->getImageResponse($path, $request->all());
} }
} }

View File

@ -7,6 +7,7 @@ use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect; use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Request; use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\URL;
use Illuminate\Validation\Rule; use Illuminate\Validation\Rule;
use Inertia\Inertia; use Inertia\Inertia;
@ -25,7 +26,7 @@ class UsersController extends Controller
'name' => $user->name, 'name' => $user->name,
'email' => $user->email, 'email' => $user->email,
'owner' => $user->owner, 'owner' => $user->owner,
'photo' => $user->photoUrl(['w' => 40, 'h' => 40, 'fit' => 'crop']), 'photo' => $user->photo_path ? URL::route('image', ['path' => $user->photo_path, 'w' => 40, 'h' => 40, 'fit' => 'crop']) : null,
'deleted_at' => $user->deleted_at, 'deleted_at' => $user->deleted_at,
]), ]),
]); ]);
@ -68,7 +69,7 @@ class UsersController extends Controller
'last_name' => $user->last_name, 'last_name' => $user->last_name,
'email' => $user->email, 'email' => $user->email,
'owner' => $user->owner, 'owner' => $user->owner,
'photo' => $user->photoUrl(['w' => 60, 'h' => 60, 'fit' => 'crop']), 'photo' => $user->photo_path ? URL::route('image', ['path' => $user->photo_path, 'w' => 60, 'h' => 60, 'fit' => 'crop']) : null,
'deleted_at' => $user->deleted_at, 'deleted_at' => $user->deleted_at,
], ],
]); ]);

View File

@ -6,10 +6,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable; use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\URL;
use League\Glide\Server;
class User extends Authenticatable class User extends Authenticatable
{ {
@ -37,13 +34,6 @@ class User extends Authenticatable
$this->attributes['password'] = Hash::needsRehash($password) ? Hash::make($password) : $password; $this->attributes['password'] = Hash::needsRehash($password) ? Hash::make($password) : $password;
} }
public function photoUrl(array $attributes)
{
if ($this->photo_path) {
return URL::to(App::make(Server::class)->fromPath($this->photo_path, $attributes));
}
}
public function isDemoUser() public function isDemoUser()
{ {
return $this->email === 'johndoe@example.com'; return $this->email === 'johndoe@example.com';

View File

@ -3,9 +3,7 @@
namespace App\Providers; namespace App\Providers;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
use League\Glide\Server;
class AppServiceProvider extends ServiceProvider class AppServiceProvider extends ServiceProvider
{ {
@ -17,15 +15,6 @@ class AppServiceProvider extends ServiceProvider
public function register() public function register()
{ {
Model::unguard(); Model::unguard();
$this->app->bind(Server::class, function ($app) {
return Server::create([
'source' => Storage::getDriver(),
'cache' => Storage::getDriver(),
'cache_folder' => '.glide-cache',
'base_url' => 'img',
]);
});
} }
/** /**

View File

@ -16,7 +16,7 @@
"laravel/framework": "^8.40", "laravel/framework": "^8.40",
"laravel/tinker": "^2.5", "laravel/tinker": "^2.5",
"laravel/ui": "^2.0", "laravel/ui": "^2.0",
"league/glide": "2.0.x-dev", "league/glide-laravel": "^1.0",
"tightenco/ziggy": "^0.8.0" "tightenco/ziggy": "^0.8.0"
}, },
"require-dev": { "require-dev": {

124
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "a0544aeae8ce3b61fcf2909c0fae1722", "content-hash": "3b6210b9a1903f968a3c773d9a60aad9",
"packages": [ "packages": [
{ {
"name": "asm89/stack-cors", "name": "asm89/stack-cors",
@ -1556,29 +1556,28 @@
}, },
{ {
"name": "league/glide", "name": "league/glide",
"version": "2.0.x-dev", "version": "1.7.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/thephpleague/glide.git", "url": "https://github.com/thephpleague/glide.git",
"reference": "e84ef237030817ab6034b2c17173dd3352a971e1" "reference": "ae5e26700573cb678919d28e425a8b87bc71c546"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/thephpleague/glide/zipball/e84ef237030817ab6034b2c17173dd3352a971e1", "url": "https://api.github.com/repos/thephpleague/glide/zipball/ae5e26700573cb678919d28e425a8b87bc71c546",
"reference": "e84ef237030817ab6034b2c17173dd3352a971e1", "reference": "ae5e26700573cb678919d28e425a8b87bc71c546",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
"guzzlehttp/psr7": "^1.1", "intervention/image": "^2.4",
"intervention/image": "^2.1",
"league/flysystem": "^1.0", "league/flysystem": "^1.0",
"php": "^5.4 | ^7.0", "php": "^7.2|^8.0",
"symfony/http-foundation": "^2.3|^3.0|^4.0|^5.0" "psr/http-message": "^1.0"
}, },
"require-dev": { "require-dev": {
"mockery/mockery": "~0.9", "mockery/mockery": "^1.3.3",
"phpunit/php-token-stream": "^1.4", "phpunit/php-token-stream": "^3.1|^4.0",
"phpunit/phpunit": "~4.4" "phpunit/phpunit": "^8.5|^9.0"
}, },
"type": "library", "type": "library",
"extra": { "extra": {
@ -1616,9 +1615,102 @@
], ],
"support": { "support": {
"issues": "https://github.com/thephpleague/glide/issues", "issues": "https://github.com/thephpleague/glide/issues",
"source": "https://github.com/thephpleague/glide/tree/2.0" "source": "https://github.com/thephpleague/glide/tree/1.7.0"
}, },
"time": "2020-03-06T21:33:29+00:00" "time": "2020-11-05T17:34:03+00:00"
},
{
"name": "league/glide-laravel",
"version": "1.0.0",
"source": {
"type": "git",
"url": "https://github.com/thephpleague/glide-laravel.git",
"reference": "b525e33e32940f3b047d6ca357131aba0e973e72"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/thephpleague/glide-laravel/zipball/b525e33e32940f3b047d6ca357131aba0e973e72",
"reference": "b525e33e32940f3b047d6ca357131aba0e973e72",
"shasum": ""
},
"require": {
"league/glide-symfony": "^1.0"
},
"require-dev": {
"mockery/mockery": "^0.9",
"phpunit/phpunit": "^4.0"
},
"type": "library",
"autoload": {
"psr-4": {
"League\\Glide\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Jonathan Reinink",
"email": "jonathan@reinink.ca",
"homepage": "http://reinink.ca"
}
],
"description": "Glide adapter for Laravel",
"homepage": "http://glide.thephpleague.com",
"support": {
"issues": "https://github.com/thephpleague/glide-laravel/issues",
"source": "https://github.com/thephpleague/glide-laravel/tree/master"
},
"time": "2015-12-26T15:40:35+00:00"
},
{
"name": "league/glide-symfony",
"version": "1.0.4",
"source": {
"type": "git",
"url": "https://github.com/thephpleague/glide-symfony.git",
"reference": "8162ec0e0b070e53e88a840a67208ec4baec9291"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/thephpleague/glide-symfony/zipball/8162ec0e0b070e53e88a840a67208ec4baec9291",
"reference": "8162ec0e0b070e53e88a840a67208ec4baec9291",
"shasum": ""
},
"require": {
"league/glide": "^1.0",
"symfony/http-foundation": "^2.3|^3.0|^4.0|^5.0"
},
"require-dev": {
"mockery/mockery": "^0.9",
"phpunit/phpunit": "^4.0"
},
"type": "library",
"autoload": {
"psr-4": {
"League\\Glide\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Jonathan Reinink",
"email": "jonathan@reinink.ca",
"homepage": "http://reinink.ca"
}
],
"description": "Glide adapter for Symfony",
"homepage": "http://glide.thephpleague.com",
"support": {
"issues": "https://github.com/thephpleague/glide-symfony/issues",
"source": "https://github.com/thephpleague/glide-symfony/tree/master"
},
"time": "2020-03-05T12:38:10+00:00"
}, },
{ {
"name": "league/mime-type-detection", "name": "league/mime-type-detection",
@ -7652,9 +7744,7 @@
], ],
"aliases": [], "aliases": [],
"minimum-stability": "dev", "minimum-stability": "dev",
"stability-flags": { "stability-flags": [],
"league/glide": 20
},
"prefer-stable": true, "prefer-stable": true,
"prefer-lowest": false, "prefer-lowest": false,
"platform": { "platform": {

View File

@ -137,4 +137,6 @@ Route::get('reports', [ReportsController::class, 'index'])
// Images // Images
Route::get('/img/{path}', [ImagesController::class, 'show'])->where('path', '.*'); Route::get('/img/{path}', [ImagesController::class, 'show'])
->where('path', '.*')
->name('image');