PHP code example of devscast / tinify

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

    

devscast / tinify example snippets


use Devscast\Tinify\Client;

$tinify = new Client('yourtinifytoken');

use Devscast\Tinify\Client;

$tinify = new Client(
    token: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    proxy: 'http://user:[email protected]:8080'
);



use Devscast\Tinify\Client;

$tinify = new Client(token: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');

$tinify->toFile(
    source: $tinify->fromFile('/home/tinify/pictures/test.png'),
    path: '/home/tinify/pictures/test-compressed.png'
);



$sourceBuffer = file_get_contents("unoptimized.jpg");
$compressedBuffer = $tinify->toBuffer(source: $tinify->fromBuffer($data));



$tinify->toFile(
    source: $tinify->fromUrl('https://tinypng.com/images/panda-happy.png'),
    path: '/home/tinify/pictures/test-compressed.png'
);

$tinify->toFile(
    source: $tinify->resize(
        source: $tinify->fromFile('/home/bernard-ng/Pictures/test.png'),
        method: 'fit',
        width: 500,
        height: 500
    ),
    path: '/home/bernard-ng/tinify/test-compressed-resized.png'
);

$tinify->toFile(
    source: $tinify->preserve(
        source: $tinify->fromFile('/home/bernard-ng/Pictures/test.png'),
        metadata: ['creation', 'copyright']
    ),
    path: '/home/bernard-ng/dev/projects/tinify-php/data/test-compressed.png'
);
 
$source = $tinify->toCloud(
    source: $tinify->fromFile('/home/tinify/pictures/test.png'),
    bucket_path: 'tinify/images/test.png',
    storage: new Aws(
        region: 'ap-south-1',
        secret_access_key: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
        access_key_id: 'xxxxxxxxxxxxxxx',
        option: ['headers' => ['Cache-Control' => 'max-age=31536000, public']] // optional
    )
);

$tinify->toCloud(
    source: $tinify->fromFile('/home/bernard-ng/Pictures/test.png'),
    bucket_path: 'tinify/images/test.png',
    storage: new Gcs(
        access_token: 'XXXXXXXXXXXXXXXXXXXXXXXXX',
        option: ['headers' => ['Cache-Control' => 'max-age=31536000, public']] // optional
    )
);



$source = $tinify->fromUrl('https://tinypng.com/images/panda-happy.png');
$source->getCompressionCount();

$tinify->toFile($source, path: '/home/tinify/pictures/test-compressed.png');