Troubleshooting imagecache
The imagecache module is able to serve different versions of uploaded images. It does this in an ingenious way, making it hard to diagnose once something goes wrong. The following may help you.
How does imagecache work?
Imagecache URL's
In the Drupal menu system imagecache can be found at the URL "/<file system path>/imagecache". Supposing you instructed imagecache to create a "small" version of an image, the corresponding imagecache URL will look like this:
http://www.example.com/files/imagecache/small/files/pictures/img001.jpg
This is composed of four parts:
- files - your Drupal files directory e.g. 'files' or 'sites/default/files'
- imagecache - the module name
- small - the imagecache version ("preset") name
- pictures/img001.jpg - the path of the originally uploaded file
The trick
Drupal's .htaccess file contains rewrite rules. Basically these rules say:
- Try to find whether a file matching the exact URL exists. If so, serve the file directly.
- If not, start Drupal and feed it the URL path.
If http://www.example.com/files/imagecache/small/files/pictures/img001.jpg is requested for the first time the file does not yet exist. So option 2 is chosen: Drupal is started and given the path /files/imagecache/small/files/pictures/img001.jpg. Drupal in turn starts imagecache and gives it the path /small/files/pictures/img001.jpg. This directs imagecache to create a "small" version of /files/pictures/img001.jpg.
At that moment imagecache does two things:
- It serves the small image back to the user
- It copies the file to /files/imagecache/small/files/pictures/img001.jpg (if necessary creating subdirectories along the way).
You can guess what will happen the next time the file is requested: option 1 is chosen, the file is served directly. Now you know where the name imagecache comes from.
What can go wrong?
No images in the imagecache directory
First, don't jump the gun and think there is something wrong. Imagecache only creates its images on demand. This means that when you create an imagecache profile, the imagecache directory {}/files/imagecache/{profilename}/ starts off empty.
Only once you've requested a few files using the imagecache URL (or add-on utilities) will the images be created one-by one. The directory will be entirely flushed whenever you modify the preset. Keep this in mind if watching the file folder when debugging.
If you are monitoring that folder remotely via FTP or a web interface, you will frequently need to refresh that view.
Files directory
Go to Administer > Site configuration > File system. Are the file system path and temporary directory writable by Drupal? Is the download method public?
Rewrite
Obviously rewriting needs to work. And clean URL's. Does rewriting work in your files directory? Is .htaccess allowed?
Try this:
- Upload a file. After that http://www.example.com/files/file.jpg should return the file. And http://www.example.com/files/nonexistantfile.jpg should present a Drupal "page not found" error.
- If you get an error from Apache instead of Drupal, rewriting is not working. Check (with your host) whether .htaccess is allowed.
- If not then this part from the Drupal .htaccess file should be copied to the Apache configuration file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA] - If .htaccess is allowed make sure the original Drupal .htaccess file, including the exact lines above, exists in the main Drupal directory. And secondly make sure the .htaccess file under your files directory contains exactly these two lines (no more no less):
SetHandler Drupal_Security_Do_Not_Remove_See_SA_2006_006
Options +FollowSymlinks
Note for sites upgraded from Drupal 4.7: Older versions of Drupal wrote an extra line to the .htaccess file disabling mod_rewrite which is no longer necessary but will interfere with ImageCache.Of course both files should be readable by Apache.
- If it still doesn't work, and you are using Drupal in a subdirectory, uncomment and modify the RewriteBase.
RewriteBase /<your subdirectory here>
Spaces in filenames
Spaces or other strange characters may prohibit the rewriting process to work correctly. Try uploading images both with and without spaces in their filenames to see if there is a difference.
If this turns out to be the problem:
- Download and install the transliteration module
- Enable the transliteration module (which will fix future upload problems)
- Replace the original theme_imagecache function by this one:
<?php
function theme_imagecache($namespace, $path, $alt = '', $title = '', $attributes = NULL) {
$attributes = drupal_attributes($attributes);
if (module_exists('transliteration'))
$imagecache_path = file_create_url(file_directory_path() .'/imagecache/'. $namespace .'/'. transliteration_get($path));
else
$imagecache_path = file_create_url(file_directory_path() .'/imagecache/'. $namespace .'/'. $path);
return '<img src="'. $imagecache_path .'" alt="'. check_plain($alt) .'" title="'. check_plain($title) .'" '. $attributes .' />';
}
?>
This will use the transliteration module to encode the filename - Flush your imagecache preset
Memory
To manipulate an image it is first decompressed in memory. Therefore file size (in MB) does not matter. What matters are image dimensions (in pixels) and color depth. If you find out large images do not get processed, and smaller images do, the available memory is too small. Either constrain the dimensions of uploaded images or increase the PHP memory limit.
Image toolkit
Imagecache depends on an image toolkit (see Administer > Site configuration > Image toolkit on 5.x, or Administer > Site Configuration > ImageAPI on 6.x) to manipulate images. PHP's built-in GD2 toolkit only handles GIF, JPEG and PNG. BMP files are not manipulated at all, and served to the user in their original size.
If you need e.g. BMP support you could try the imagemagick toolkit.
If you want to block certain file types: Imagefield 5.x-2.0 and up allows you to specify permitted upload file extensions.
PHP Settings
PHP "Safe mode" is not recommended and may interfere with the workings of the image processing libraries. Turn it off if you can.
zend.ze1_compatibility_mode should be turned off.

