Torsten Baldes Posted January 22, 2019 Share Posted January 22, 2019 Hi, I'm building a small REST-API for my ProcessWire website to use the content outside in other apps. There are a lot of images and the apps will run on many different systems (mobile phones, tablets with iOS and android). To have the ability to always request the optimal image for each device width and screen density, I don't want to generate a fixed set of image sizes. Instead, it would be nice to request the resized image on the fly by sending the URL of the image with some parameters (width, height, crop) to the server and get my resized and cached image back. Similar to scripts like Timthumb or phpThumb, but with all the nice additions (e.g. focus point) from ProcessWire. The URL could look like this: https://mydomain.tld/resize/?url=/site/assets/files/1125/profile-image.jpg&width=1200&height=1200&crop=1 The response of this would be the image itself, resized to a square image, 1200x1200px How would I approach this? Is there something like this already available? Thanks! Link to comment Share on other sites More sharing options...
Sergio Posted January 22, 2019 Share Posted January 22, 2019 https://cloudinary.com/ does this for you and it has an API already. ? You can save the returned image on PW server after it's processed as well. 1 Link to comment Share on other sites More sharing options...
Torsten Baldes Posted January 23, 2019 Author Share Posted January 23, 2019 Thanks, but I don't want to / can't use an external service and PW can already do all the resizing and cropping stuff. I just want access to these features outside from the template context. Link to comment Share on other sites More sharing options...
BitPoet Posted January 23, 2019 Share Posted January 23, 2019 It's not hard to implement yourself. Create a template with a PHP file where you: Parse and sanitize all relevant information from the call (page id, filename, width, height, other options/actions) Retrieve the page with the given id Instantiate a PagefilesManager object with the page as the parameter Retrieve the image by calling getFile on the PagefilesManager Call size() on the image with necessary parameters/options Call $session->redirect() with the url of the object return by the site() call Here's a quick&dirty implementation: Spoiler <?php namespace ProcessWire; /** site/templates/resize.php */ $allowOptions = [ "cropping" => "alphanumeric", "quality" => "int", "upscaling" => "bool", "suffix" => "alpha", "sharpening" => "alpha", "autorotation" => "bool", "rotate" => "intSigned", "flip" => "alpha", "hidpi" => "bool", "hidpiQuality" => ["intUnsigned", ["min" => 0, "max" => 100]], "focus" => "bool" ]; /* Validate input */ if(! preg_match('~/site/assets/files/(\d+)/([^/]+\.(gif|jpg|jpeg|png))$~i', $input->get->url, $match)) { throw new Wire404Exception(); } $id = $match[1]; $filename = $sanitizer->filename($match[2]); $width = $sanitizer->int($input->get->width); $height = $sanitizer->int($input->get->height); $options = []; // If passed, sanitize option values and add them to the options array foreach($allowOptions as $param => $san) { if($input->get->$param !== null) { $options[$param] = is_array($san) ? $sanitizer->{$san[0]}($input->get->$param, $san[1]) : $sanitizer->$san($input->get->$param); } } // Get the requested page $imgpage = $pages->findOne($id); if($imgpage instanceof NullPage) { throw new Wire404Exception(); } // Create a PagefilesManager for the requested page $PFM = new PagefilesManager($imgpage); // Find the file specified $img = $PFM->getFile($filename); if(! $img) { throw new Wire404Exception(); } // Resize with the settings given in the request $sized = $img->size( $width, $height, $options ); // Redirect to the resized image $session->redirect($sized->url); 6 2 Link to comment Share on other sites More sharing options...
Torsten Baldes Posted January 24, 2019 Author Share Posted January 24, 2019 Whooohaaa! Thanks! That was more then I expected! I will give it a try. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now