PHP code example of flynsarmy / image-optimizer

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

    

flynsarmy / image-optimizer example snippets


$factory = new \ImageOptimizer\OptimizerFactory();
$optimizer = $factory->get();

$filepath = /* path to image */;

$optimizer->optimize($filepath);
//optimized file overwrites original one

$factory = new \ImageOptimizer\OptimizerFactory(array('ignore_errors' => false), $logger);

//default optimizer is `smart`
$optimizer = $factory->get();

//png optimizer
$pngOptimizer = $factory->get('png');

//jpegoptim optimizer etc.
$jpgOptimizer = $factory->get('jpegoptim');

$factory = new \ImageOptimizer\OptimizerFactory(array('custom_optimizers' => array(
    'some_optimizier' => array(
        'command' => 'some_command',
        'args' => array('-some-flag')
    )
)), $logger);

$customOptimizer = $factory->get('some_optimizier');

$factory = new \ImageOptimizer\OptimizerFactory();
print_r($factory->checkOptimizers());

class StdoutLogger extends \Psr\Log\AbstractLogger {
    public function log($level, $message, array $context = array()) {
        echo $message."\n";
    }
}

$factory = new \ImageOptimizer\OptimizerFactory(array(), new StdoutLogger());

$factory->get()->optimize('yourfile.jpg');

// and have a look at stdout