1. CCK version: 6.x-2.8
2. imagefield version: 6.x-3.7

I created an image field for a custom content type, then create content > upload image.

I am having an issues with these IMAGES in Drupal pages. The problem is, they are being referenced as ABSOLUTE URL’s instead of RELATIVE URL’s.

For example from firebug:

<img  class="imagefield imagefield-field_emblem" width="69" height="58" alt="" src="http://mysite.com/sites/default/files/Menominee_sm_0.png?1283886420"/>

As you can see, this is ABSOLUTE. I need to figure out how to make Drupal generate RELATIVE URLs. In other words, we need the page to look like this:

<img  class="imagefield imagefield-field_emblem" width="69" height="58" alt="" src="/sites/default/files/Menominee_sm_0.png?1283886420"/>

Comments

quicksketch’s picture

Category: bug » support

I'm not sure why it's imperative to make them relative instead of absolute, but you can do this by overriding theme_imagefield_image() for ImageField images and theme_imagecache() if using ImageCache.

mitchelljj’s picture

I *believe* I have fixed this … it’s working locally (as in, I’m getting relative URLS for uploaded images now). I’d like to know if changing the below will break anything else within Drupal.

Here’s what I did:

In “file.inc” (in includes folder), I took the argument for creating paths for PRIVATE uploads and replaced the argument for creating PUBLIC uploads with the same argument:

WAS:

switch (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC)) {

case FILE_DOWNLOADS_PUBLIC:

return $GLOBALS['base_url'] .'/'. file_directory_path() .'/'. str_replace('\\', '/', $path);

case FILE_DOWNLOADS_PRIVATE:

return url('system/files/'. $path, array('absolute' => FALSE));

}

}

NOW IS:

switch (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC)) {

case FILE_DOWNLOADS_PUBLIC:

return url('system/files/'. $path, array('absolute' => FALSE));

case FILE_DOWNLOADS_PRIVATE:

return url('system/files/'. $path, array('absolute' => FALSE));

}

}

Thus far nothing has broken – I’ve uploaded new images, created new content types and added image fields and uploaded new content. In firebug I’m getting back relative paths.

quicksketch’s picture

I don't think hacking file.inc is a good way to go, as you'll need to remember to modify those files every time you update Drupal core for security updates. I'd really suggest theming as the way to fix this problem, which would keep the changes in your theme instead of modifying Drupal core.

quicksketch’s picture

Status: Active » Closed (fixed)
PMorris’s picture

If you just want to change imagefield you override it it template.php which is the recommended way.

function garlandsub_imagefield_image($file, $alt = '', $title = '', $attributes = NULL, $getsize = TRUE) {
  $file = (array) $file;

  if ($getsize) {
    // Use cached width and height if available.
    if (!empty($file['data']['width']) && !empty($file['data']['height'])) {
      $attributes['width']  = $file['data']['width'];
      $attributes['height'] = $file['data']['height'];
    }
    // Otherwise pull the width and height from the file.
    elseif (list($width, $height, $type, $image_attributes) = @getimagesize($file['filepath'])) {
      $attributes['width'] = $width;
      $attributes['height'] = $height;
    }
  }

  if (!empty($title)) {
    $attributes['title'] = $title;
  }

  // Alt text should be added even if it is an empty string.
  $attributes['alt'] = $alt;

  // Add a timestamp to the URL to ensure it is immediately updated after editing.
  $query_string = '';
  if (isset($file['timestamp'])) {
    $query_character = (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PRIVATE && variable_get('clean_url', '0') == '0') ? '&' : '?';
    $query_string = $query_character . $file['timestamp'];
  }

  // Encode the path so that unusual characters are printed correctly.
  $path = field_file_urlencode_path($file['filepath']);

  // Construct the URL.
  $url = $path . $query_string;
  $attributes['src'] = $url;
  $attributes = drupal_attributes($attributes);
  return '<img '. $attributes .' />';
}

all i changed was the line with "$url = file_create_url($path) . $query_string;" to " $url = $path . $query_string;" to get rid of the http://

I didn't test this extensively so beware. But it appears to work with a quick test with just an imagefield's path. garlandsub was the name of my test subtheme so change that to whatever theme name you have and put the code in template.php