Skip to content

Instantly share code, notes, and snippets.

@cron13
Last active August 19, 2019 13:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cron13/1d8892153cbd60200917c452e24f9ce9 to your computer and use it in GitHub Desktop.
Save cron13/1d8892153cbd60200917c452e24f9ce9 to your computer and use it in GitHub Desktop.
spatie/image-optimizer + spatie/laravel-glide
<?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