Image Compressor for PHP
Free online image compressor with PHP code examples
Working with image compressor in PHP? Our free online image compressor helps PHP developers format, validate, and process data instantly. Below you will find PHP code examples using GD (built-in) / Intervention Image so you can achieve the same result programmatically in your own projects.
Try the Image Compressor Online
Use our free Image Compressor directly in your browser — no setup required.
Open Image CompressorPHP Code Example
<?php
// Using built-in GD library
$image = imagecreatefromjpeg('input.jpg');
$width = imagesx($image);
$height = imagesy($image);
// Resize if needed
$maxWidth = 1920;
if ($width > $maxWidth) {
$ratio = $maxWidth / $width;
$newWidth = (int)($width * $ratio);
$newHeight = (int)($height * $ratio);
$resized = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($resized, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
imagejpeg($resized, 'output.jpg', 80);
imagedestroy($resized);
} else {
imagejpeg($image, 'output.jpg', 80);
}
imagedestroy($image);Quick Setup
Library: GD (built-in) / Intervention Image
composer require intervention/imagePHP Tips & Best Practices
- GD library is built into most PHP installations
- Intervention Image provides a cleaner API
- imagejpeg quality parameter ranges from 0 (worst) to 100 (best)