You are here

function imagecache_external_fetch in Imagecache External 7.2

Same name and namespace in other branches
  1. 8 imagecache_external.module \imagecache_external_fetch()
  2. 6.2 imagecache_external.module \imagecache_external_fetch()
  3. 7 imagecache_external.module \imagecache_external_fetch()

Api function to fetch a url.

Parameters

string $url: The url to fetch.

string $cachepath: The directory where to save the images within the files directory.

1 call to imagecache_external_fetch()
imagecache_external_generate_path in ./imagecache_external.module
Util to generate a path to an image.

File

./imagecache_external.module, line 336
Allows the usage of Image Styles on external images.

Code

function imagecache_external_fetch($url, $cachepath) {

  // Validate the image URL against the whitelist.
  if (imagecache_external_validate_host($url) === FALSE) {
    return FALSE;
  }
  $result = drupal_http_request($url);
  $code = floor($result->code / 100) * 100;
  $types = imagecache_external_allowed_mimetypes();

  // If content-type not set, use the default 'application/octet-stream'.
  $response_mimetype = !empty($result->headers['content-type']) ? strtolower($result->headers['content-type']) : 'application/octet-stream';

  // Add extension to the cached file to allow file_entity to use it for
  // mimetype identification.
  $cachepath_ext = pathinfo($cachepath, PATHINFO_EXTENSION);
  if (!$cachepath_ext && variable_get('imagecache_default_extension', '.jpg') != '') {
    require_once DRUPAL_ROOT . '/includes/file.mimetypes.inc';
    $map = file_mimetype_mapping();
    $mimetype_id = array_search($response_mimetype, $map['mimetypes']);
    if ($mimetype_id !== FALSE) {
      $cachepath_ext = array_search($mimetype_id, $map['extensions']);
      $cachepath .= $cachepath_ext ? '.' . $cachepath_ext : '';
    }
  }
  $fallback_image_fid = variable_get('imagecache_fallback_image', '');

  // Explode content-type to handle mimetypes with more than one
  // property (eg. image/jpeg;charset=UTF-8).
  $content_type_array = explode(';', $response_mimetype);
  $content_type_allowed = FALSE;
  foreach ($content_type_array as $content_type) {
    if (in_array(strtolower($content_type), $types)) {
      $content_type_allowed = TRUE;
    }
  }
  if (!empty($result->data) && $code != 400 && $code != 500 && $content_type_allowed) {
    if (variable_get('imagecache_external_management', 'unmanaged') == 'unmanaged') {
      return file_unmanaged_save_data($result->data, $cachepath, FILE_EXISTS_REPLACE);
    }
    else {
      $file = file_save_data($result->data, $cachepath, FILE_EXISTS_REPLACE);
      return $file->uri;
    }
  }
  elseif (!empty($fallback_image_fid)) {
    $fallback_image = file_load($fallback_image_fid);
    return $fallback_image->uri;
  }
  else {

    // If we are unsuccessful then log a message in watchdog.
    watchdog('imagecache_external', 'The image %url could not be retrieved', array(
      '%url' => $url,
    ));
    return FALSE;
  }
}