WordPress Media Library pre-processing with phpThumb

Certainly great things can be done to images with CSS3 and polyfills, and WP’s image editor is pretty keen… but sometimes you want some sort of image processing done in-between when you upload an image and its saved to the library. Maybe rounded corners, color correction, watermarks, whatever.

If you try to do this processing dynamically via phpThumb’s url API, it can be quite a challenge to get caching working correctly, and, generally you will be seeing performance hits, both in memory and page page. Luckily, we can hook into “image_make_intermediate_size” and process the image as described above. Try something like this in your functions.php

//obviously we need the library
require_once('/home/xxxx/public_html/wp-content/plugins/get-post-image/phpthumb/phpthumb.class.php');
add_filter('image_make_intermediate_size', 'james_rounded_filter');
 
function james_rounded_filter($file) {

        // create phpThumb object
        $phpThumb = new phpThumb();
        $phpThumb->resetObject();

        // set data source -- do this first, any settings must be made AFTER this call      
        $phpThumb->setSourceData(file_get_contents($file));
        $output_filename = $file;

        // PLEASE NOTE:
        // You must set any relevant config settings here. The phpThumb
        // object mode does NOT pull any settings from phpThumb.config.php
        $phpThumb->setParameter('config_document_root', '/home/xxxx/public_html/wp-content/plugins/get-post-image/phpthumb/');
        $phpThumb->setParameter('config_cache_directory', '/home/xxxx/public_html/wp-content/cache/phpthumb/');
		$phpThumb->setParameter('cache_directory_depth', 4);

        // set parameters (see "URL Parameters" in phpthumb.readme.txt)
        $phpThumb->setParameter('fltr', 'ric|12|12');
        $phpThumb->setParameter('q', '95');

        // generate & output thumbnail
        if ($phpThumb->GenerateThumbnail()) { // this line is VERY important, do not remove it!
            if ($phpThumb->RenderToFile($output_filename)) {
                // do something on success, besides processing
				error_log('Successfully rendered to "'.$output_filename.'"'."\n\n", 3, '/home/xxxx/public_html/wp-content/cache/thumblog.txt');
            } else {
                // do something with debug/error messages
                error_log('Failed:'.implode("\n\n", $phpThumb->debugmessages), 3, '/home/xxxx/public_html/wp-content/cache/thumblog.txt');
               die;
            }
        } else {
            // do something with debug/error messages
            error_log('Failed:'.$phpThumb->fatalerror."\n\n".implode("\n\n", $phpThumb->debugmessages), 3, '/home/xxxx/public_html/wp-content/cache/thumblog.txt');
            die;
        }
	
		if ( !is_wp_error($output_filename) && $output_filename) {
			return $output_filename;
		}
		
		return false;
}

If you only want to your processing to happen against a subset of images, then you should define a custom image size for this subset. You can then filter the pre-processing using a function like so:

function my_get_image_size( $name ) {
	global $_wp_additional_image_sizes;

	if ( isset( $_wp_additional_image_sizes[$name] ) )
		return $_wp_additional_image_sizes[$name];

	return false;
}

$name being whatever you named your custom size. Getter would be, something like this (what you are getting is an array of the values you initially set):

$image_size = my_get_image_size( 'my-custom-size' );

4 thoughts on “WordPress Media Library pre-processing with phpThumb

  1. This is great! I don’t understand your description of how to set it up for custom image sizes though. I’d like to be able to define a different setting for large and thumbnail size images. Can you show me what the code would look like?

    Monty

  2. Well, I suppose you could resize the image. Difficult if you use a screen reader. And then there’s the loss of quality in the resized (smaller) image as well as the issue of keeping track of different versions of the same image. Argh!

  3. Pingback: terrance

What say you?