SimpleImageCompressor - is a tiny simple PHP image resizer lib which allows you to resize and compress any image easily on the fly.
composer require "geckon01/simple-image-compressor"
- Download latest release here.
- Unpack the archive to your project directory.
- Include the library files:
require "src/SimpleImageCompressor.php";
require "src/CompressedImage.php";
use geckon01\SimpleImageCompressor\SimpleImageCompressor;
Resize and compress an image:
$resolutionTargetPercent = 50;
$targetQuality = 50;
$compressor = SimpleImageCompressor::load("image.png");
$compressedImage = $compressor->resizeAndCompress($resolutionTargetPercent, $targetQuality);
$compressedImage->toFile("image");
load method supports loading from local file, or you can specify any valid URL image link like this:
$compressor = SimpleImageCompressor::load("https://example.com/image.jpg");
Method chaining is supported:
SimpleImageCompressor::load("image.png")
->resizeAndCompress(50, 50)
->toFile("image");
Note: File extensions are automatically added. Use toFile("filename") without extension.
You can specify output format. Supported output fomats are:
$compressedImage->toFile("image");
$compressedImage->toBase64();
$compressedImage->toGdImage();
Set approximate minimum dimensions (aspect ratio preserved):
$compressor->setApproxMinimumHeight(500);
$compressor->setApproxMinimumWidth(500);
Note actual dimensions may differ due to aspect ratio preservation. Example: 1920×1080 image reduced to 50% becomes 960×540 (maintaining 16:9).
This software is licensed under the MIT License. View the license.