A simple way to compress images on your server and save space. Made in pure php. Does not require dependencies.
<?php
include_once "lib/image-compressor.php";
// Array with base64 and binary image
$compressedImage = CompressImage::compress("img/image.jpg", 'compress/', 'new_image', 1000, 1000, 75, 'jpeg');
?>
The lower the resolution (width and height), the higher the image optimization rate.
- $fileSource Path, URL, or base64 of the image.
- $fileDestination File Destination Folder. Set to null to use default.
- $fileName File name. Set to null to use default.
- $maxWidth Maximum width of the image.
- $maxHeight Maximum height of the image.
- $quality Percent of the quality relative to the original image. Set to null to use default(50).
- $type Output type (jpeg, png, gif). The default is 'jpeg'. See if the original file is compatible.
Returns an Array containing base64 and binary image data.
$compressedImage = CompressImage::compress("img/image.jpg", 'compress/', 'new_image', 1000, 1000, 75, 'jpeg');
echo $compressedImag['binary']; // Base64 binary text
echo $compressedImag['base64']; // Binary text
// Writing a new image to a file
file_put_contents('new_image_compress.jpeg', $compressedImag['binary']);