Jump to content
Torsten Baldes

Use Processwire (ImageSizer) as image resizing service

Recommended Posts


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?



Share this post

Link to post
Share on other sites

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.

Share this post

Link to post
Share on other sites

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:


<?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(

// Redirect to the resized image



  • Like 5
  • Thanks 2

Share this post

Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...