Crop (Abstract Base Class)

The Crop class is the foundation of all cropping strategies in the library. It provides common functionality and defines the interface that all concrete cropping implementations must follow.

Class Overview

1
abstract class drzippie\crop\Crop

This abstract class cannot be instantiated directly. Instead, use one of the concrete implementations:

Constructor

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

Creates a new cropping instance with optional image initialization.

Parameters:

Examples:

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

// With Imagick object
$imagick = new Imagick('image.jpg');
$crop = new CropCenter($imagick);

// Empty instance
$crop = new CropCenter();

Core Methods

resizeAndCrop()

1
public function resizeAndCrop(int $targetWidth, int $targetHeight): Imagick

The main method that performs the resize and crop operation.

Parameters:

Returns: Imagick - The processed image

Throws: RuntimeException if no image is set

Example:

1
2
3
$crop = new CropCenter('image.jpg');
$result = $crop->resizeAndCrop(300, 200);
$result->writeImage('output.jpg');

setImage()

1
public function setImage(Imagick $image): self

Sets the image to be processed.

Parameters:

Returns: self for method chaining

setFilter()

1
public function setFilter(int $filter): self

Sets the resize filter to use during processing.

Parameters:

Common Filters:

Returns: self for method chaining

setBlur()

1
public function setBlur(float $blur): self

Sets the blur factor for resizing operations.

Parameters:

Returns: self for method chaining

setAutoOrient()

1
public function setAutoOrient(bool $autoOrient): self

Enables or disables automatic orientation based on EXIF data.

Parameters:

Returns: self for method chaining

Getter Methods

getFilter()

1
public function getFilter(): int

Returns the current resize filter setting.

getBlur()

1
public function getBlur(): float

Returns the current blur factor.

getAutoOrient()

1
public function getAutoOrient(): bool

Returns the current auto-orientation setting.

Utility Methods

Profiling

1
2
public static function start(): void
public static function mark(): string

Static methods for performance profiling:

Example:

1
2
3
4
Crop::start();
$crop = new CropCenter('image.jpg');
$result = $crop->resizeAndCrop(300, 200);
echo "Processing time: " . Crop::mark();

Entropy Calculation

1
public function getEntropyFromArray(array $histogram): float

Calculates entropy from a histogram array. Used internally by entropy-based cropping strategies.

Parameters:

Returns: float - Calculated entropy value

Abstract Methods

getSpecialOffset()

1
2
3
4
5
abstract protected function getSpecialOffset(
    Imagick $original, 
    int $targetWidth, 
    int $targetHeight
): array

Abstract method that must be implemented by concrete classes to define their specific cropping logic.

Parameters:

Returns: array - Array containing x and y offset coordinates

Method Chaining

All setter methods return $this, allowing for fluent interface usage:

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

Error Handling

The class throws RuntimeException when:

Performance Considerations

See Also