function image_crop

Crops an image to a rectangle specified by the given dimensions.

Parameters

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

int $x: The top left coordinate, in pixels, of the crop area (x axis value).

int $y: The top left coordinate, in pixels, of the crop area (y axis value).

int $width: The target width, in pixels.

int $height: The target height, in pixels.

Return value

bool TRUE on success, FALSE on failure.

See also

image_load()

image_scale_and_crop()

\Drupal\system\Plugin\ImageToolkitInterface::crop()

Related topics

3 calls to image_crop()
image_crop_effect in drupal/core/modules/image/image.effects.inc
Image effect callback; Crop an image resource.
image_scale_and_crop in drupal/core/includes/image.inc
Scales an image to the exact width and height given.
ToolkitTest::testCrop in drupal/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitTest.php
Test the image_crop() function.
1 string reference to 'image_crop'
ImageDimensionsTest::testImageDimensions in drupal/core/modules/image/lib/Drupal/image/Tests/ImageDimensionsTest.php
Test styled image dimensions cumulatively.

File

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

Code

function image_crop($image, $x, $y, $width, $height) {
  $aspect = $image->info['height'] / $image->info['width'];
  if (empty($height)) {
    $height = $width / $aspect;
  }
  if (empty($width)) {
    $width = $height * $aspect;
  }
  $width = (int) round($width);
  $height = (int) round($height);
  return $image->toolkit
    ->crop($image, $x, $y, $width, $height);
}