1

Every time I download a new copy of the live database to my local site I also have to download the photos (several GB). Is there an easy way to write the URL for one "photo" field (type image) to always use the live site urls?

I'm using S3 but haven't found a good way to batch move images and image styles to S3.

1

1 Answer 1

3

theme_image() by default looks like this:

function theme_image($variables) {
  $attributes = $variables['attributes'];
  $attributes['src'] = file_create_url($variables['path']);

  foreach (array('width', 'height', 'alt', 'title') as $key) {

    if (isset($variables[$key])) {
      $attributes[$key] = $variables[$key];
    }
  }

  return '<img' . drupal_attributes($attributes) . ' />';
}

so, if you are storing the images in this field in an identifiable place, eg, you can look at $variables['path'] and figure out they are the ones you want to always look for on a specific host, you should be able to do something like this in your theme:

function MOTOTHEME_image($variables) {
  $attributes = $variables['attributes'];

  if (dirname($variables['path'])==WHEREIKEEPTHISFIELDSIMAGES) { // on d7 this might be a uri so you might need to get fancier than dirname() :)
    $attributes['src'] = // http://.....
  } else {
    $attributes['src'] = file_create_url($variables['path']);
  }

  foreach (array('width', 'height', 'alt', 'title') as $key) {

    if (isset($variables[$key])) {
      $attributes[$key] = $variables[$key];
    }
  }

  return '<img' . drupal_attributes($attributes) . ' />';
}
1
  • Great solution, works a treat
    – Clive
    Commented Jul 13, 2012 at 8:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.