PHP code example of kinglozzer / tinypng

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

    

kinglozzer / tinypng example snippets


use Kinglozzer\TinyPng\Compressor;
use Kinglozzer\TinyPng\Exception\AuthorizationException;
use Kinglozzer\TinyPng\Exception\InputException;
use Kinglozzer\TinyPng\Exception\LogicException;

$compressor = new Compressor('<your-tinypng-api-key>');

try {
    $result = $compressor->compress('/path/to/original.png');
    $result = $compressor->compress('<image data>', true); // Compress raw image data
    $result->writeTo('/path/to/compressed.png'); // Write the returned image
    $result->getCompressedFileSize(); // Int size of compressed image, e.g: 104050
    $result->getCompressedFileSize(true); // Human-readable, e.g: '101.61 KB'
    $result->getResponseData(); // array containing JSON-decoded response data
} catch (AuthorizationException $e) {
    // Invalid credentials or requests per month exceeded
} catch (InputException $e) {
    // Not a valid PNG or JPEG
} catch (Exception $e) {
    // Unknown error
}