Hope to be useful
I got an error 500 page when accessin the generated images directly.
The thumbnail and images not showing.
In my case the problem was solved totally deleting the .htaccess in the files directory.
Any countereffect?
bye
http://www.womm.it
Thank You and You Might Want to Add Back the .htaccess
Midmood,
I found this 20 minutes into my trouble-shooting... I'm sure you saved me hours.
First I deleted the .htaccess file from the files directory and imagecache started working... hurray. But then I noticed your question, "Any countereffect?" The files directory in my case was from a 2005 site previously running D 4.5 that I upgraded to 6 in many steps. But I never even looked at the .htaccess file inside the files directory. So I then compared what had been in the .htaccess file in the files directory with a fresh D6 install and the contents were different. So I then added the .htaccess file back to the installation, using the one located at sites/default/files/.htaccess from a fresh Drupal install. Imagecache is still working like a charm.
Thanks again,
Shai
content2zero.com
glad to be useful
Glad to be useful :-)... and yes: I wasted hours
In my case I started from a brand new version 6, and the .htaccess didn't have the unwanted line.
So the only way for me was keeping away the file. Maybe it's a matter of the provider's configuration.
Sorry if I didn't answer you so fast. I'm quite new to drupal, and not so used to it's "forum workflow" :-)
Have a nice day
http://www.womm.it
Rewrite issues?
I've run out of ideas on how to fix this problem and would very much appreciate any ideas
I have ISAPI_rewrite working ok I think but as you say in Rewrite #1:
I can go to http://www.mydomain.com - which gets rewritten to http://mydomain.com so this says to me that rewrite is working.
However when you suggest uploading a file (image.jpg) I can see it in sites\default\files\image.jpg but if I try as you suggest going to
http://mydomain.com/files/image.jpg I get a 'The requested page could not be found.' definately a Drupal error. But if I go to:
http://mydomain.com/sites/default/files/image.jpg it comes up fine is this normal? I'm using IIS 6.0 with ISAPI_rewrite and apart from imagecache not working I think it was ok.
I've updated to PHP 5.2.11 from 5.2.8 because I couldn't get imagecache working and I have the latest modules. My .htaccess file (in Drupal root) is just like yours in step #3 (with extra comments)
The .htaccess file in sites/default/files is exactly the two lines you have in step #4
zend.ze1_compatibility_mode is set to off in php.ini
The directories I'm using for my tmp folder and the sites\default\files folder is completely writeable by Everyone.
My PHP memory limit I've set to 128M and I'm not using spaces in any of my file names. I have tried both GD and ImageMagick and still I get nothing.
I have rebooted my server and done everything I can think of (apart from getting mydomain.com/files/image.jpg to work)
Do you know how I can fix this or have any ideas as to what could be wrong?
Any help much appreciated!
Rewrite issues? YES Indeed!
Yup. If you're like me and hosting your Drupal site on IIS 6.0 there's a definate lack of help on this matter - Until now!
Got it working and 2:05 this morning! I'm too excited to go to sleep now but what the heck, it's been 3 days of slogging my guts out on this issue so for anyone else who is having a huge amount of grief on this matter, there's a complete (if slightly long winded explanation - which is why I'm not posting it here but on my site)
http://www.caspianit.co.uk/imagecache-wrong-path/
pulling my hair out
I've been working on imagecache for 2 days now and can't get it to work...i feel like i've gone through this troubleshooting guide 4 times but I'm still getting an error. not sure what the cause is...
the image is located at:
http://test.genesisforestry.com/sites/test.genesisforestry.com/files/ima...
trying to access the preview here :
http://test.genesisforestry.com/sites/test.genesisforestry.com/files/ima...
this is the error i get:
* warning: getimagesize(sites/test.genesisforestry.com/files/imagecache/gallery_photo/imagecache_sample.png) [function.getimagesize]: failed to open stream: No such file or directory in /home/content/g/a/f/myusername/html/sites/test.genesisforestry.com/modules/imagecache/imagecache.module on line 479.
* warning: stat() [function.stat]: stat failed for sites/test.genesisforestry.com/files/imagecache/gallery_photo/imagecache_sample.png in /home/content/g/a/f/myusername/html/sites/test.genesisforestry.com/modules/imagecache/imagecache.module on line 482.
any ideas? this is driving me crazy! my site is hosted by godaddy so i feel like it could be a godaddy issue, permissions, or my multisite setup. clean urls are enabled and working. tmp directory appears to be working as there are copies of files there. imagecache folder exists, but nothing is writing to the folder at all. upgraded memory on php.ini to 256mb. i'm at a loss!
I'm having similar problems
I'm having similar problems on some sites I'm developing. It only happens where I'm using multisites, and the server I'm on has safe-mode turned on, so my guess is that the problem is safe-mode combined with multisites.
Sample png image file hangs but other .gif and .jpg files work
The sample png file provided (imagecache_sample.png) hangs in the browser then trying to run it as suggested when setting up the image cache module:
http://www.example.com/sites/default/files/imagecache/thumbnail/imagecac...
The sample png file is definately there as it can be accessed through:
http://www.example.com/sites/default/files/imagecache_sample.png
Other .gif and .jpg images appear to be working e.g.
http://www.example.com/sites/default/files/imagecache/thumbnail/someothe...
Any ideas on how to get the sample png file workng?
Activating "ImageAPI GD2" fixed this for me
had the same problem, just activated "ImageAPI GD2" module and it worked fine for me.
300 dpi
After messing up my site with following the changes suggested above (e.g. I think in recent Drupal versions the standard .htaccess is set up fine already) I found out that for my server no images work which have any higher resolution than 72 dpi.
It would be great if you somehow could limit the dpi to some level...