Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Process only one size of thumbnail with phpThumb WordPress

  • SOLVED

I need to create little round 50x50 thumbnails automatically on image upload.

I'm using phpThumb and I've got a function in my functions.php that creates the images just fine, I've got the round 50x50 plus all my other thumbnail sizes - BUT - this function seems to make the images inaccessible by WP. They appear as broken links in the Media Library and anywhere I insert them in my theme...

Here's the code I'm currently using:


// add thumbnail size
add_theme_support( 'post-thumbnails' );
add_image_size( 'marker-thumb', 50, 50, true );

////Create round thumbnails
require_once('/home/gdeals/public_html/100seconds.in/wp-content/plugins/phpthumb/phpthumb.class.php');

add_filter('image_make_intermediate_size', 'james_rounded_filter');

function james_rounded_filter($file) {

// Get image information
// Image edtor is used for this
$img = wp_get_image_editor( $file );
// Get image size, width and height
$img_size = $img->get_size();
if($img_size['width']=='50')
{
// 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/gdeals/public_html/100seconds.in/wp-content/plugins/phpthumb/');
$phpThumb->setParameter('config_cache_directory', '/home/gdeals/public_html/100seconds.in/wp-content/plugins/phpthumb/cache/');
$phpThumb->setParameter('cache_directory_depth', 4);

// set parameters (see "URL Parameters" in phpthumb.readme.txt)
$phpThumb->setParameter('f', 'png');
$phpThumb->setParameter('fltr', 'over|50px-border-overlay.png|0|0|100');
$phpThumb->setParameter('fltr', 'mask|50px-round-mask.png');

// 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/gdeals/public_html/100seconds.in/wp-content/plugins/phpthumb/cache/thumblog.txt');
} else {
// do something with debug/error messages
error_log('Failed:'.implode("\n\n", $phpThumb->debugmessages), 3, '/home/gdeals/public_html/100seconds.in/wp-content/plugins/phpthumb/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/gdeals/public_html/100seconds.in/wp-content/plugins/phpthumb/cache/thumblog.txt');
die;
}

if ( !is_wp_error($output_filename) && $output_filename) {
return $output_filename;
}
return false;
}}


So how can I continue to process my 50x50 images via phpThumb, but actually have all the thumbnails accessible by wordpress?

NOTE: I need to create these round images using phpThumb, so please don't suggest alternate ways of making the images such as styling with CSS.

Thanks!

Answers (2)

2013-12-10

Dbranes answers:

It looks like you are not returning the filename for the other sizes:


function james_rounded_filter( $file )
{

// Get image information

// Image edtor is used for this
$img = wp_get_image_editor( $file );

// Get image size, width and height
$img_size = $img->get_size();

if( $img_size['width'] == '50' )
{
//....
}
else
{
return $file; // this part is missing in your code
}
}


jpdavidson comments:

Perfect! That did it. Thanks so much.

JP

2013-12-08

Hariprasad Vijayan answers:

Hello,

Does the above function creates round image for all thumbnail sizes?


jpdavidson comments:

Yep!


Hariprasad Vijayan comments:

I think this thread help you to write condition

http://wordpress.stackexchange.com/questions/123240/rename-image-uploads-replacing-width-and-height-in-filename-with-medium

In that you can see code for getting image size in "image_make_intermediate_size" filter. Like

// Get image information
// Image edtor is used for this
$img = wp_get_image_editor( $image );
// Get image size, width and height
$img_size = $img->get_size();

And you can write if condition using $img_size. Like

if($img_size['width']=='50')
{
// Your function
}

Hope this help you to solve this issue


Hariprasad Vijayan comments:

Is that working?


jpdavidson comments:

Hmm... doesn't seem to be working...

I get this error when I try to upload an image:
Fatal error: Call to undefined method WP_Error::get_size() in /home/gdeals/public_html/100seconds.in/wp-content/themes/GeoJP/functions.php on line 23

and line 23 is this:

$img_size = $img->get_size();

any thoughts?


Hariprasad Vijayan comments:

I think wp_get_image_editor() fails fetching details, please please make sure input parameter for wp_get_image_editor() not empty.


jpdavidson comments:

Ok, I got rid of the error and it's creating the files (including the round 50x50) - but they're still not accessible by wordpress.

Thanks for your help, I've increased the prize for this question and updated the current code I'm using above - hopefully you or someone out there can crack this!

Thanks.