WPMU media locations
WPMU implements a URL rewrite for uploaded media so that the location of an uploaded file is given as http://www.example.com/files/2009/01/image.jpg
, but the actual URL of the file is http://www.example.com/wp-content/blogs.dir/BLOG_ID/files/2009/01/image.jpg
(where BLOG_ID is the ID of the blog in question).
TimThumb problem
The easiest way to specify the URL for a post image (whether you’re using Thesis post images, or custom fields to add a post thumbnail, or something else) is to copy the URL that WordPress inserts into the post. However, this URL doesn’t work with the TimThumb script (which Thesis and other themes use to generate thumbnails from larger images). Essentially TimThumb looks for the image file at the URL you specify, but the image file doesn’t exist there and the redirect that allows you to see the image in the browser doesn’t work for the script.
Overcoming the TimThumb problem
There’s two ways that I can see to get TimThumb working on your site: modify your behaviour, or modify the script’s behaviour.
Modify your behaviour
You could simply edit the URL that you copy from WordPress and add wp-content/blogs.dir/BLOG_ID/
into the image URL you want to use.
I don’t like this because it won’t fix any posts that have already been written and it’s extra work for the user every time they write a post. It also might not be easy for some users to remember that they need to change the URL, or in what way they need to change it.
Modify TimThumb’s behaviour
I came across a TimThumb hack for Thesis, but it didn’t work for me at all. I suspect it had something to do with my image URLs not being relative, but I’m not sure.
I was able to use the first part of the information in that post to make the blog IDs available in the TimThumb script and then using PHP replace the /files/
part of the URL with /wp-content/blogs.dir/BLOG_ID/files/
.
In the Thesis theme (version 1.7 at the time of writing) the TimThumb script is located at thesis_17/lib/scripts/thumb.php.
Update 5th Nov 2010: Changed line 448 below to fix a problem with this on blogs within a site network. Also, these edits still work the same with Thesis 1.8.
- At line 23 (after the end of the file comments section) I added
require_once('../../../../../wp-blog-header.php');
to make the blog IDs available.
- After line 447, which says
$src = preg_replace("/\.\.+\//", "", $src);
To line 448 (initially an empty line) I addedglobal $current_site; global $current_blog; $src = str_replace($current_blog->path.'files/', $current_site->path.'wp-content/blogs.dir/'.$current_blog->blog_id.'/files/', $src);
to replace the section of the image URL we need to change.
That’s all.
Further comments
Other themes may use their own version of the TimThumb script, or they might use the one included with WPMU. These modifications should work with them, but the line numbers might be slightly different.
In theory this modification could be present in regular WordPress installations and in WordPress 3.0 with networks turned on or off (assuming WP3.0 directory structure is the same) without affecting the existing behaviour of the script. If “/files/” is not found in the image URL, it won’t be replaced and the image URL will remain the same.
In the long run it might be better to use the thumbnails built into WordPress. However, the caveat there is you will need to regenerate the different image sizes after setting up theme support for thumbnails, whereas TimThumb generates thumbnails on the fly and then caches them: changing image sizes is not a big deal with TimThumb.
Gary says
That’s a great find Kristen, excellent.
In terms of re-sizing existing images, something like http://wordpress.mfields.org/p.....e-utility/ as a plugin may help.
Dixie Vogel says
Kristerella – Thanks for linking to my article, and adding your solution, which is clearly more robust than what I came up with hacking it out on my own. 🙂
I’ve worked with several versions of the timthumb script, most recently the beta that allows showing thumbs from images hosted at other domains so I can use Flicker, FB, and other remotely hosted images. The differences in the version I used may well account for why my hack didn’t work for you out of the box (and an excellent reminder to note what versions of what I’m using when I write this stuff up).
Appreciate all your work here. The info I found on your site is why I’ve been able to use Thesis with MU to begin with.
Thiet ke logo says
Thanks, i wan’t allows showing thumbs from images hosted at other domains so I can use Flicker, FB, and other remotely hosted images, thanks for work
kristarella says
Thiet — Sorry, I don’t understand your comment. Are you saying thumbs won’t work from other domains (this is true with the standard timthumb script) or are you asking me to tell you how you can do that (a Google search should bring that up, I did see such a modification while I was researching this problem)?
Dixie Vogel says
The only version of TimThumb I know of that allows remotely hosted images out of the box is the beta script here: http://www.binarymoon.co.uk/20.....beta-test/
If you hack this to work with Thesis/WPMU in conjunction with domain mapping, you’ll need to add each domain used in the allowed domain list manually, via editing the script.
kristarella says
Dixie — Thank you!
primordialsoup77 says
Thanks for sharing this hack: this problem was driving me nuts and I was not looking forward to manually defining thumbnails!
Mark Hodder says
Appreciate you taking the time to write this, just fixed my issue!
Lee Hughes says
Can I just confirm something please,
Is this aimed to auto crop the post image to the thumbnail? I’m using thesis 1.7 and WP 3.0 and i’m having trouble getting the thumbnail to re-size on the teaser page. Would this script help do that?
Many thanks
kristarella says
Lee — Yes. This was to get the thumbnail script working in WPMU, exactly the same as it worked in regular WP, so resize+crop. I did it with WPMU 2.9.2, I don’t know how it goes with WP 3.0. It should work as long as WP 3.0 put uploads in the same place as WPMU did and outputs the upload URLs the same way, i.e., with the /files/ rewrite in the URL. You might need to check the description at the top of the post against your site’s file structure to see if it is the same.
A few notes: this is only for multi-site, and shouldn’t affect a regular single blog WP install. Also, sometimes you need to reload the page a couple of times to get the images to generate, cache & display properly. There may be other server-related reasons for timthumb not to work. And I believe the images have to be on the same server as your WP install because it uses the PHP fopen() function to open the file, but PHP won’t have access to external servers.
Paul Colman says
Hi Kristarella
Thanks for this info. Unfortunately it didn’t work for me but…..
I have noticed that as well as my thumbnails not working I’ve just noticed that the Thesis default settings for the post image are ALSO being ignored i.e. not just the thumbnail.
For example, if I set post images to be centered in the Thesis design options section, it makes no difference to any of my posts. It seems whatever I do in post image or thumb image in Thesis it makes no difference. Do you think the problems are connected? Have you come across this before in conjunction with missing thumbnail images? If not then maybe not appropriate for this post – It’s a long shot but I’ve tried everything else I can find.
Paul
kristarella says
Paul — It’s possible those problems are related, but not necessarily. I think the first thing to check is whether your host has
fopen
enabled (it allows PHP scripts to open and do things with files on the server). It is crucial for timthumb to work and is used in dealing with post images, so it could have some bearing there.Paul Colman says
Kristarella – thanks for the quick response. So far I have:-
1) used phpinfo – I can confirm that ‘allow_url_fopen is OFF’
2) used your mods above to the thumb.php file
3) checked that the route urls are the same for image and call i.e. both are @ pcolman.com (tried variations of www and without www)
4) tried every which way to insert an image into a post and tried different folders in route directory
5) changed permissions as in this post – http://tiny.cc/u9550
6) also tried permissions and solutions in this post – http://tiny.cc/5cq7e
7) modified getimagesize in the post_images.php file – currently awaiting answer from host re: any php errors created (see this post – http://tiny.cc/hqdnr)
DIY/Thesis expert (Girlie) on the forum has suggested the problem is most likely to be with getimagesize in the post_images.php file as Thesis global settings aren’t having an effect on my main images let alone thumbnails (so perhaps the thumbnail issue is a red herring – if so apologies for posting here).
Either way I’ll let you know if I get any answers from my host in (7) or find anything else out which may be useful to you/your readers. In the meantime if you have any brainwaves I’d really appreciate it.
kristarella says
Paul —
allow_url_fopen
might not have to be on for these things to work, but there are other restrictions that can be made on fopen to prevent it behaving properly. However, turningallow_url_fopen
on could be a step in the right direction. If fopen doesn’t have the permissions it needs andallow_url_fopen
is off PHP won’t have access to the file to resize it, so no thumb will be generated.getimagesize
requires similar permissions in that it needs access to the file to determine the size and may require special permissions to get the size of an image from a URL rather than filepath… it may even depend on fopen, but I’m not sure.I think that fopen is too restricted on your server for PHP to open the image files. I don’t know what steps can be taken to fix this, it probably depends on the host and they should be able to tell you.
Shelyn says
Hi Kristarella,
I don’t understand this : require_once(‘../../../../../wp-blog-header.php’)
What should I adjust in this code in order to get this work? Thanks!
kristarella says
Shelyn — It is a relative filepath.
../
indicates one folder up from the current location. You shouldn’t have to adjust it, what is shown worked for me for a normal install of WordPress (i.e., WP files in the root directory and the path to the timthumb script is wp-content/themes/thesis/lib/scripts). However, if it doesn’t work for you, you may have to adjust the number of../
in the path.Shelyn says
Kristarella,
Thanks!! It works now, but some how it doesn’t work for the photos stored in Photobucket. Did you encounter same issue?
For eg. http://www.travel-living-bliss.....&zc=1 -> this doesn’t work.
But
http://www.travel-living-bliss.....&zc=1 -> this works.
Thanks!
kristarella says
Shelyn — Yes, TimThumb only works with images on the same server. It needs PHP access to the direct file, it can’t use URLs. I seen some posts on getting it to work with external URLs, but I couldn’t get it to work.
Paul Colman says
Kristarella
Just following up on my posts above in case it helps others. After a frustrating exchange with my web host I now have an answer as to why the script isn’t working. Here is their reply:
“I’ve referred this to one of our systems admins and they have confirmed that one of the components required by this script is not installed on our shared servers. Unfortunately due to the nature of our hosting it isn’t possible to install the imagemagick library it requires. WordPress does include image manipulation tools by default so you may be better off using those instead.”
So I guess the lesson here is if you want to use this script/Thesis don’t get your hosting from Easyspace.com because it won’t work 🙁
Paul
kristarella says
Paul — Thanks for the follow up. That’s a shame. I wonder what else doesn’t happen on the server… I didn’t think the whole imagemagick library was needed, but I don’t really know about these things. Does your install generate different sizes of images when you upload them via WordPress?
Paul says
Hi Kris,
Do you have any idea whether Thesis has plans to fix this issue in future releases? Multisite is now more mainstream with wp3.0 so I hope that Thesis will make it part of the core files out of the box.
kristarella says
Paul — I actually have no idea if Chris plans to include a modified version of TimThumb with Thesis. I did send him a copy of the file with these mods when I came up with it, so maybe if you hound him on Twitter he’ll include it.
Paul says
Thanks for the tip. Chris has no plans for the fix in Thesis 1.8. So for now, your work-around will have to do. Works great btw 😉
Paul says
I’m using WP Multi-site (upgraded from WPMU), and have thumbnails working with the thumbs.php hack you’ve provided above. Thanks.
I’ve set up an image store on one of our sub-domains and would like to serve all sitewide images from there. As Thesis only supports auto-generation of thumbnails from the same domain, (sub-domain), is there a way to alter code to allow my images subdomain to be recognised as being ‘same domain’ as my other content subdomains > and hence be able to serve all my images from the one repository.
It’s not very effiicient with our multi-site structure to have separate image folders for each blog//site as they are all part of a network of functional blogs all feeding the one purpose.
thanks Kris
kristarella says
Paul — When I was playing with this I didn’t get external image URLs to work, but there are tutorials out there so perhaps one of them will work for you.
Paul says
Thanks Kris.
So a subdomain is considered as an external domain then?
kristarella says
Paul — I’m not sure. If using the images as they are on the subdomain doesn’t work with timthumb by default then I would think that they are being treated as an external domain. But since they’re actually on the same server they might just work. Have you tried using them from that domain yet?
Paul says
yes, subdomains not working. 🙁
I have no clue personally, but I’m assuming that it’d be a relatively simple code change to get it to recognise the sub.
I’m actually considering moving away from thesis image control anyway as I’m wanting all images sitewide to link back to the images.site.com subdomain anyway. Thesis Post Image control doesn’t seem to allow this, wheras wp media gallery control does. i.e. source url and link url.
Do you have experience in making the ‘Set Featured Post’ feature in WP to work with Thesis teaser thumbs?
thanks again.
kristarella says
Paul — Sorry for the delay. Yes, using WP thumbs isn’t too hard. Check out this post about WordPress thumbnails. You just need to paste the theme support and thumbnail size lines in custom_functions.php then you can hook
the_post_thumbnail();
into posts and/or teasers. You basically use it instead of Thesis thumbs. Or you can set up a conditional code to only show the post thumbnail if the thesis thumb isn’t there.Paul says
Thanks Kris.
I’ve added the theme support and thumbnail size to custom_functions.php. Can you give me an example of the function you’d write to hook the thumbnail into teasers?
Andrew Carter says
I’ve been searching around like crazy and there’s so many different solutions for this issue out there. I ended up doing something similar to you (http://andrewroycarter.com/wor.....ulti-user/) but i can’t decide which way is best!
kristarella says
Paul — Sorry for the delay. Conveniently Bill just posted about that! Featured image thumbnail.
You can use his code straight, or you could add a bit of code in there to only use the featured image thumb if there’s no Thesis thumb by doing something like:
kristarella says
Andrew — I can’t really say which is better either. I like mine because it’s only two little changes. The other one is handy because it appears to make it useable with images on external servers, which is pretty handy if you’re hosting images elsewhere to save on bandwidth or a big Flickr user etc.
Ryan says
Kristarella, I made the modifications to thumbs.php. I using WP 3.0.1/Thesis 1.8. I noticed the path to an image on my blog is:
/wp-content/blogs.dir/{$blog_id}/files/2009/10/{filename}.jpg
Does the hack above for for images stored below files/ [year/month/filename]?
kristarella says
Ryan — Yes, I pretty sure WP keeps the dated folder structure when it rewrites the image URLs.
Ryan says
Kris, Thx for this mod. It’s exactly what I need. Attempt #2 to get this going under 3.0.1 and Thesis 1.8.
I’m getting a file not found error because the blog name (subdirectory) is being added to the path that thumbs.php uses for finding the image. For example, on one of my blogs, I’m getting the following error when trying to access the file using the URL that thumbs.php uses.
file not found /politalogue/wp-content/blogs.dir/4/files/2008/04/ChavezMickey.jpg
URL: http://xero.net/politalogue/wp.....#038;q=100
Take the “/politalogue” out and the image is found.
Thoughts?
kristarella says
Ryan — Ah, that’s interesting, and I suppose not entirely unexpected, now that I think about it. I thought perhaps the WP htaccess rewrites would take care of it, but I guess not. You can fix that by determining the site (i.e., the main site, not the current blog within that site) path and replacing that with the main site path. Change line 448 to
global $current_site; global $current_blog; $src = str_replace($current_blog->path.'files/', $current_site->path.'wp-content/blogs.dir/'.$current_blog->blog_id.'/files/', $src);
. I’ve tested this on my WP network and it works. I’m going to update the post with it.Ryan says
Kris, Yes! It works great!! Thank you.
Max says
Does this work on non–WPMU sites too?
kristarella says
Max, it works on regular WP in the sense that it won’t affect it: what it does is address the problem that WPMU or WP networked sites have where WPMU rewrites the real image URLs. It won’t affect regular WP because it only changes the image URL if it detects the /files/ pattern in the image URL.
Max says
Thanks for that quick reply!
I would like your suggestion in that case as to how do I dynamically resize an image? I want such a functionality that – no matter what size image I upload, the image would be resized to say 500 x 500. I used a plugin nCode image resizer for this purpose.. but it was a memory hog. Hence, I had to remove it. Could you please help me?
kristarella says
Hey Max,
WordPress already does that. Check out the settings for media (I think) and you can set the sizes for thumbnails, medium and large image sizes that WordPress will resize your images to.
Barbara Feldman says
Well, I am having the dreaded thumbnail problem with WP 3.04 multi-site with Thesis 1.8
Are the code modifications you are suggesting IN ADDITION to those for thumb.php given by DIYTHEMES: http://diythemes.com/thesis/rt.....multisite/
Or in INSTEAD of?
Thanks!
kristarella says
Barbara — This whole post is essentially instructions for modifying TimThumb instead of the ones given at DIYthemes. Did the above not work for you?
Barbara Feldman says
Oh… Kristarella, you rock! Yes, it works!! My original problem was that I was applying BOTH edits (DIYthemes and YOURS) to the thumb.php file.
Thank you for your help.
Jason Sanders says
Kristarella,
I’m having problems with my WPMU site and I’m not sure if it’s tied to this particular problem or not because I don’t understand TimThumb.
I’m using your hack for thesis and have found that after upgrading to thesis 1.8 and wp 3.04 I can’t upload pictures at all. Whether I’m trying to do it inside the post/page or through the library function i get the same error:
Warning: Invalid argument supplied for foreach() in /home/thevalue/public_html/wp-includes/functions.php on line 2365
Dismiss “P1000325.jpg” has failed to upload due to an error
File type does not meet security guidelines. Try another.
I also get this “error message” at the top of the admin screen:
Warning! The current theme supports Featured Images. You must enable image uploads on the options page for it to work.
When I go to the options page and make changes, they never actually save. I’m not sure if all of this is related or if it has to do with TimThumb, but if you have any insights for me I would really appreciate it!
kristarella says
Jason — I don’t see how that would be caused by TimThumb, since it is only used when the homepage generates a thumbnail, it’s not really relevant the running of the WordPress dashboard. I would think that there may be another plugin or another function that is messing things up. Try disabling all your plugins and then see if things work. If they don’t try switching to the Twenty Ten theme and see if they work. If they do then it’s probably something in your custom functions file that’s messing things up.
Dee says
Hi Kristarella,
I am using the latest version of WordPress Multisite and Thesis. I cannot get thumbnails to show. All that’s there is a placeholder. Would your solution work with these latest versions?
kristarella says
Dee — Yes, this mod works for the latest versions… I also had troubles with post images with multisite and I had to comment out (by putting // at the start) lines 65 (
if (@getimagesize($image_path)) {
) and 86 (}
) of thesis/lib/functions/post_images.php. Not sure why it wasn’t finding the post images, perhaps due to the subdomains in the site URLs.Dee says
Thanks. I’ll try that and see what happens. I’m working on my local server. I can get the post images ok and thumbnails on the first blog in the network. I just can’t get them in the other blogs. Frustrating.
paul says
thanks for this, i used the str_replace bit in my code as that seemed simpliest for what i was trying to acheive…