This library, created with focuses on simple methods for image processing using the Imagick
PHP extension.
- PHP extension
ext-imagick
version > 3.7
To install Imagick Image Processor using Composer, run the following command:
composer require kenura/imagick
The resizeImage
method resizes an image with the specified width and height.
public function resizeImage($inputImagePath, $outputImagePath, $width, $height);
The compressToJpg
method compresses an image to JPEG format with a specified quality.
public function compressToJpg($inputImagePath, $outputImagePath, $quality = 80);
The addWatermark
method adds a watermark to an image with various positioning options, preserving the width*height ratio of the watermark.
public function addWatermark($inputImagePath, $outputImagePath, $watermarkImagePath, $position = 'center', $widthPercent = 10, $heightPercent = 10);
The addOpacity
method makes an image transparent with a specified opacity percentage and saves it as PNG.
public function addOpacity($inputImagePath, $outputImagePath, $opacityPercent);
The resizeAndCompress
method combines image resizing and compression.
public function resizeAndCompress($inputImagePath, $outputImagePath, $width, $height, $quality = 80);
The resizeWatermarkAndCompress
method combines image resizing, watermarking, and compression.
public function resizeWatermarkAndCompress($inputImagePath, $outputImagePath, $width, $height, $watermarkImagePath, $position = 'center', $widthPercent = 10, $heightPercent = 10, $quality = 80);
use YourNamespace\ImageProcessor;
// Create an instance of the ImageProcessor
$imageProcessor = new ImageProcessor();
// Use the methods based on your requirements
$imageProcessor->resizeImage($inputImagePath, $outputImagePath, $width, $height);
$imageProcessor->compressToJpg($inputImagePath, $outputImagePath, $quality);
$imageProcessor->addWatermark($inputImagePath, $outputImagePath, $watermarkImagePath, $position, $widthPercent, $heightPercent);
$imageProcessor->addOpacity($inputImagePath, $outputImagePath, $opacityPercent);
$imageProcessor->resizeAndCompress($inputImagePath, $outputImagePath, $width, $height, $quality);
$imageProcessor->resizeWatermarkAndCompress($inputImagePath, $outputImagePath, $width, $height, $watermarkImagePath, $position, $widthPercent, $heightPercent, $quality);
Thanks to @[email protected] for using this library!
Feel free to contribute or report issues.