You are here

function image_imageinfo_cache_get_info in Imageinfo Cache 7.3

Get details about an image.

Parameters

object $image: An image object.

Return value

mixed FALSE, if the file could not be found or is not an image. Otherwise, a keyed array containing information about the image:

  • width: Width in pixels.
  • height: Height in pixels.
  • extension: Commonly used file extension for the image.
  • mime_type: MIME type ('image/jpeg', 'image/gif', 'image/png').
  • file_size: Image size in bytes.

See also

image_get_info()

File

./imageinfo_cache.toolkit.inc, line 25
Imageinfo Cache module. Pseudo image toolkit functions.

Code

function image_imageinfo_cache_get_info($image) {

  // If not using the cache, bypass it.
  if (!variable_get('imageinfo_cache_getimagesize', IMAGEINFO_CACHE_GETIMAGESIZE)) {
    return _image_imageinfo_cache_get_details($image);
  }
  if (strpos($image->source, '/styles/') === FALSE) {
    $cid = 'ORIGINAL-FILE:';
  }
  else {
    $matches = array();
    preg_match('/.*\\/styles\\/(.+?)\\/.*/', $image->source, $matches);
    $cid = $matches[1] . ':';
  }
  $details = array();
  $cid .= drupal_hash_base64($image->source);

  // Try the static cache first.
  $static_cache =& drupal_static(__FUNCTION__);
  if (isset($static_cache[$cid])) {
    return $static_cache[$cid];
  }

  // Try the cache back end.
  $cache = cache_get($cid, 'cache_imageinfo');
  if (!empty($cache) && !empty($cache->data)) {
    return $cache->data;
  }

  // Try the database next.
  if (db_table_exists('file_metadata')) {
    $results = db_query("\n      SELECT\n        file_metadata_width.value AS width,\n        file_metadata_height.value AS height,\n        SUBSTRING_INDEX(file_managed.uri, '.', -1) AS extension,\n        file_managed.filemime AS mime_type,\n        file_managed.filesize AS file_size\n      FROM {file_managed} AS file_managed\n      INNER JOIN {file_metadata} AS file_metadata_width\n        ON file_metadata_width.fid = file_managed.fid\n        AND file_metadata_width.name = 'width'\n      INNER JOIN {file_metadata} AS file_metadata_height\n        ON file_metadata_height.fid = file_managed.fid\n        AND file_metadata_height.name = 'height'\n      WHERE file_managed.uri = :uri\n    ", array(
      ':uri' => $image->source,
    ))
      ->fetchAll();
    if (!empty($results) && !empty($results[0])) {
      $details['width'] = (int) unserialize($results[0]->width);
      $details['height'] = (int) unserialize($results[0]->height);
      $details += (array) $results[0];
    }
  }

  // Change toolkit back to the original value and get the info from the file
  // system.
  if (empty($details)) {
    $details = _image_imageinfo_cache_get_details($image);
  }

  // Write to the cache.
  if (!empty($details) && empty($cache->data)) {

    // Write to the static cache first.
    $static_cache[$cid] = $details;

    // CACHE_PERMANENT isn't good here. Use 2 weeks from now + 0-45 days.
    // The random 0 to 45 day addition is to prevent a cache stampede.
    cache_set($cid, $details, 'cache_imageinfo', round(REQUEST_TIME + 1209600 + mt_rand(0, 3888000), -3));
  }
  return $details;
}