You are here

function image_get_sizes in Image 5

Same name and namespace in other branches
  1. 5.2 image.module \image_get_sizes()
  2. 6 image.module \image_get_sizes()
  3. 7 image_legacy.module \image_get_sizes()

Helper function to return the defined sizes (or proper defaults).

Parameters

$size: An optional string to return only the image size with the specified key.

$aspect_ratio: Float value with the ratio of image height / width. If a size has only one dimension provided this will be used to compute the other.

Return value

An associative array with width, height, and label fields for the size. If a $size parameter was specified and it cannot be found FALSE will be returned.

14 calls to image_get_sizes()
image_admin_settings in ./image.module
Admin settings callback.
image_attach_block in contrib/image_attach/image_attach.module
Implementation of hook_block().
image_attach_form_alter in contrib/image_attach/image_attach.module
implementation of hook_form_alter()
image_create_node_from in ./image.module
Function to other modules to use to create image nodes.
image_form in ./image.module
Implementation of hook_form

... See full list

File

./image.module, line 976

Code

function image_get_sizes($size = NULL, $aspect_ratio = NULL) {
  $defaults = array(
    IMAGE_ORIGINAL => array(
      'width' => '',
      'height' => '',
      'label' => t('Original'),
      'link' => IMAGE_LINK_SHOWN,
    ),
    IMAGE_THUMBNAIL => array(
      'width' => 100,
      'height' => 100,
      'label' => t('Thumbnail'),
      'link' => IMAGE_LINK_SHOWN,
    ),
    IMAGE_PREVIEW => array(
      'width' => 640,
      'height' => 640,
      'label' => t('Preview'),
      'link' => IMAGE_LINK_SHOWN,
    ),
  );
  $sizes = array();
  foreach (variable_get('image_sizes', $defaults) as $key => $val) {

    // Only return sizes with a label.
    if (!empty($val['label'])) {

      // For a size with only one dimension specified, compute the other
      // dimension based on an aspect ratio.
      if ($aspect_ratio && (empty($val['width']) || empty($val['height']))) {
        if (empty($val['height']) && !empty($val['width'])) {
          $val['height'] = (int) round($val['width'] * $aspect_ratio);
        }
        elseif (empty($val['width']) && !empty($val['height'])) {
          $val['width'] = (int) round($val['height'] / $aspect_ratio);
        }
      }
      $sizes[$key] = $val;
    }
  }

  // If they requested a specific size return only that.
  if (isset($size)) {

    // Only return an array if it's available.
    return isset($sizes[$size]) ? $sizes[$size] : FALSE;
  }
  return $sizes;
}