PHP code example of renderthat / php-pngquant

1. Go to this page and download the library: Download renderthat/php-pngquant library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

renderthat / php-pngquant example snippets




use ourcodeworld\PNGQuant\PNGQuant;

$instance = new PNGQuant();

// Change the path to the binary of pngquant, for example in windows would be (with an example path):
$instance->setBinaryPath("C:\\Users\\sdkca\\Desktop\\pngquant.exe")
    // Other options of PNGQuant here
    ->execute();



ance = new PNGQuant();

// Configuration here ...

// Include the class
use ourcodeworld\PNGQuant\PNGQuant;

$instance = new PNGQuant();

// Set the path to the image to compress
$exit_code = $instance->setImage("/a-folder/image-original.png")
    // Set the output filepath
    ->setOutputImage("/a-folder/image-compressed.png")
    // Overwrite output file if exists, otherwise pngquant will generate output ...
    ->overwriteExistingFile()
    // As the quality in pngquant isn't fixed (it uses a range)
    // set the quality between 50 and 80
    ->setQuality(50,80)
    // Execute the command
    ->execute();

// if exit code is equal to 0 then everything went right !
if(!$exit_code){
    echo "Image succesfully compressed";
}else{
    echo "Something went wrong (status code $exit_code)  with description: ". $instance->getErrorTable()[(string) $exit_code];
}

// Include the class
use ourcodeworld\PNGQuant\PNGQuant;

$instance = new PNGQuant();

// Set the path to the image to compress
$result = $instance->setImage("/a-folder/image-original.png")
    // Overwrite output file if exists, otherwise pngquant will generate output ...
    ->overwriteExistingFile()
    // As the quality in pngquant isn't fixed (it uses a range)
    // set the quality between 50 and 80
    ->setQuality(50,80)
    // Retrieve RAW data from pngquant
    ->getRawOutput();

$exit_code = $result["statusCode"];


// if exit code is equal to 0 then everything went right !
if($exit_code == 0){

    $rawImage = imagecreatefromstring($result["imageData"]);

    // Example Save the PNG Image from the raw data into a file or do whatever you want.
    // imagepng($rawImage , 'newfile.png');

    echo "Image succesfully compressed, do something with the raw Data";
}else{
    echo "Something went wrong (status code $exit_code)  with description: ". $instance->getErrorTable()[(string) $exit_code];
}
batch
composer