function image_scale

Scales an image while maintaining aspect ratio.

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

Parameters

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

$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 scaled up. This generally results in a low quality image.

Return value

TRUE on success, FALSE on failure.

See also

image_dimensions_scale()

image_load()

image_scale_and_crop()

Related topics

3 calls to image_scale()
file_validate_image_resolution in drupal/core/modules/file/file.module
Verifies that image dimensions are within the specified maximum and minimum.
image_scale_effect in drupal/core/modules/image/image.effects.inc
Image effect callback; Scale an image resource.
ToolkitTest::testScale in drupal/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitTest.php
Test the image_scale() function.
6 string references to 'image_scale'
image.style.large.yml in drupal/core/modules/image/config/image.style.large.yml
drupal/core/modules/image/config/image.style.large.yml
image.style.medium.yml in drupal/core/modules/image/config/image.style.medium.yml
drupal/core/modules/image/config/image.style.medium.yml
image.style.thumbnail.yml in drupal/core/modules/image/config/image.style.thumbnail.yml
drupal/core/modules/image/config/image.style.thumbnail.yml
ImageAdminStylesTest::testEditEffect in drupal/core/modules/image/lib/Drupal/image/Tests/ImageAdminStylesTest.php
Verifies that editing an image effect does not cause it to be duplicated.
ImageDimensionsTest::testImageDimensions in drupal/core/modules/image/lib/Drupal/image/Tests/ImageDimensionsTest.php
Test styled image dimensions cumulatively.

... See full list

File

drupal/core/includes/image.inc, line 252
API for manipulating images.

Code

function image_scale(stdClass $image, $width = NULL, $height = NULL, $upscale = FALSE) {
  $dimensions = $image->info;

  // Scale the dimensions - if they don't change then just return success.
  if (!image_dimensions_scale($dimensions, $width, $height, $upscale)) {
    return TRUE;
  }
  return image_resize($image, $dimensions['width'], $dimensions['height']);
}