Drupal Hack: Getting Pages and Images from Different Servers

One of the issues with running the OTIH system over a distributed network of static and mobile devices is the lower bandwidth available at the mobile end.  It is therefore important to figure out the best ways to make use of local file storage on an ‘Out There’ server, whilst maintaining an up to date link back to the ‘In Here’ site.

A potential bandwidth hog would be the need to access images from the In Here server for display in the pages of our Drupal-based CMS on Out There devices. Given that we will sync files between the OT and IH servers, a much better solution is to serve the HTML from the IH server for users at both locations, but to grab the images locally at each end.

Drupal doesn’t support this kind of thing as standard, but after wading through a fair amount of the core code, we found that it actually requires only a few additional lines to get it to work.

In our site, we are using two modules to display images. One is ImageField, which is responsible for the CCK image type and produces the full sized images, the other is ImageCache, used to produce thumbnails or other modified sizes.

In nearly all cases, path variables in Drupal are relative, this makes sense as you can then easily move the site to various servers, but it makes it hard to modify the full path on the fly.  To do this and get it rendered correctly we need to modify the theme function of both our modules: Function theme_imagefield_image in  ImageField.module, and function theme_imagecache in the ImageCache.module. What we are aiming to do is change the value of the url that is returned from these functions, as this will then be rendered by the site.

Firsly, we find the line in the function that generates the standard url, in the ImageField module this is: $url = file_create_url($path) . $query_string; , and in ImageCache, it is:   $imagecache_url = imagecache_create_url($presetname, $path);

After this, we insert our code. We are going to decide which server to use based on the current users’ membership of a group. If they are members of ‘Out There’, we will modify the url and thus the images will be served locally. We also want to ignore the admin user, they will get images from the standard server, even though they are technically a member of the ‘Out There’ group.

The following lines get or set the variables we need. $user identifies the current user, $base_url gives us the current path of the server (without the relative part) and we also identify the gid of the group we are checking for in our conditional statement.

global $user;
global $base_url;
$out_there_gid = 2;

Next is our conditional statement, we know that User ID 1 = admin, so we want to ignore that. We then use the og_is_group_member function to check if the current user is a member of Out There:

if (($user->uid != 1) &&(og_is_group_member($out_there_gid, NULL, $user->uid))) {

We then take the $url variable, and replace base_url with the out there server address (In ImageCache this variable is called $imagecache_url)

$url = str_replace($base_url, “http://outthereserver.org“, $url);
};

After our inserted code, the existing code from the function then takes $url and returns a string of the HTML output for this image. e.g.:

$attributes['src'] = $url;
$attributes = drupal_attributes($attributes);
return ‘<img ‘. $attributes .’ />’;

Thats it. A few lines changed and hopefully a whole lot of unnecessary data transfer saved!

This entry was posted in Development and tagged , , . Bookmark the permalink.

One Response to Drupal Hack: Getting Pages and Images from Different Servers

  1. Moses says:

    Hi. I’m a student of UNN.As far as I,m concerned technology is one of my best interest.I really love what your website is doing.Keep it up! Thanks a lot.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>