Cropping Strategies

The Crop library provides three intelligent cropping algorithms, each designed for different use cases and quality requirements.

CropCenter

Simple center-based cropping - Fast and reliable

How it works

CropCenter uses the most basic cropping technique:

  1. Find the exact center of the image
  2. Trim any edges that exceed the target width and height
  3. Ensure the crop is perfectly centered

When to use

Example

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

$crop = new CropCenter('portrait.jpg');
$result = $crop->resizeAndCrop(200, 200);
$result->writeImage('profile.jpg');

Performance


CropEntropy

Edge-detection based cropping - Intelligent content preservation

How it works

CropEntropy finds the position with the most “energy” or “entropy” in the image:

  1. Convert image to grayscale
  2. Apply edge detection filter to highlight edges
  3. Find the area with highest entropy (most edges/details)
  4. Position crop to preserve this high-energy area

When to use

Example

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

$crop = new CropEntropy('landscape.jpg');
$result = $crop->resizeAndCrop(400, 300);
$result->writeImage('cropped_landscape.jpg');

Technical Details

The entropy calculation uses:

Performance


CropBalanced

Weighted center of interest - Balanced composition

How it works

CropBalanced is a refined version of CropEntropy that provides more balanced results:

  1. Divide image into four equal quadrants
  2. Find the most energetic point in each quadrant
  3. Calculate weighted mean interest point across all quadrants
  4. Position crop to balance composition

When to use

Example

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

$crop = new CropBalanced('mixed_content.jpg');
$result = $crop->resizeAndCrop(300, 200);
$result->writeImage('balanced_crop.jpg');

Technical Details

The balanced approach:

Performance


Strategy Comparison

Visual Comparison

Image Type CropCenter CropEntropy CropBalanced
Portraits ✅ Good ⚠️ May cut faces ✅ Good
Landscapes ⚠️ May miss focal points ✅ Excellent ✅ Good
Products ⚠️ May cut product ✅ Excellent ✅ Good
Graphics ❌ Poor ✅ Excellent ✅ Good
Mixed Content ⚠️ Variable ✅ Good ✅ Excellent

Performance Metrics

Strategy Processing Time Memory Usage Quality Score
CropCenter ~10ms Low 7/10
CropBalanced ~50ms Medium 8/10
CropEntropy ~100ms High 9/10

Times are approximate for a 1920x1080 image

Algorithm Details

CropCenter Algorithm

1
2
3
4
5
6
function getCenterOffset($image, $targetWidth, $targetHeight) {
    $geometry = $image->getImageGeometry();
    $x = max(0, ($geometry['width'] - $targetWidth) / 2);
    $y = max(0, ($geometry['height'] - $targetHeight) / 2);
    return ['x' => $x, 'y' => $y];
}

CropEntropy Algorithm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function getEntropyOffsets($image, $targetWidth, $targetHeight) {
    // 1. Enhance edges
    $measureImage = clone($image);
    $measureImage->edgeimage(1);
    
    // 2. Convert to grayscale
    $measureImage->modulateImage(100, 0, 100);
    
    // 3. Apply threshold
    $measureImage->blackThresholdImage("#070707");
    
    // 4. Find optimal position
    return $this->getOffsetFromEntropy($measureImage, $targetWidth, $targetHeight);
}

CropBalanced Algorithm

1
2
3
4
5
6
7
8
9
10
11
12
13
function getOffsetBalanced($targetWidth, $targetHeight) {
    // 1. Get entropy-based position
    $entropyOffset = $this->getEntropyOffsets($this->originalImage, $targetWidth, $targetHeight);
    
    // 2. Get random edge position for variation
    $randomOffset = $this->getRandomEdgeOffset($this->originalImage, $targetWidth, $targetHeight);
    
    // 3. Find highest energy point
    $energyPoint = $this->getHighestEnergyPoint($this->originalImage);
    
    // 4. Balance all factors
    return $this->balanceOffsets($entropyOffset, $randomOffset, $energyPoint, $targetWidth, $targetHeight);
}

Choosing the Right Strategy

Decision Matrix

For thumbnails and avatars:

1
$crop = new CropCenter($image); // Fast, consistent

For product catalogs:

1
$crop = new CropEntropy($image); // Preserves product details

For content management systems:

1
$crop = new CropBalanced($image); // Reliable for diverse content

For batch processing:

1
2
3
// Start with CropCenter for speed
$crop = new CropCenter($image);
// Fall back to CropBalanced for important images

Custom Strategy Selection

1
2
3
4
5
6
7
8
9
10
11
function chooseCropStrategy($imagePath, $priority = 'balanced') {
    switch ($priority) {
        case 'speed':
            return new CropCenter($imagePath);
        case 'quality':
            return new CropEntropy($imagePath);
        case 'balanced':
        default:
            return new CropBalanced($imagePath);
    }
}

Next Steps