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

Rethink image manipulations with Glide

Rethink image manipulations with Glide

Jonathan Reinink

May 21, 2015
Tweet

More Decks by Jonathan Reinink

Other Decks in Technology

Transcript

  1. It’s a good time for HTTP + PHP. Middleware and

    PSR-7 are changing how we build applications in PHP.
  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. 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. get('/img/{path}', function ($path) { ! $server = ServerFactory::create([ 'source' =>

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

    => 'path/to/source', 'cache' => 'path/to/cache', ]); ! $server->outputImage($request); });
  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. Prevent URL hacking by signing the image URLs. An important

    security step that should always be used in production.
  10. $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.'">';