Last active
February 26, 2016 16:26
-
-
Save willemwollebrants/19dc48834e5c202bbd26 to your computer and use it in GitHub Desktop.
Glide + GlideFormats + HTTP
This file contains hidden or 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
<IfModule mod_rewrite.c> | |
RewriteEngine On | |
RewriteBase / | |
RewriteRule ^index\.php$ - [L] | |
RewriteCond %{REQUEST_FILENAME} !-f | |
RewriteCond %{REQUEST_FILENAME} !-d | |
RewriteRule . /index.php [L] | |
</IfModule> |
This file contains hidden or 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
{ | |
"require": { | |
"studiow/glideformat": "^1.0", | |
"league/route": "^1.2", | |
"league/flysystem-memory": "^1.0" | |
} | |
} |
This file contains hidden or 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 | |
/** | |
* Use league/glide and studiow/glideformat for on-demand image scaling | |
* The first request to a specific preset/image combination will generate the file in the right location | |
* so for the second request, no php gets executed. | |
* | |
* @author Willem Wollebrants <[email protected]> | |
*/ | |
use Studiow\GlideFormat\GlideFormatServer; | |
use Studiow\GlideFormat\Exception\PresetNotFoundException; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\HttpFoundation\Response; | |
use League\Route\RouteCollection; | |
use League\Flysystem\Filesystem; | |
use League\Flysystem\Adapter\Local; | |
use League\Flysystem\Memory\MemoryAdapter; | |
use League\Glide\Filesystem\FileNotFoundException; | |
$loader = include __DIR__ . '/vendor/autoload.php'; | |
$route = new RouteCollection(); | |
$server = GlideFormatServer::createServer([ | |
//The upload dir | |
'source' => new Filesystem(new Local(__DIR__ . '/uploads')), | |
//The cache only exists in memory | |
'cache' => new Filesystem(new MemoryAdapter()), | |
]); | |
//add some presets (= image formats) | |
$server->addPreset('thumbnail', ["w" => 100, "h" => 150, "fit" => "crop"]); | |
$server->addPreset('medium', ["w" => 300, "h" => 300, "fit" => "crop"]); | |
$route->get('/img/{preset}/{filename}', function(Request $request, Response $response, array $params = []) use($server) { | |
//read the variables | |
$preset = $params['preset']; | |
$filename = $params['filename']; | |
try { | |
//let Glide create the image according to preset | |
$path = $server->makeImage($filename, $preset); | |
$images = new Filesystem(new Local(__DIR__ . '/img')); | |
$images->put("/{$preset}/{$filename}", $server->getGlideServer()->getCache()->read($path)); | |
$server->outputImage($filename, $format); | |
} catch (FileNotFoundException $exc) { | |
//handle error: non-existing file | |
return $response->setContent('wrong file'); | |
} catch (PresetNotFoundException $exc) { | |
//handle error: non-existing preset | |
return $response->setContent('wrong preset'); | |
} | |
}); | |
//The usual router stuff | |
$request = Request::createFromGlobals(); | |
$route->getDispatcher()->dispatch($request->getMethod(), $request->getPathInfo())->send(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment