You are here

function imageapi_image_scale in ImageAPI 6

Same name and namespace in other branches
  1. 5 imageapi.module \imageapi_image_scale()

Scales an image to the given width and height while maintaining aspect ratio.

The resulting image can be smaller for one or both target dimensions.

Parameters

$image: An image object returned by imageapi_image_open().

$width: The target width, in pixels. This value is omitted then the scaling will based only on the height value.

$height: The target height, in pixels. This value is omitted then the scaling will based only on the width value.

$upscale: Boolean indicating that files smaller than the dimensions will be scalled up. This generally results in a low quality image.

Return value

TRUE or FALSE, based on success.

File

./imageapi.module, line 220
An ImageAPI supporting additional image plugins as modules. Images are treated as objects, and images are not written per manipulation as Drupal's core image handling works.

Code

function imageapi_image_scale(&$image, $width = NULL, $height = NULL, $upscale = FALSE) {
  $aspect = $image->info['height'] / $image->info['width'];
  if ($upscale) {

    // Set width/height according to aspect ratio if either is empty.
    $width = !empty($width) ? $width : $height / $aspect;
    $height = !empty($height) ? $height : $width / $aspect;
  }
  else {

    // Set impossibly large values if the width and height aren't set.
    $width = !empty($width) ? $width : 9999999;
    $height = !empty($height) ? $height : 9999999;

    // Don't scale up.
    if (round($width) >= $image->info['width'] && round($height) >= $image->info['height']) {
      return TRUE;
    }
  }
  if ($aspect < $height / $width) {
    $height = $width * $aspect;
  }
  else {
    $width = $height / $aspect;
  }
  return imageapi_image_resize($image, $width, $height);
}