You are here

function image_scale in Drupal 4

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

Parameters

$source The filepath of the source image:

$destination The file path of the destination image:

$width The target width:

$height The target height:

Return value

True or false, based on success

2 calls to image_scale()
user_validate_picture in modules/user.module
_upload_image in modules/upload.module
Check an upload, if it is an image, make sure it fits within the maximum dimensions allowed.

File

includes/image.inc, line 124

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,
  ));
}