You are here

Overriding how an image displays

One of the big problems we have is that we have a staging server and then copy that information across to our live server which is proxied via another server. Unfortunately because Drupal uses absolute URLs this breaks things like images. A quick change to the way image cache generates the URLs does the trick.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function cls2_imagecache($presetname, $path, $alt = '', $title = '', $attributes = NULL, $getsize = TRUE) {
  // Check is_null() so people can intentionally pass an empty array of
  // to override the defaults completely.
  global $base_url;
 
  if (is_null($attributes)) {
    $attributes = array('class' => 'imagecache imagecache-'. $presetname);
  }
  if ($getsize && ($image = image_get_info(imagecache_create_path($presetname, $path)))) {
    $attributes['width'] = $image['width'];
    $attributes['height'] = $image['height'];
  }
  $attributes = drupal_attributes($attributes);
  $imagecache_url = imagecache_create_url($presetname, $path);
  $regex = "^" . $base_url . "^";
  $final_url = preg_replace($regex, "", $imagecache_url);
  return '<img src="'. $final_url .'" alt="'. check_plain($alt) .'" title="'. check_plain($title) .'" '. $attributes .' />';
}
 
Tags: