PHP Classes

How Can PHP Convert JPG to PNG Images Files Without PHP Image Extensions Using the Package JPEG Codec PHP: Convert JPEG images using pure PHP code

Recommend this page to a friend!
  Info   Example   Screenshots   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2025-04-22 (3 days ago) RSS 2.0 feedNot yet rated by the usersTotal: Not yet counted Not yet ranked
Version License PHP version Categories
jpeg_codec_php 1.0BSD License5Algorithms, PHP 5, Graphics
Description 

Author

This package can convert JPEG images using pure PHP code.

It provides one class that can take an array with details of the image like the width, height and pixel colors and generates an image file encoded in the JPEG format.

There is also a class that can take a JPEG image file and decode it to extract the image width, height, pixel colors and other details.

The package was tested to convert small images (256x256).

Picture of Uku-Kaarel Jo~esaar
Name: Uku-Kaarel Jo~esaar <contact>
Classes: 9 packages by
Country: Estonia Estonia
Innovation award
Innovation award
Nominee: 6x

Instructions

Example

<?php

include('class.jpeg_decoder.php');

$inputFile = "./input.jpg"; // Replace with your input file path
$jpeg_decoder_output_png = './output.png';

echo
"<html><head><title>PHP JPEG Decoder Test</title></head><body>";
echo
"<p>This is an experimental JPEG encoder and decoder ported to PHP from the JavaScript JPEG codec.</p>
<h1>Decoding: "
. htmlspecialchars($inputFile) . "</h1>";
echo
"<hr>";
echo
"<h2>Decoder Output:</h2>";
echo
"<pre style='background-color:#f0f0f0; border:1px solid #ccc; padding: 10px; max-height: 300px; overflow-y: scroll;'>";

