You are here

function image_scale in Drupal 6

Same name and namespace in other branches
  1. 4 includes/image.inc \image_scale()
  2. 5 includes/image.inc \image_scale()
  3. 7 includes/image.inc \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

$source: The file path of the source image.

$destination: The file path of the destination image.

$width: The target width, in pixels.

$height: The target height, in pixels.

Return value

TRUE or FALSE, based on success.

Related topics

1 call to image_scale()
file_validate_image_resolution in includes/file.inc
If the file is an image verify that its dimensions are within the specified maximum and minimum dimensions. Non-image files will be ignored.

File

includes/image.inc, line 188
API for manipulating images.

Code

function image_scale($source, $destination, $width, $height) {
  $info = image_get_info($source);

  // Don't scale up.
  if ($width >= $info['width'] && $height >= $info['height']) {
    return FALSE;
  }
  $aspect = $info['height'] / $info['width'];
  if ($aspect < $height / $width) {
    $width = (int) min($width, $info['width']);
    $height = (int) round($width * $aspect);
  }
  else {
    $height = (int) min($height, $info['height']);
    $width = (int) round($height / $aspect);
  }
  return image_toolkit_invoke('resize', array(
    $source,
    $destination,
    $width,
    $height,
  ));
}