Last active
August 19, 2019 13:06
-
-
Save cron13/1d8892153cbd60200917c452e24f9ce9 to your computer and use it in GitHub Desktop.
spatie/image-optimizer + spatie/laravel-glide
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Awesome extends League\Flysystem\Adapter\Local { | |
public function write($path, $contents, League\Flysystem\Config $config) { | |
$location = $this->applyPathPrefix($path); | |
$this->ensureDirectory(dirname($location)); | |
if (($size = file_put_contents($location, $contents, $this->writeFlags)) === false) { | |
return false; | |
} | |
ImageOptimizer::optimize($location, $location); | |
if(($size = filesize($location)) === false) { | |
return false; | |
} | |
$type = 'file'; | |
$result = compact('contents', 'type', 'size', 'path'); | |
if ($visibility = $config->get('visibility')) { | |
$result['visibility'] = $visibility; | |
$this->setVisibility($path, $visibility); | |
} | |
return $result; | |
} | |
public function update($path, $contents, League\Flysystem\Config $config) { | |
$location = $this->applyPathPrefix($path); | |
$size = file_put_contents($location, $contents, $this->writeFlags); | |
if ($size === false) { | |
return false; | |
} | |
ImageOptimizer::optimize($location, $location); | |
if(($size = filesize($location)) === false) { | |
return false; | |
} | |
$type = 'file'; | |
$result = compact('type', 'path', 'size', 'contents'); | |
if ($mimetype = Util::guessMimeType($path, $contents)) { | |
$result['mimetype'] = $mimetype; | |
} | |
return $result; | |
} | |
} | |
Route::get('glide/{path}', function($path) { | |
$server = ServerFactory::create([ | |
'response' => new LaravelResponseFactory(app('request')), | |
'source' => app('filesystem')->disk('public')->getDriver(), | |
'cache' => new League\Flysystem\Filesystem( | |
new Awesome( storage_path('glide') ) | |
) | |
]); | |
return $server->getImageResponse($path, Input::query()); | |
})->where('path', '.+'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment