PHP code example of despark / image-purify

1. Go to this page and download the library: Download despark/image-purify 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/ */

    

despark / image-purify example snippets


$options = [];
$purifierFactory = new ImagePurifierFactory($options, $logger);

$purifier = $purifierFactory->create();
$purifier->purify('path/to/image.png');

[
// This is the chain config. You can define your own chain and feed it commands
'chains' => [
    JpegChain::class => [ // Chains must be real classes which contains commands to be executed
        'commands' => [
            'mozJpeg' => [
                'bin' => 'cjpeg', // Path to the executable. The lbrary tries to resolve it itself
                'arguments' => ['-optimize', '-progressive'], // What arguments to run
                'customClass' => MozJpeg::class, // If you want you can give custom class that must implements our CommandInterface
            ],
        ],
        'first_only' => false, // If this is true only the first command will be executed
    ],
    ...
],
'suppress_errors' => false // If set to true exceptions will be catched and only logs will be written,
]);

try{
    $purifier->purify($filePath);
}catch(CommandException $e){
    if($e->getCode() === 98){
        // handle png quant already processed
    }
}