Use PHP to convert PNG to JPG with compression?

Use PHP to convert PNG to JPG with compression?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Converting PNG to JPG in PHP: Quality, Compression, and Implementation

Converting high-quality PNG files to JPG for web delivery is a common optimization task. The motivation is straightforward: JPG offers significantly smaller file sizes than PNG when dealing with photographic content or complex gradients, which reduces bandwidth usage and improves page load times. PHP provides built-in capabilities to handle this conversion directly within your application logic, but understanding the underlying mechanics of quality control and memory management is essential for production use.

The core functionality resides in GD2, the Graphics Draw library that comes bundled with most PHP installations. You do not need external extensions like ImageMagick unless you require advanced features such as transparency preservation or specific color profile manipulations. To convert a file, you simply read the image into memory using imagecreatefrompng, apply any necessary adjustments (like resizing), and then write the output in JPG format using imagejpeg.

$sourceFile = 'input.png';
$outputFile = 'output.jpg';
$maxWidth = 1200; // Web-safe dimension

// Create image from PNG source
$src = imagecreatefrompng($sourceFile);

if ($src) {
    // Optional: Resize to maintain quality without excessive file size
    list($w, $h) = getimagesize($sourceFile);
    $ratio = $maxWidth / $w;
    $newW = $w * $ratio;
    $newH = $h * $ratio;

    if ($newW < $w || $newH < $h) {
        $dst = imagecreatetruecolor($newW, $newH);
        imagecopyresampled($dst, $src, 0, 0, 0, 0, $newW, $newH, $w, $h);
        
        // Output as JPG with quality factor (0-100)
        // 75 is a standard balance between size and fidelity
        imagejpeg($dst, $outputFile, 75);
        
        imagedestroy($src);
        imagedestroy($dst);
    } else {
        imagejpeg($src, $outputFile, 75);
        imagedestroy($src);
    }
}

The second argument to imagejpeg is the quality factor, ranging from 0 to 100. This value directly impacts the compression algorithm used by the JPEG encoder. A lower quality setting reduces file size but introduces artifacts like blurring or color banding, which are particularly visible in high-contrast edges common in PNGs. A setting of 85 to 95 usually provides an optimal balance for web delivery, ensuring the visual difference from the original is negligible to the human eye while achieving substantial compression gains.

One critical pitfall involves memory usage. PHP loads the entire image into RAM before processing. If you are converting a batch of high-resolution images (e.g., 4000x3000 pixels), your script may exhaust available memory, causing a fatal error or triggering server-level limits. For large batches, it is better to process images asynchronously using a job queue rather than running a synchronous loop that holds multiple large image buffers in memory simultaneously.

Another consideration is the handling of transparency. PNG files often support alpha channels, whereas standard JPG does not. If you convert a transparent PNG directly to JPG without first creating a background color (usually white or black), the result will have a solid-colored background instead of transparency. The code snippet above assumes an opaque source; if transparency must be preserved visually, you should composite a background onto the image before saving as JPG.

For high-volume conversions on modern PHP versions (7.4+), leveraging the imagick extension offers superior control over compression filters and noise removal, but it requires additional installation steps. Sticking to GD2 is often sufficient for standard web assets if configured correctly. If your application stack is built with Laravel, you can wrap this logic in a service class or utilize existing queue workers to handle the heavy lifting off the request thread, ensuring the user experience remains snappy regardless of conversion latency.

Our network