API Reference

Complete API documentation for the Crop library.

Classes

Crop (Abstract Base Class)

The foundation class that provides common functionality for all cropping strategies.

CropCenter

Simple center-based cropping implementation.

CropEntropy

Entropy-based cropping that preserves high-energy areas.

CropBalanced

Balanced cropping with weighted center of interest.

Quick Reference

Common Methods

All cropping classes inherit these methods from the base Crop class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Constructor
public function __construct(string|Imagick|null $image = null)

// Main cropping method
public function resizeAndCrop(int $targetWidth, int $targetHeight): Imagick

// Configuration methods
public function setImage(Imagick $image): self
public function setFilter(int $filter): self
public function setBlur(float $blur): self
public function setAutoOrient(bool $autoOrient): self

// Getters
public function getFilter(): int
public function getBlur(): float
public function getAutoOrient(): bool

// Profiling utilities
public static function start(): void
public static function mark(): string

Strategy-Specific Methods

Each cropping strategy implements:

1
2
// Protected method that defines the cropping logic
protected function getSpecialOffset(Imagick $original, int $targetWidth, int $targetHeight): array

Method Details

Constructor

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

Creates a new cropping instance.

Parameters:

Examples:

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

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

// Empty instance
$crop = new CropCenter();
$crop->setImage($imagick);

resizeAndCrop()

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

Resizes and crops the image to the specified dimensions.

Parameters:

Returns: Imagick - The cropped image

Throws: RuntimeException if no image is set

Example:

1
2
3
$crop = new CropEntropy('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

Example:

1
2
$crop = new CropBalanced();
$crop->setImage(new Imagick('image.jpg'));

setFilter()

1
public function setFilter(int $filter): self

Sets the resize filter to use.

Parameters:

Returns: self for method chaining

Common Filters:

Example:

1
2
$crop = new CropCenter('image.jpg');
$crop->setFilter(Imagick::FILTER_LANCZOS);

setBlur()

1
public function setBlur(float $blur): self

Sets the blur factor for resizing.

Parameters:

Returns: self for method chaining

Guidelines:

Example:

1
2
$crop = new CropEntropy('image.jpg');
$crop->setBlur(0.3); // Sharper result

setAutoOrient()

1
public function setAutoOrient(bool $autoOrient): self

Enables or disables automatic orientation based on EXIF data.

Parameters:

Returns: self for method chaining

Example:

1
2
$crop = new CropBalanced('image.jpg');
$crop->setAutoOrient(true); // Default

Profiling Methods

start()

1
public static function start(): void

Starts profiling timer.

mark()

1
public static function mark(): string

Returns elapsed time since start() was called.

Returns: string - Formatted time string

Example:

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

Error Handling

Common Exceptions

The library may throw these exceptions:

Error Handling Example

1
2
3
4
5
6
7
8
9
10
11
try {
    $crop = new CropCenter('image.jpg');
    $result = $crop->resizeAndCrop(300, 200);
    $result->writeImage('output.jpg');
} catch (RuntimeException $e) {
    echo "Runtime error: " . $e->getMessage();
} catch (ImagickException $e) {
    echo "Imagick error: " . $e->getMessage();
} catch (Exception $e) {
    echo "General error: " . $e->getMessage();
}

Type Safety

All methods use strict typing (PHP 8.3+):

1
2
3
4
5
6
// Correct usage
$crop->resizeAndCrop(300, 200);

// These will cause type errors
$crop->resizeAndCrop("300", "200");  // Strings instead of integers
$crop->resizeAndCrop(300.5, 200);    // Float instead of integer

Performance Considerations

Memory Usage

Processing Time

Recommendations

  1. For thumbnails: Use CropCenter
  2. For general use: Use CropBalanced
  3. For important images: Use CropEntropy

Next Steps