Same name and namespace in other branches
  1. 4.6.x includes/image.inc \image_gd_crop()
  2. 4.7.x includes/image.inc \image_gd_crop()
  3. 5.x includes/image.inc \image_gd_crop()
  4. 6.x includes/image.gd.inc \image_gd_crop()

Crop an image using the GD toolkit.

Parameters

$image: An image object. The $image->resource, $image->info['width'], and $image->info['height'] values will be modified by this call.

$x: The starting x offset at which to start the crop, in pixels.

$y: The starting y offset at which to start the crop, in pixels.

$width: The width of the cropped area, in pixels.

$height: The height of the cropped area, in pixels.

Return value

TRUE or FALSE, based on success.

See also

image_crop()

Related topics

File

modules/system/image.gd.inc, line 206
GD2 toolkit for image manipulation within Drupal.

Code

function image_gd_crop(stdClass $image, $x, $y, $width, $height) {
  $width = (int) $width;
  $height = (int) $height;
  $res = image_gd_create_tmp($image, $width, $height);
  if (!imagecopyresampled($res, $image->resource, 0, 0, (int) $x, (int) $y, $width, $height, $width, $height)) {
    return FALSE;
  }

  // Destroy the original image and return the modified image.
  imagedestroy($image->resource);
  $image->resource = $res;
  $image->info['width'] = $width;
  $image->info['height'] = $height;
  return TRUE;
}