Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Protocol relative image paths in Drupal 7
* Implementation of hook_preprocess_image().
* Make images that use a full url be protocol relative.
function custom_preprocess_image(&$variables) {
// If the image URL starts with a protocol remove it and use a
// relative protocol.
$scheme = file_uri_scheme($variables['path']);
$protocols = array('http', 'https');
if ($scheme && in_array($scheme, $protocols)) {
$variables['path'] = '//' . file_uri_target($variables['path']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment