CropCenter

The CropCenter class implements the simplest and fastest cropping strategy by centering the crop area on the image.

Class Overview

1
class drzippie\crop\CropCenter extends drzippie\crop\Crop

This class provides basic center-based cropping functionality. It’s the fastest option and ideal for:

Algorithm

The CropCenter algorithm works as follows:

  1. Find the center of the original image
  2. Calculate crop area centered on the image center
  3. Trim edges that exceed the target dimensions
  4. Resize to final dimensions

This approach is:

Constructor

1
public function __construct(string|Imagick|null $image = null)

Creates a new CropCenter instance.

Parameters:

Examples:

1
2
3
4
5
6
7
8
9
10
// From file path
$crop = new CropCenter('photo.jpg');

// From Imagick object
$imagick = new Imagick('photo.jpg');
$crop = new CropCenter($imagick);

// Empty instance
$crop = new CropCenter();
$crop->setImage(new Imagick('photo.jpg'));

Basic Usage

Simple Center Crop

1
2
3
4
5
use drzippie\crop\CropCenter;

$crop = new CropCenter('landscape.jpg');
$result = $crop->resizeAndCrop(300, 200);
$result->writeImage('thumbnail.jpg');

Multiple Sizes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
use drzippie\crop\CropCenter;

$crop = new CropCenter('photo.jpg');

// Generate different sizes
$sizes = [
    'thumb' => [100, 100],
    'small' => [200, 150],
    'medium' => [400, 300],
    'large' => [800, 600]
];

foreach ($sizes as $name => [$width, $height]) {
    $result = $crop->resizeAndCrop($width, $height);
    $result->writeImage("output_{$name}.jpg");
}

Advanced Configuration

Custom Filter Settings

1
2
3
4
5
6
7
8
9
10
11
12
13
14
use drzippie\crop\CropCenter;

$crop = new CropCenter('image.jpg');

// High quality (slower)
$crop->setFilter(Imagick::FILTER_LANCZOS);

// Default quality
$crop->setFilter(Imagick::FILTER_CUBIC);

// Fast processing
$crop->setFilter(Imagick::FILTER_POINT);

$result = $crop->resizeAndCrop(300, 200);

Blur Adjustment

1
2
3
4
5
6
7
8
9
10
11
use drzippie\crop\CropCenter;

$crop = new CropCenter('image.jpg');

// Sharper result
$crop->setBlur(0.2);

// Softer result
$crop->setBlur(1.0);

$result = $crop->resizeAndCrop(300, 200);

Auto-Orientation

1
2
3
4
5
6
7
8
9
10
11
use drzippie\crop\CropCenter;

$crop = new CropCenter('photo_with_exif.jpg');

// Enable auto-orientation (default)
$crop->setAutoOrient(true);

// Disable to preserve original orientation
$crop->setAutoOrient(false);

$result = $crop->resizeAndCrop(300, 200);

Method Chaining

1
2
3
4
5
6
7
8
9
use drzippie\crop\CropCenter;

$result = (new CropCenter('image.jpg'))
    ->setFilter(Imagick::FILTER_LANCZOS)
    ->setBlur(0.8)
    ->setAutoOrient(true)
    ->resizeAndCrop(400, 300);

$result->writeImage('output.jpg');

Performance Characteristics

Speed

Memory Usage

Quality Trade-offs

Use Cases

Ideal For:

Not Ideal For:

Performance Comparison

For a 1920x1080 image:

Error Handling

1
2
3
4
5
6
7
8
9
10
11
use drzippie\crop\CropCenter;

try {
    $crop = new CropCenter('image.jpg');
    $result = $crop->resizeAndCrop(300, 200);
    $result->writeImage('output.jpg');
} catch (RuntimeException $e) {
    echo "Error: " . $e->getMessage();
} catch (ImagickException $e) {
    echo "ImageMagick error: " . $e->getMessage();
}

Examples

Thumbnail Generation

1
2
3
4
5
6
7
8
9
10
11
use drzippie\crop\CropCenter;

function generateThumbnail($inputPath, $outputPath, $size = 150) {
    $crop = new CropCenter($inputPath);
    $result = $crop->resizeAndCrop($size, $size);
    $result->writeImage($outputPath);
    return $outputPath;
}

// Usage
$thumb = generateThumbnail('photo.jpg', 'thumb.jpg', 200);

Batch Processing

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
use drzippie\crop\CropCenter;

function processBatch($inputDir, $outputDir, $width, $height) {
    $files = glob($inputDir . '/*.jpg');
    
    foreach ($files as $file) {
        $crop = new CropCenter($file);
        $result = $crop->resizeAndCrop($width, $height);
        
        $filename = basename($file);
        $result->writeImage($outputDir . '/' . $filename);
    }
}

// Process all images in directory
processBatch('input/', 'output/', 300, 200);

Implementation Details

The getSpecialOffset() method simply returns the center coordinates:

1
2
3
4
5
6
7
8
9
10
11
12
protected function getSpecialOffset(
    Imagick $original, 
    int $targetWidth, 
    int $targetHeight
): array {
    $geometry = $original->getImageGeometry();
    
    return [
        ($geometry['width'] - $targetWidth) / 2,
        ($geometry['height'] - $targetHeight) / 2
    ];
}

See Also