thephpleague / glide

Wonderfully easy on-demand image manipulation library with an HTTP based API.
http://glide.thephpleague.com
MIT License
2.55k stars 199 forks source link

Glide with RESTFUL API #191

Closed BillyFigueroa closed 7 years ago

BillyFigueroa commented 7 years ago

Hi,

I am trying to use a combination of technology including slimframework for creating an api with glide on the server side on a different subdomain (api.sample.com) then my client side (app.sample.com)

I make a http (angular $http service) call to my restful service with the name of the image as a query parameter

return $http.get('http://api.sample.com/photos/' + imgName + '?h=1000&w=1000')
                   .then(function(response) {
                       // What I get back here is not useable???
                    });

on the slimphp side I handle the route...

$server = ServerFactory::create([
    'source'   => 'public/photos',
    'cache'    => 'public/cache',
    'response' => new SlimResponseFactory()
]);

return $server->getImageResponse($request->getAttribute('iid'), $request->getQueryParams());

Is this something Glide is meant to do or not? Was it only written to be used with serverside frameworks? I can't find any documentation to help with these and there are not many tutorials online for Glide

reinink commented 7 years ago

Hey @BillyFigueroa! Some comments:

Was it only written to be used with serverside frameworks?

Glide is a server-side library, that obviously runs on PHP. However, it's intended to be exposed via HTTP, meaning you can use it client side as well.

http://api.sample.com/photos/' + imgName + '?h=1000&w=1000

When you make a request like this, you will get an actual image back, not JSON. It's an actual image request. I'm not sure what "usable" info you're expecting in that Ajax request.

The beauty with Glide is that you 1) setup it up in your server-side framework as an actual endpoint, and then 2) you request whatever images you want, using the manipulation parameters (get vars) to modify how the image is manipulated.

I hope that helps!

reinink commented 7 years ago

Also, conceptually this video might help you: https://vimeo.com/118089742. Note, that video was for version 0.3. Things changed in 1.0.

This video may also be helpful: https://www.youtube.com/watch?v=y_mPTp4lwds

Good luck 👍

BillyFigueroa commented 7 years ago

I got it working with using a public route that I allow using jwtAuthentication passthrough, even though this is not what I wanted so...

My next question is, in your video you sort of show how to sign the images but do not show it in the context of the route, same goes in the documentation. I now need to make sure I sign my images so any help will be great

reinink commented 7 years ago

Hey man, I'd recommend reading through the security docs here: http://glide.thephpleague.com/1.0/config/security/

There are basically two steps in the process:

  1. Generating signatures when outputting an image URL. For example: <img src="/img/cat.jpg?w=500&s=af3dc18fc6bfb2afb521e587c348b904">

  2. Verifying that signature when outputting the image in your controller/route:

<?php

use League\Glide\Signatures\SignatureFactory;
use League\Glide\Signatures\SignatureException;

try {
    // Set complicated sign key
    $signkey = 'v-LK4WCdhcfcc%jt*VC2cj%nVpu+xQKvLUA%H86kRVk_4bgG8&CWM#k*b_7MUJpmTc=4GFmKFp7=K%67je-skxC5vz+r#xT?62tT?Aw%FtQ4Y3gvnwHTwqhxUh89wCa_';

    // Validate HTTP signature
    SignatureFactory::create($signkey)->validateRequest($path, $_GET);

} catch (SignatureException $e) {
    // Handle error
}