You are here

function image_get_sizes in Image 7

Same name and namespace in other branches
  1. 5.2 image.module \image_get_sizes()
  2. 5 image.module \image_get_sizes()
  3. 6 image.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.

16 calls to image_get_sizes()
ImageTestCase::setUp in tests/image.test
Sets up a Drupal site for running functional and integration tests.
image_admin_settings in ./image.admin.inc
Menu callback; Form builder function for image settings.
image_admin_settings_submit in ./image.admin.inc
Form submit handler for image admin settings form.
image_gallery_handler_field_gallery_cover_thumbnail::options_form in contrib/image_gallery/views/image_gallery_handler_field_gallery_cover_thumbnail.inc
Extends the field's basic options with more image specific options.
image_handler_argument_image_size::title in views/image_handler_argument_image_size.inc
Get the human-readable label for the image size.

... See full list

File

./image_legacy.module, line 93

Code

function image_get_sizes($size = NULL, $aspect_ratio = NULL) {
  $defaults = array(
    IMAGE_ORIGINAL => array(
      'width' => '',
      'height' => '',
      'label' => t('Original'),
      'operation' => 'scale',
      'link' => IMAGE_LINK_SHOWN,
    ),
    IMAGE_THUMBNAIL => array(
      'width' => 100,
      'height' => 100,
      'label' => t('Thumbnail'),
      'operation' => 'scale',
      'link' => IMAGE_LINK_SHOWN,
    ),
    IMAGE_PREVIEW => array(
      'width' => 640,
      'height' => 640,
      'label' => t('Preview'),
      'operation' => 'scale',
      '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;
}