2

I need to rewrite all image urls inside certain node types into custom-defined urls. For example, this image:

<img src="/sites/default/files/u22/pretty.jpg" width="678" height="1024" alt="pretty.jpg" />

need to be rewritten to:

<img src="http://files.mysite.com/sites/default/files/u22/pretty.jpg" width="678" height="1024" alt="pretty.jpg" />

And I need to do that for all of the user-generated images. Obviously, the goal is to be able to off-load all images to a secondary file server. How can I do that?

Thanks

  • for what version of drupa, D6 or D7 ? – tenken Mar 10 '13 at 2:53
  • Sorry, it is for a Drupal 6 site. – Levox Mar 10 '13 at 4:39
3

Take a look at the cdn module. It provides mapping methods for that kind of use.

  • I mapped my files to files/mysite.com and added 'files.mysite.com|.gif .jpg' to CDN configuration and it worked like a charm. Thanks – Levox Mar 10 '13 at 7:22
3

ok if you're looking at drupal 6 look into custom_url_rewrite_outbound(). For D7, you can use hook_url_outbound_alter().

Some example code I have sitting around in a module that takes media resources of a Drupal site and points the generated urls to another server to serve media content, we used Nginx. Keep in mind that if you're just sending back to same server on another port .... you're just feeding yourself more traffic -- which isn't as helpful as a true CDN.

 /*
 *
 * A mechanism in Drupal to rewrite outbound urls to point to another domain.
 * This can be used to serve static content from a dedicated server, aka a CDN.
 *
 * Taken 100% from the following url. I am not smart enough to pull off this
 * magic :).
 *
 * http://drupal4hu.com/node/260
 *
 * In the below code $conf['dew_cdn_domain'] is a custom value in
 * settings.php that points to like "http://yourotherserver.com" to serve
 * media files.
 */


/**
 * Implementation of hook_file_url_alter()
 *
 * This inspects and transforms file urls to our remote CDN host.
 */
function dew_cdn_file_url_alter(&$uri) {
  global $conf;
  static $drupal_static_fast;
  // A drupal_static_reset() would clear this and then we need to rerun variable_get.
  if (!isset($drupal_static_fast['dew_cdn_domain'])) {
    $drupal_static_fast['dew_cdn_domain'] = &drupal_static(__FUNCTION__);
    $drupal_static_fast['dew_cdn_domain'] = $conf['dew_cdn_domain'];
  }
  $cdn_domain = &$drupal_static_fast['dew_cdn_domain'];
  if ($cdn_domain && strpos($uri, ':') === FALSE) {
    $uri = $cdn_domain . $uri;
  }
}


/**
 * Implementation of hook_stream_wrappers_alter()
 */
function dew_cdn_stream_wrappers_alter(&$wrappers) {
  $wrappers['public']['class'] = 'DrupalCDNPublicStreamWrapper';
}


/**
 * Any files that would go the public stream public:// get rewritten to another
 * base domain.
 */
class DrupalCDNPublicStreamWrapper extends DrupalPublicStreamWrapper {
  function getExternalUrl() {
    global $conf;
    $path = str_replace('\\', '/', $this->getTarget());
    return $conf['dew_cdn_domain'] . self::getDirectoryPath() . '/' . drupal_encode_path($path);
  }
}
  • As I'm using Pressflow 6, I think this code is still applicable? However I'm not a Drupal developer so appreciate if you explain how to implement the code. – Levox Mar 10 '13 at 4:54
  • then look at the CDN module below by @Serpiente. Less learn the language they use and you should be able to configure the module to do what you want. but in D7 its like whatever ... the 20 lines above ... – tenken Mar 10 '13 at 5:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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