Skip to content

Kirby 5.0.4

$file->crop()

Crops the image by the given width and height

$file->crop(int $width, int|null $height = null, $options = null): Kirby\Cms\FileVersion|Kirby\Cms\File|Kirby\Filesystem\Asset

Parameters

Name Type Default
$widthrequired int no default value
$height intornull null
$options mixed null

Return types

Kirby\Cms\FileVersionorKirby\Cms\FileorKirby\Filesystem\Asset

This method modifies the existing $file object it is applied to and returns it again.

Parent class

Kirby\Cms\File

Crop positions

If a focal point is set, this focal point will automatically be used when cropping an image. If you use one of the following focal points when cropping, this will overwrite the focal point set in the file metadata.

You can define from which side or corner a file should be cropped. The following crop options are available:

  • top left
  • top
  • top right
  • left
  • center
  • right
  • bottom left
  • bottom
  • bottom right

This is how to use the crop positions in your code:

// quick and simple
$image->crop(100, 200, 'top right');

// more fine grained control
$image->crop(100, 200, [
'quality' => 70,
'crop' => 'left'
]);

Examples

if ($image = $page->image()) {
    // crop into a square of 300 x 300
    echo $image->crop(300);

    // crop by height as well
    echo $image->crop(300, 200);

    // adjust the quality
    echo $image->crop(300, 200, 70);
}