TimThumb is a popular image resizing script that was created for Mimbo Pro - but it's never worked properly with WordPress MU, so I wanted to change that.
The reason it doesn't work is quite simple. Because of the way WordPress Multisite (formerly WordPress mu) runs multiple blogs it uses a server rewrite rule to point to media uploads. This in turn means that the file paths for the images do not actually exist in the way they are used in blog posts.
People have asked me how to make TimThumb work with multisite quite a few times, but now I've looked into it I have concluded that TimThumb doesn't need changing - it does what it's told to (resize images). What needs to change is the file path of the media being resized - which means we need to alter our themes to work with multisite. For reference all Pro Theme Design themes, including Elemental, already have this functionality included.
I want to emphasize that you can not modify TimThumb to work with WordPress MultiSite (or mu). Changes you make for your website are unlikely to work for other peoples. Editing your theme to support TimThumb is the best way to go, and it's surprisingly easy.
When using TimThumb I would recommend using the latest version of the plugin, and passing it the absolute url of the image you want to resize - however for WordPress Multisite the path on the server does not match the website path, and you need to take this into account.
The first thing to do is create a function to grab the image path (and not just echoing a get_post_meta value). This will make future changes easier.
We should then do a series of things to get the correct file path for the image.
- Determine if the theme is being used on WordPress Multisite or WordPress normal
- Determine if the image is on the blog domain or some other domain
- Work out the blog ID
- Calculate the file location based on the gathered info
Which turns into code that looks something like this:
$theImageSrc = 'path/To/Image.jpg';
global $blog_id;
if (isset($blog_id) && $blog_id > 0) {
$imageParts = explode('/files/', $theImageSrc);
if (isset($imageParts[1])) {
$theImageSrc = '/blogs.dir/' . $blog_id . '/files/' . $imageParts[1];
}
}
Pulling this together
Since most themes use a custom field to store the thumbnail image path a PHP function to calculate the image location would look something like this:
function get_image_path ($post_id = null) {
if ($post_id == null) {
global $post;
$post_id = $post->ID;
}
$theImageSrc = get_post_meta($post_id, 'Image', true);
global $blog_id;
if (isset($blog_id) && $blog_id > 0) {
$imageParts = explode('/files/', $theImageSrc);
if (isset($imageParts[1])) {
$theImageSrc = '/blogs.dir/' . $blog_id . '/files/' . $imageParts[1];
}
}
return $theImageSrc;
}
Hi
Please ignore my last post that didn't work.
I am using the plugin 'Download Manager' (http://www.wpdownloadmanager.com/) and it uses timthumb.php in a number of places to display images and doesn't support WPMS.
In one of the page templates for example is calls:-
if($file[preview]!='')
$file['thumb'] = "";
else
$file['thumb'] = "";
and in the plugins own functions.php file an example would be:-
$data['thumb'] = "";
I've added the function in functions.php but how would I amend the code?
Would really appreciate your input.
Glennyboy
March 28, 2012 • @slickmedia
Hi,
I'm having trouble with this timthumb wpmu problem for a long time.
The thumbs don't generate. You can see the problem here: http://chabelle.be/cbs/nieuws-cbs/#nieuws-cbs
I'm not an expert to know if it's timthumb or something with the theme...
Can somebody help me with this? It's for a client of me and been searching very long for a fix.
Thanks.
April 4, 2012
Okay, so I've added this code to functions.php...
function get_image_path ($post_id = null) {
if ($post_id == null) {
global $post;
$post_id = $post->ID;
}
$theImageSrc = get_post_meta($post_id, 'Image', true);
global $blog_id;
if (isset($blog_id) && $blog_id > 0) {
$imageParts = explode('/files/', $theImageSrc);
if (isset($imageParts[1])) {
$theImageSrc = '/wp-content/blogs.dir/' . $blog_id . '/files/' . $imageParts[1];
}
}
return $theImageSrc;
}
but that doesn't seem to work. when I look at the slider, and look at the image info for the image that isn't working, I see that the img source is:
http://www.lipolitics.com/...8;w=310&zc=1&q=90
Any thoughts?
April 30, 2012
I was able to fix my issue by doing TWO things.
I turned this line to true: [line 33]
if(! defined('ALLOW_ALL_EXTERNAL_SITES') ) define ('ALLOW_ALL_EXTERNAL_SITES', TRUE); // Less secure.
and I commented out this line [line 212]
//$this->src = preg_replace('/https?:\/\/(?:www\.)?' . $this->myHost . '/i', '', $this->src);
Whether or not this is an appropriate way, it is the fix I found to work after 8+ hours of troubleshooting.
Hopefully this helps some people!
June 27, 2012
Thank you Jacob Raccuia, I searched per hours on the internet and tested, nothing worked. Finally I tried your solution and it works perfectly ! thanks a lot
July 12, 2012
Hours ?? I spent 3 days to find this ! And when I almost gave up and turn back to add_image_size() ... So - BIG THANKS (at this moment I don't give much damn if it's less secure or what ...)
July 30, 2012
Jacob, really thanks.
October 3, 2012
Thank you JACOB RACCUIA from the bottom of my heart!
October 4, 2012
Excellent suggestion, Jacob. Thank you! I think it would be better to add this to a timthumb-config.php file, rather than changing line 33:
<?
$ALLOWED_SITES = array (
'',
);
?>
This would still allow the same functionality that you suggested, but it would limit the danger by only allowing images from the intended domain.
I do agree that it's necessary to comment out the
preg_replaceline to get this to work smoothly, though. Nice catch! I wonder why this isn't done by default?November 15, 2012
Hrm. Well that didn't work. In between the empty single quotes of the array should go your domain name. Sorry for the confusion.
November 15, 2012
I implemented your advice on a couple of my sites and was hacked via timthumb shortly thereafter. By setting ALLOW_ALL_EXTERNAL to true, you let a hacker upload php scripts from their website to your timthumb cache directory where they are able to execute the script.
Here's how to do this properly:
1. Make sure that ALLOW_EXTERNAL is set to TRUE [line 32] and ALLOW_ALL_EXTERNAL is set to FALSE [line 33]
2. Comment out [line 212]
//$this->src = preg_replace('/https?:\/\/(?:www\.)?' . $this->myHost . '/i', '', $this->src);
3. Go to the $ALLOWED_SITES array [line 125] delete all the default sites, and add your site's domain.
I would also recommend downloading the Timthumb scanner plugin and installing on your root site to make sure you've fixed all the versions of timthumb in your wp-content folder.
February 5, 2013
Hi Peter - really sorry to hear you were hacked. This problem was found 1 and a half years ago and was fixed in version 2 of TimThumb. I would recommend keeping the software up to date so that you can solve these issues before they happen.
February 5, 2013
IT WORKS!!!! Thank you, thank you thank you. 2 months ago I spent even more days trying to fix it before giving up. Then trying again now I have spent hours trying to fix this problem.
Don't know how you came up with the solution, but good job! You got some skills there.
February 7, 2013
THANKS, JUST THAT, THANKS!!!
April 9, 2013
Thanks for the fix! Very useful
August 30, 2012
I get this message, Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 20736 bytes) in /storage/content/49/146049/unitedsardinefactory.com/public_html/wp-content/themes/blacklabel/framework/timthumb.php on line 1085
Any idea what the problem is? I run WordPress as an installation on my webhost, so I don't think I have access to the files. Help would be so appreciated.
November 3, 2012
The problem is in the description. Your server ran out of memory. You were probably trying to resize an image that is too big
November 3, 2012
JACOB RACCUIA you are amazing - just your method works!!!
November 26, 2012
I created a script that loads timthumb.php into wordpress, it uses multisite. it is basically a function that you can call from your wordpress template /widget/plugin
It can be found here http://github.com/...yarich/wordpress_timthumb_multisite
this script supports all acceptable timthumb parameters http://www.binarymoon.co.uk/...imthumb-parameters-guide/
How to use:
1. you add the mr_timthumb function to your functions.php
2. edit the location to the timthumb.php script , change the value for the variable $timthumblocation
3. then call timthumb like so
$timthumb_img_param = array(
'src' => $orig_uploaded_img,
'w'=>590,
'h' => 315,
'a'=>'top'
);
mr_timthumb($timthumb_img_param);
January 5, 2013
Thanks for the contribution. Personally I don't see a need for this - TimThumb isn't difficult to use. But perhaps others will like it.
January 5, 2013
If you use this link
http://piranagroup.com/blog/undercurrents/
it will lead you to a page if you notice this page is not able to generate thumbnails i guess it has to do something with the timthumb.php as when you click on it the image gets enlarged.
Can you please help me with this.
February 28, 2013
If you view the image you will see the error. In this case the GD library does not exist which means TimThumb can't work
March 1, 2013
i don't this workaround is working anymore with new installs of WPMU using WP3.5.1 - works for subdomain setup but am having issues with the subfolder setup
anyone else notice this?
March 11, 2013
sorry that should have read...
i don't "think" this workaround is working anymore with new installs of WPMU using WP3.5.1 - works for subdomain setup but am having issues with the subfolder setup
anyone else notice this?
March 11, 2013
No. I don't think this works now either. I've been trying for two days to get this to work too and it's getting on my nerves. I'm using a theme which had timthumb embedded (Splashing Pixels 'Mio' so I'm kind of stuck with it)
So many 'fixes' which just seem to cause other problems.
Everything works on the main site but can't get any images on the subfolder sites.
Any help gratefully received
March 15, 2013
Just keep getting
Could not find the internal image you specified.
errors even though the image is there when you grab just the image url.
March 26, 2013