Drupal - imagecache problems with special characters
27th Sep 2008
Mike Dixon
Senior Mind
Hey, you seem to look at this article a lot! Why not Bookmark this article so you can find it easily in the future?
We came across this problem on one of our Drupal sites the other day, someone uploaded an image with an ampersand in the filename, and imagecache refused to display the image. A bit of investigating revealed that imagecache had a problem with a number of special characters in the image filenames.
The solution is a little imagecache theme override - you can see examples for both Drupal5 and Drupal6 below
Drupal 5 version
function phptemplate_imagecache($namespace, $path, $alt = '', $title = '',
$attributes = NULL) {
$attributes = drupal_attributes($attributes);
$imagecache_path = file_create_url(file_directory_path() .'/imagecache/'.
$namespace .'/'. drupal_urlencode($path));
return '<img src="'. $imagecache_path .'" alt="'. check_plain($alt) .'" title="'. check_plain($title) .'" '. $attributes .' />';
}
Drupal 6 version
function phptemplate_imagecache($namespace, $path, $alt = '', $title = '', $attributes = NULL) {
if ($image = image_get_info(imagecache_create_path($namespace, $path))) {
$attributes['width'] = $image['width'];
$attributes['height'] = $image['height'];
}
// check is_null so people can intentionally pass an empty
// array of attributes to override
// the defaults completely... if
if (is_null($attributes)) {
$attributes['class'] = 'imagecache imagecache-'. $namespace;
}
$attributes = drupal_attributes($attributes);
$imagecache_url = imagecache_create_url($namespace, drupal_urlencode($path));
return '<img src="'. $imagecache_url .'" alt="'. check_plain($alt) .'" title="'. check_plain($title) .'" '. $attributes .' />';
}