Skip to content

ImageProcessor Class

The ImageProcessor class is the main class of the Imagick Image Processor library. It provides methods for resizing, compressing, watermarking, and adjusting opacity of images.

Kenura\Imagick\ImageProcessor
class ImageProcessor
{
// Public Methods
public function resizeImage(
string $inputImagePath,
string $outputImagePath,
int $width,
int $height
): void;
public function compressToJpg(
string $inputImagePath,
string $outputImagePath,
int $targetFileSize = 100,
int $quality = 80
): void;
public function addWatermark(
string $inputImagePath,
string $outputImagePath,
string $watermarkImagePath,
string $position = 'center',
int $scalePercent = 10
): void;
public function addOpacity(
string $inputImagePath,
string $outputImagePath,
int $opacityPercent
): void;
public function convertToWebP(
string $inputImagePath,
string $outputImagePath,
int $quality = 80
): void;
// Private Methods
private function openImage(string $imagePath): resource;
}
Terminal window
composer require kenura/imagick
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Kenura\Imagick\ImageProcessor;
// Create an instance
$processor = new ImageProcessor();
// Use the methods
$processor->resizeImage('input.jpg', 'output.jpg', 800, 600);

Resizes an image while maintaining aspect ratio.

public function resizeImage(
string $inputImagePath,
string $outputImagePath,
int $width,
int $height
): void

Parameters:

  • $inputImagePath - Path to the input image
  • $outputImagePath - Path where the resized image will be saved
  • $width - Target width in pixels
  • $height - Target height in pixels

Returns: void

Throws: Exception if image processing fails

Example:

$processor->resizeImage('photo.jpg', 'resized.jpg', 1200, 800);

Full documentation →


Compresses an image to a target file size.

public function compressToJpg(
string $inputImagePath,
string $outputImagePath,
int $targetFileSize = 100,
int $quality = 80
): void

Parameters:

  • $inputImagePath - Path to the input image
  • $outputImagePath - Path where the compressed image will be saved
  • $targetFileSize - Target file size in KB (default: 100)
  • $quality - Starting quality level 0-100 (default: 80)

Returns: void

Throws: Exception if image processing fails

Example:

$processor->compressToJpg('large.jpg', 'compressed.jpg', 150, 85);

Full documentation →


Adds a watermark to an image with flexible positioning.

public function addWatermark(
string $inputImagePath,
string $outputImagePath,
string $watermarkImagePath,
string $position = 'center',
int $scalePercent = 10
): void

Parameters:

  • $inputImagePath - Path to the input image
  • $outputImagePath - Path where the watermarked image will be saved
  • $watermarkImagePath - Path to the watermark image
  • $position - Position of watermark: center, top, bottom, left, right, top-left, top-right, bottom-left, bottom-right (default: center)
  • $scalePercent - Watermark size as percentage of image diagonal (default: 10)

Returns: void

Throws: Exception if image processing fails

Example:

$processor->addWatermark(
'photo.jpg',
'watermarked.jpg',
'logo.png',
'bottom-right',
12
);

Full documentation →


Adjusts the opacity/transparency of an image.

public function addOpacity(
string $inputImagePath,
string $outputImagePath,
int $opacityPercent
): void

Parameters:

  • $inputImagePath - Path to the input image
  • $outputImagePath - Path where the output image will be saved (always PNG)
  • $opacityPercent - Opacity level 0-100 (0=transparent, 100=opaque)

Returns: void

Throws: Exception if image processing fails

Example:

$processor->addOpacity('image.jpg', 'transparent.png', 50);

Full documentation →


Converts images to modern WebP format for superior web performance.

public function convertToWebP(
string $inputImagePath,
string $outputImagePath,
int $quality = 80
): void

Parameters:

  • $inputImagePath - Path to the input image
  • $outputImagePath - Path where the WebP image will be saved
  • $quality - Quality level 0-100 (default: 80)

Returns: void

Throws: Exception if image processing fails

Example:

$processor->convertToWebP('photo.jpg', 'photo.webp', 85);

Full documentation →

Opens an image file and returns a GD resource. Automatically detects the image type and uses the appropriate function.

private function openImage(string $imagePath): resource

Parameters:

  • $imagePath - Path to the image file

Returns: GD image resource

Throws: Exception if image type is unsupported

Supported Formats:

  • JPEG (.jpg, .jpeg)
  • PNG (.png)
  • GIF (.gif)

The library supports the following input formats:

FormatExtensionNotes
JPEG.jpg, .jpegFully supported
PNG.pngTransparency preserved where applicable
GIF.gifFirst frame only (animations not preserved)

Output format depends on the method used:

MethodOutput Format
resizeImage()Same as input
compressToJpg()JPEG only
addWatermark()JPEG
addOpacity()PNG only

All public methods may throw exceptions. Always wrap calls in try-catch blocks:

<?php
use Kenura\Imagick\ImageProcessor;
$processor = new ImageProcessor();
try {
$processor->resizeImage('input.jpg', 'output.jpg', 800, 600);
echo "Success!";
} catch (Exception $e) {
error_log("Error: " . $e->getMessage());
echo "Processing failed.";
}
  • File not found: Input image or watermark doesn’t exist
  • Invalid image: File is not a valid image
  • Unsupported format: Image format not supported
  • Processing failed: General processing error
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Kenura\Imagick\ImageProcessor;
$processor = new ImageProcessor();
try {
// Resize
$processor->resizeImage('original.jpg', 'temp1.jpg', 1200, 800);
echo "✅ Resized\n";
// Add watermark
$processor->addWatermark(
'temp1.jpg',
'temp2.jpg',
'watermark.png',
'bottom-right',
10
);
echo "✅ Watermarked\n";
// Compress
$processor->compressToJpg('temp2.jpg', 'final.jpg', 150);
echo "✅ Compressed\n";
// Clean up
unlink('temp1.jpg');
unlink('temp2.jpg');
echo "✅ Processing complete!\n";
} catch (Exception $e) {
echo "❌ Error: " . $e->getMessage() . "\n";
}
  • PHP 8.1 or higher
  • ext-imagick extension version 3.7 or higher
  • GD library (included with most PHP installations)