function picture_picture_image_uri in Picture 7
Same name and namespace in other branches
- 7.2 picture.module \picture_picture_image_uri()
Implements hook_picture_image_uri().
File
- ./
picture.module, line 1353 - Picture formatter.
Code
function picture_picture_image_uri($src) {
global $base_path;
$uri = '';
// Prepare the src by removing http:// or https://.
$src = parse_url($src, PHP_URL_PATH);
// Remove leading or trailing slashes.
$src = trim($src, '/');
// List all visible stream wrappers. Make sure they're also local since the
// getDirectoryPath method exists only for classes that extend
// DrupalLocalStreamWrapper.
$local_visible_stream_wrappers = array_intersect_key(file_get_stream_wrappers(STREAM_WRAPPERS_LOCAL), file_get_stream_wrappers(STREAM_WRAPPERS_VISIBLE));
$needles = array();
$matches = array();
foreach ($local_visible_stream_wrappers as $scheme => $data) {
$class = file_stream_wrapper_get_class($scheme);
$stream_wrapper = new $class();
// Trim leading or trailing slashes since the Directory could be root
// relative.
$needles[$scheme] = trim($base_path . $stream_wrapper
->getDirectoryPath(), '/');
// Check whether the file stream directory is at the beginning of
// the image src. Use === since strpos could return false.
if (!empty($needles[$scheme]) && strpos($src, $needles[$scheme]) === 0) {
$matches[$scheme] = $needles[$scheme];
}
}
// No file stream wrapper is starting with the image src.
// So it's not a public file.
if (empty($matches)) {
// Check for managed/private file with relative path like 'system/files/...'.
$src_exploded = explode('/', $src);
if ($src_exploded[0] == 'system' && $src_exploded[1] == 'files') {
// Managed/private file recognized.
// Check for image style derivatives.
if ($src_exploded[2] == 'styles') {
// Image style recognized.
$unwanted_part = 'system/files/styles/' . $src_exploded[3] . '/private/';
$uri = str_replace($unwanted_part, 'private://', $src);
$uri = file_stream_wrapper_uri_normalize($uri);
return urldecode($uri);
}
else {
// No image style recognized; must be an original.
$unwanted_part = 'system/files/';
$uri = str_replace($unwanted_part, 'private://', $src);
$uri = file_stream_wrapper_uri_normalize($uri);
return urldecode($uri);
}
// It's not a managed/private file.
}
else {
// Can't figure out the Drupal uri.
return FALSE;
}
}
// If one file scheme directory is a subdirectory of another file
// scheme directory, choose the longer one. This issue is possible with
// the following scenario:
// public file dir: /sites/default/files/
// private file dir: /sites/default/files/private/
// image src: /sites/default/files/private/the-image.jpg
// In this example, the intended scheme would be 'private'.
// Find the length of each matching directory path.
$lengths = array_map('strlen', $matches);
// Determine the key of the longest one.
$the_scheme = array_search(max($lengths), $lengths);
// Construct the Drupal uri.
$uri = $the_scheme . '://' . str_replace($matches[$the_scheme], '', $src);
return $uri;
}