PHP code example of megaoptim / megaoptim-php

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

    

megaoptim / megaoptim-php example snippets




use MegaOptim\Optimizer;
$megaoptim = new Optimizer('your-api-key');
$response   = $megaoptim->run( '/path/to/file.jpg', array( 'compression' => Optimizer::COMPRESSION_INTELLIGENT ) );

use MegaOptim\Optimizer;
$megaoptim = new Optimizer('your-api-key');
$response   = $megaoptim->run( 'http://yoursite.com/some_image.jpg', array( 'compression' => Optimizer::COMPRESSION_INTELLIGENT ) );

use MegaOptim\Optimizer;
$megaoptim = new Optimizer('your-api-key');
$resources = array(
	'/path/to/file1.jpg',
	'/path/to/file2.jpg',
	'/path/to/file3.jpg',
);

$response   = $megaoptim->run( $resources, array( 'compression' => Optimizer::LOSSY ) );

use MegaOptim\Optimizer;
$megaoptim = new Optimizer('your-api-key');
$resources = array(
	'http://somesite.com/path/to/file1.jpg',
	'http://somesite.com/path/to/file2.jpg',
	'http://somesite.com/path/to/file3.jpg',
);

$response   = $megaoptim->run( $resources, array( 'compression' => Optimizer::LOSSY ) );

$files = $response->getOptimizedFiles();
foreach($files as $file) {
 // Do something
}

if( !empty($files) ) {
	// Note: Thise are for demonstration purposes, you don't have to use it like this.
	// Overwrite
	$files[0]->saveOverwrite();
	// Or save as other file
	$files[0]->saveAsFile('/path/to/other/file.jpg');
	// Or save in some other dir with the original name
	$files[0]->saveToDir('/path/to/other');
}


composer 

public function run($resource, $args = array())
$response
$response->isSuccessful()
$response->isError()
$response->getErrors()
array
$response->getOptimizedFiles()
$file->getFileName()
$file->getOptimizedSize()
$file->getSavedBytes()
$file->getSavedPercent()
$file->getUrl()
$file->getWebP()
$file->saveOverwrite()
$file->saveAsFile( $file_path )
$file->saveToDir( $dir_path )