Skip to content

Quick Start

This guide will help you get started with the Imagick Image Processor library quickly. We’ll cover the basics of each major feature with simple examples.

First, make sure you’ve installed the library. Then, create a new PHP file and include the autoloader:

<?php
require_once __DIR__ . '/vendor/autoload.php';
use Kenura\Imagick\ImageProcessor;
// Create an instance
$processor = new ImageProcessor();

Let’s start with a simple resize operation:

<?php
require_once __DIR__ . '/vendor/autoload.php';
use Kenura\Imagick\ImageProcessor;
$processor = new ImageProcessor();
// Resize an image to 800x600 (maintains aspect ratio)
$processor->resizeImage(
'path/to/input.jpg',
'path/to/output.jpg',
800,
600
);
echo "Image resized successfully!";

Reduce file size while maintaining quality:

// Compress to approximately 100KB
$processor->compressToJpg(
'input.jpg',
'compressed.jpg',
100 // Target size in KB
);

The method will automatically adjust the quality to reach the target file size.

Protect your images with a watermark:

// Add watermark in the bottom-right corner
$processor->addWatermark(
'input.jpg',
'watermarked.jpg',
'logo.png',
'bottom-right', // Position
10 // Size (10% of diagonal)
);

Available positions:

  • center (default)
  • top, bottom, left, right
  • top-left, top-right, bottom-left, bottom-right

Make an image semi-transparent:

// Set opacity to 50%
$processor->addOpacity(
'input.png',
'transparent.png',
50 // Opacity percentage (0-100)
);

For more complex workflows, you can perform multiple operations sequentially:

<?php
require_once __DIR__ . '/vendor/autoload.php';
use Kenura\Imagick\ImageProcessor;
$processor = new ImageProcessor();
// Step 1: Resize
$processor->resizeImage('original.jpg', 'temp.jpg', 1200, 800);
// Step 2: Add watermark
$processor->addWatermark('temp.jpg', 'temp2.jpg', 'logo.png', 'bottom-right', 8);
// Step 3: Compress
$processor->compressToJpg('temp2.jpg', 'final.jpg', 150);
// Clean up temporary files
unlink('temp.jpg');
unlink('temp2.jpg');
echo "Processing complete!";

Always wrap your operations in try-catch blocks for production code:

<?php
require_once __DIR__ . '/vendor/autoload.php';
use Kenura\Imagick\ImageProcessor;
$processor = new ImageProcessor();
try {
$processor->resizeImage('input.jpg', 'output.jpg', 800, 600);
echo "Success!";
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
// Log the error or handle it appropriately
}

The library supports JPEG, PNG, and GIF formats:

// JPEG
$processor->resizeImage('photo.jpg', 'resized.jpg', 800, 600);
// PNG (preserves transparency)
$processor->resizeImage('graphic.png', 'resized.png', 800, 600);
// GIF (preserves animation on first frame)
$processor->resizeImage('animation.gif', 'resized.gif', 800, 600);

Here’s a complete example that processes user-uploaded images:

<?php
require_once __DIR__ . '/vendor/autoload.php';
use Kenura\Imagick\ImageProcessor;
$processor = new ImageProcessor();
// Configuration
$uploadDir = __DIR__ . '/uploads/';
$processedDir = __DIR__ . '/processed/';
$watermarkPath = __DIR__ . '/assets/watermark.png';
// Ensure directories exist
if (!is_dir($processedDir)) {
mkdir($processedDir, 0755, true);
}
// Process an uploaded image
if (isset($_FILES['image'])) {
$inputPath = $uploadDir . $_FILES['image']['name'];
$outputPath = $processedDir . 'processed_' . $_FILES['image']['name'];
try {
// Move uploaded file
move_uploaded_file($_FILES['image']['tmp_name'], $inputPath);
// Resize to max 1920x1080
$processor->resizeImage($inputPath, $outputPath, 1920, 1080);
// Add watermark
$processor->addWatermark(
$outputPath,
$outputPath,
$watermarkPath,
'bottom-right',
10
);
// Compress to ~200KB
$processor->compressToJpg($outputPath, $outputPath, 200);
echo "Image processed successfully!";
// Clean up original
unlink($inputPath);
} catch (Exception $e) {
echo "Error processing image: " . $e->getMessage();
}
}

Now that you understand the basics, explore more detailed guides:

Or check out the API Reference for complete method documentation.