tags:

views:

53

answers:

1

Is there away to get ImageCache to use a default image? Or using htaccess to provide a default image to imagecache to process? Some of our clients sites are >4GB's and it's very painful dealing with all of their images that we don't need for development. I've tried using htaccess but ImageCache does not process the file and just ends up using the files dimensions which screws up the layout.

Any thoughts?

A: 

As I understand ImageCache responds to URIs like

http://www.yourdomain.com/default/files/imagecache/set/images/pic.png

where http://www.yourdomain.com is your domain, files/imagecache is the imagecache path, set is the predefined set of image manipulation settings and the rest (here: images/pic.png) is the actual relative path of the original image.

So, if pic.png doesn't exist, another file (default.png) should be served to ImageCache. An .htaccess solution for non-existant files could be:

RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule ^([^.]+)\.[gif|jpg|png]$ /images/default.png [L]

Now ImageCache requests images/pic.png which does not exist and gets images/default.png served, processes it and saves it at default/files/imagecache/set/images/pic.png.

Well, at least this is my theory.

Regards, Paul

-###########-

EDIT regarding first comment: Ok, I looked into the module. In imagecache.module, line 386 starts the helper function _imagecache_cache($presetname, $path). Within this function is a check for the existance of original file (line 403). Change this block

  // Check if the path to the file exists.
  if (!is_file($src) && !is_file($src = file_create_path($src))) {
    watchdog('imagecache', '404: Unable to find %image ', array('%image' => $src), WATCHDOG_ERROR);
    header("HTTP/1.0 404 Not Found");
    exit;
  };

to

  // Check if the path to the file exists.
  if (!is_file($src) && !is_file($src = file_create_path($src))) {
    watchdog('imagecache', '404: Unable to find %image ', array('%image' => $src), WATCHDOG_ERROR);
    /*header("HTTP/1.0 404 Not Found");
    exit;*/
    $src = 'sites/all/modules/imagecache/sample.png';
  };

(Notes: I left the original code lines as comments. You can set $src to any default file you want.)

Paul
Paul, I've implemented an htaccess solution but the problem with it is ImageCache does not process the file. Instead it just passes it straight-through. I think it's because PHP is accessing the file from the filesystem and not through Apache. I may just have to make a change ImageCache directly.
fadeddata
Hi fadeddata, I see the problem and added a PHP based solution, just see my post above. Tested it and works like a charm. By leaving the watchdog line it even still reports missing images.
Paul

related questions