try {
   
// --- Run the Decoder ---
   
list($frame, $jfif, $adobe, $components, $comments) = read_image_file($inputFile);

    echo
"</pre>"; // End decoder output pre block
   
echo "<hr>";
    echo
"<h2>Image Details:</h2>";
    echo
"<ul>";
    echo
"<li>Dimensions: {$frame['samplesPerLine']} x {$frame['scanLines']}</li>";
    echo
"<li>Precision: {$frame['precision']} bits</li>";
    echo
"<li>Mode: " . ($frame['baseline'] ? 'Baseline' : ($frame['progressive'] ? 'Progressive' : ($frame['extended'] ? 'Extended Sequential' : 'Unknown'))) . " (SOF Marker: 0x" . dechex($frame['type']) . ")</li>";
    echo
"<li>Components: " . count($frame['components']) . "</li>";
    if (
$jfif) echo "<li>JFIF Version: {$jfif['version']['major']}.{$jfif['version']['minor']}</li>";
    if (
$adobe) echo "<li>Adobe Marker Found: Yes (Transform: {$adobe['transformCode']})</li>";
    if (
$comments) echo "<li>Comments: " . count($comments) . "</li>";
    echo
"</ul>";


   
// --- Prepare for Image Output ---
   
$width = $frame['samplesPerLine'];
   
$height = $frame['scanLines'];

    echo
"<hr><h2>Processing Component Data for Output:</h2><pre>";
   
// Combine components into RGBA pixel data using getData
   
$pixelDataRGBA = getData($components, $width, $height, $adobe);
    echo
"</pre>";


   
// Prepare imageData structure (though copyToImageData doesn't strictly need it now)
   
$imageData = [
       
'width' => $width,
       
'height' => $height,
       
'data' => [] // Will be filled by copyToImageData (or directly assign $pixelDataRGBA)
   
];
   
// Copy (or just assign) the RGBA data
   
$imageData['data'] = $pixelDataRGBA;


   
// --- Create image with GD library ---
    
echo "<hr><h2>Creating PNG with GD:</h2>";
   
$image = imagecreatetruecolor($width, $height);
    if (!
$image) {
        throw new
Exception("GD: imagecreatetruecolor failed.");
    }
   
// Enable alpha blending for transparency handling if needed (usually not for JPEG)
   
imagealphablending($image, true); // Blend alpha channel
   
imagesavealpha($image, true); // Save full alpha channel

   
$pixelIndex = 0;
    for (
$y = 0; $y < $height; $y++) {
        for (
$x = 0; $x < $width; $x++) {
            if (!isset(
$imageData['data'][$pixelIndex+3])) {
                
// Data array might be shorter than expected if getData had issues
                
echo "<p>Warning: Ran out of pixel data at ($x, $y)</p>";
                
// Set pixel to black or break? Let's set to magenta for visibility
                
$color = imagecolorallocatealpha($image, 255, 0, 255, 0); // Opaque magenta
                
imagesetpixel($image, $x, $y, $color);
                
// Ensure pixelIndex advances correctly even on error if we continue
                
$pixelIndex += 4; // Move past the expected RGBA group
                
continue; // Skip to next pixel
           
}
           
$r = $imageData['data'][$pixelIndex++];
           
$g = $imageData['data'][$pixelIndex++];
           
$b = $imageData['data'][$pixelIndex++];
           
$a_byte = $imageData['data'][$pixelIndex++]; // Alpha 0-255

            // GD alpha is 0 (opaque) to 127 (transparent)
           
$a_gd = floor((127 * (255 - $a_byte)) / 255); // Convert 0-255 alpha to 0-127 GD alpha
           
$a_gd = max(0, min(127, $a_gd)); // Clamp just in case

           
$color = imagecolorallocatealpha($image, $r, $g, $b, $a_gd);
            if (
$color === false) {
                 echo
"<p>Warning: GD: imagecolorallocatealpha failed for pixel ($x, $y) with RGBA($r, $g, $b, $a_byte). Setting black.</p>";
                
$color = imagecolorallocatealpha($image, 0, 0, 0, 0); // Fallback: opaque black
           
}
           
imagesetpixel($image, $x, $y, $color);
        }
    }
    echo
"<p>GD Image object created.</p>";


   
// --- Save and Display ---
   
echo "<p>Saving PNG to: $jpeg_decoder_output_png</p>";
   
$saveResult = imagepng($image, $jpeg_decoder_output_png);
    if (!
$saveResult) {
         echo
"<p><b>Error: Failed to save PNG image! Check permissions and GD installation.</b></p>";
    } else {
        echo
"<p>PNG saved successfully.</p>";
    }
   
imagedestroy($image);

    echo
"<hr><h2>Comparison:</h2>";
    echo
"<h3>Original Input Image:</h3>";
    echo
"<img src='" . htmlspecialchars($inputFile) . "' alt='Original JPEG' title='Original JPEG' style='max-width: 400px; border: 1px solid blue;'>";
    echo
"<h3>Decoded Output Image (PNG):</h3>";
    if (
$saveResult) {
      echo
"<img src='" . htmlspecialchars($jpeg_decoder_output_png) . "?t=" . time() . "' alt='Decoded PNG' title='Decoded PNG' style='max-width: 400px; border: 1px solid green;'>"; // Add timestamp to avoid caching
   
} else {
      echo
"<p>(Failed to save decoded image)</p>";
    }


} catch (
Exception $e) {
    echo
'</pre><hr><h2 style="color: red;">Caught exception:</h2><pre>';
    echo
'Message: ', htmlspecialchars($e->getMessage()), "\n";
    echo
'File: ', $e->getFile(), "\n";
    echo
'Line: ', $e->getLine(), "\n";
    echo
"\nStack trace:\n", htmlspecialchars($e->getTraceAsString());
    echo
"</pre>";
}

echo
"</body></html>";


Screenshots (2)  
  • input.jpg
  • output.jpg
  Files folder image Files (7)  
File Role Description
Plain text file class.jpeg_decoder.php Class Class source
Plain text file class.jpeg_encoder.php Class Class source
Accessible without login Plain text file decoder_example.php Example Example script
Accessible without login Plain text file encoder_example.php Example Example script
Accessible without login Image file input.png Icon Icon image

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads  
 100%
Total:0
This week:0