Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rethink image manipulations with Glide (ForumPH...

Rethink image manipulations with Glide (ForumPHP 2015)

Jonathan Reinink

November 24, 2015
Tweet

More Decks by Jonathan Reinink

Other Decks in Technology

Transcript

  1. Jonathan Reinink Software developer from Canada. Been writing PHP for

    over 15 years. Marketing agency for over a decade. Started contract development this year. I <3 open source.
  2. Only save your original images, not the manipulations. Then request

    different image sizes when they are needed.
  3. Use Glide in an app, or create a separate image

    server. Offload image manipulations to a separate server on larger projects
  4. Route::get('/img/users/{id}', function ($id) { ! $server = ServerFactory::create([ 'source' =>

    'path/to/source', 'cache' => 'path/to/cache', ]); ! $server->outputImage('users/'.$id.'.jpg', [ 'w' => 300, 'h' => 400 ]); });
  5. Route::get('/img/{path}', function ($path) { ! $server = ServerFactory::create([ 'source' =>

    'path/to/source', 'cache' => 'path/to/cache', ]); ! $server->outputImage($path, $_GET); });
  6. Route::get('/img/{path}', function ($path) { ! $server = ServerFactory::create([ 'source' =>

    'path/to/source', 'cache' => 'path/to/cache', ]); ! return $server->getImageResponse($path, $_GET); });
  7. $source = new Local('path/to/source'); $cache = new Local('path/to/cache'); ! $server

    = ServerFactory::create([ 'source' => new Filesystem($source), 'cache' => new Filesystem($cache), ]);
  8. $client = S3Client::factory([ 'key' => 'your-key', 'secret' => 'your-secret', ]);

    ! $s3 = new AwsS3Adapter($client, 'your-bucket'); $cache = new Local('path/to/cache'); ! $server = ServerFactory::create([ 'source' => new Filesystem($s3), 'cache' => new Filesystem($cache), ]);
  9. $server = ServerFactory::create([ 'presets' => [ 'small' = [ 'w'

    => 200, 'h' => 200, 'fit' => 'crop', ], ] ]);
  10. Prevent URL hacking by signing the image URLs. An important

    security step that should always be used in production.
  11. // Create a URL builder $builder = UrlBuilderFactory::create( 'http://example.com', 'your-sign-key'

    ); ! // Generate a URL $url = $builder->getUrl('cat.jpg', ['w' => 500]); ! // Use the URL in your app echo '<img src="'.$url.'">';
  12. use League\Glide\Signatures\SignatureFactory; use League\Glide\Signatures\SignatureException; ! try { // Validate HTTP

    signature SignatureFactory::create('your-sign-key') ->validateRequest($path, $_GET); } catch (SignatureException $e) { // Handle error }
  13. $source = new Local('path/to/source'); $cache = new MemoryAdapter(); ! $server

    = ServerFactory::create([ 'source' => new Filesystem($source), 'cache' => new Filesystem($cache), ]);
  14. // Handle your image upload $image = ... ! //

    Dispatch the job to your queue $this->dispatch( new ProcessImageManipulations($image) ) ! // Run the job foreach (['small', 'medium', 'large'] as $size) { $glide->makeImage($image['path'], ['p' => 'small']); }