-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 2b9b5d8
Showing
8 changed files
with
289 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.idea | ||
.DS_Store | ||
/composer.lock | ||
/vendor/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
PHPLepton | ||
=========== | ||
A php wrapper library for Dropbox Lepton compression tool. | ||
|
||
**This guide assumes you have Dropbox Lepton installed.** | ||
|
||
Installation | ||
----------- | ||
You can install this library with composer or include it manually in your project. | ||
|
||
Quick start | ||
----------- | ||
|
||
```php | ||
$lepton = new Lepton('<LeptonPath>'); | ||
``` | ||
|
||
After this you can run one of two commands, compress or decompress. If the compression or decompression failed Lepton will throw an Exception, otherwise it returns TRUE. | ||
|
||
```php | ||
$options = array( | ||
); | ||
|
||
$result = $lepton->compress( | ||
'example.jpg', | ||
'example.lep', | ||
$options | ||
); | ||
|
||
$result = $lepton->decompress( | ||
'example.lep', | ||
'example.jpg', | ||
$options | ||
); | ||
``` | ||
|
||
##Available Options | ||
```php | ||
$options = array( | ||
'-unjailed', | ||
'-singlethread', | ||
'-maxchildren', | ||
'-preload', | ||
'-unkillable', | ||
'-allowprogressive', | ||
'-zlib0', | ||
'-timebound=<>ms', | ||
'-trunc=<>', | ||
'-memory=<>M', | ||
'-threadmemory=<>M', | ||
'-hugepages', | ||
'-avx2upgrade', | ||
'-injectsyscall={1..4}', | ||
'-socket', | ||
'-socket=<name>', | ||
'-listen', | ||
'-listen=<port>', | ||
'-zliblisten', | ||
'-zliblisten=<port>', | ||
'-recodememory=<>M' | ||
); | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"name": "gtuk/php-lepton", | ||
"author": "Florian Lambart", | ||
"description": "A php wrapper library for Dropbox Lepton compression tool", | ||
"type": "library", | ||
"keywords": [ | ||
"lepton", | ||
"dropbox", | ||
"jpeg", | ||
"jpg", | ||
"lep", | ||
"compression", | ||
"decompression", | ||
"php" | ||
], | ||
"license": "MIT", | ||
"require": { | ||
"php": ">=5.5" | ||
}, | ||
"autoload": { | ||
"psr-0": { | ||
"PHPLepton": "src/" | ||
} | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^4.8" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
namespace PHPLepton; | ||
|
||
use Exception; | ||
|
||
/** | ||
* Class Lepton | ||
* | ||
* @package PHPLepton | ||
*/ | ||
class Lepton | ||
{ | ||
protected $binaryPath; | ||
|
||
/** | ||
* Lepton Constructor | ||
* | ||
* @param string $binaryPath | ||
* | ||
* @throws \Exception | ||
*/ | ||
public function __construct($binaryPath) | ||
{ | ||
if (! file_exists($binaryPath)) { | ||
throw new Exception('Binary path doesn\'t exist'); | ||
} | ||
$this->binaryPath = $binaryPath; | ||
} | ||
|
||
|
||
/** | ||
* Compress image to .lep | ||
* | ||
* @param string $inputFile | ||
* @param string $outputFile | ||
* @param array $parameters | ||
* | ||
* @return bool | ||
* | ||
* @throws \Exception | ||
*/ | ||
public function compress($inputFile, $outputFile, $parameters = array()) | ||
{ | ||
if ('jpg' != pathinfo($inputFile, PATHINFO_EXTENSION) && | ||
'jpeg' != pathinfo($inputFile, PATHINFO_EXTENSION) | ||
) { | ||
throw new Exception('Input file must be a jpg or jpeg'); | ||
} | ||
|
||
if ('lep' != pathinfo($outputFile, PATHINFO_EXTENSION)) { | ||
throw new Exception('Output file must be a .lep file'); | ||
} | ||
|
||
$parameters = implode(' ', $parameters); | ||
|
||
exec($this->binaryPath.' '.$parameters.' '.escapeshellarg($inputFile).' '.escapeshellarg($outputFile).' 2>&1', $output, $returnValue); | ||
|
||
if (0 !== $returnValue) { | ||
throw new Exception('There was an error during the compression'); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Decompress .lep image | ||
* | ||
* @param string $inputFile | ||
* @param string $outputFile | ||
* @param array $parameters | ||
* | ||
* @return bool | ||
* | ||
* @throws \Exception | ||
*/ | ||
public function decompress($inputFile, $outputFile, $parameters = array()) | ||
{ | ||
if ('lep' != pathinfo($inputFile, PATHINFO_EXTENSION)) { | ||
throw new Exception('Input file must be a .lep file'); | ||
} | ||
|
||
if ('jpg' != pathinfo($outputFile, PATHINFO_EXTENSION) && | ||
'jpeg' != pathinfo($outputFile, PATHINFO_EXTENSION) | ||
) { | ||
throw new Exception('Output file must be a jpg or jpeg'); | ||
} | ||
|
||
$parameters = implode(' ', $parameters); | ||
|
||
exec($this->binaryPath.' '.$parameters.' '.escapeshellarg($inputFile).' '.escapeshellarg($outputFile).' 2>&1', $output, $returnValue); | ||
|
||
if (0 !== $returnValue) { | ||
throw new Exception('There was an error during the decompression'); | ||
} | ||
|
||
return true; | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
namespace PHPLepton\Tests; | ||
|
||
use Exception; | ||
use PHPLepton\Lepton; | ||
use PHPUnit_Framework_TestCase; | ||
|
||
/** | ||
* Class LeptonTest | ||
* | ||
* @package PHPLepton\Tests | ||
*/ | ||
class LeptonTest extends PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* Simple compress and decompress test | ||
*/ | ||
public function testSimpleCompressionAndDecompressionSuccess() | ||
{ | ||
$lepton = new Lepton('/usr/local/bin/lepton'); | ||
$compressionResult = $lepton->compress( | ||
__DIR__.'/Images/bridge.jpg', | ||
__DIR__.'/Images/bridge.lep' | ||
); | ||
|
||
$this->assertTrue($compressionResult); | ||
$this->assertTrue(file_exists(__DIR__.'/Images/bridge.lep')); | ||
|
||
$decompressionResult = $lepton->decompress( | ||
__DIR__.'/Images/bridge.lep', | ||
__DIR__.'/Images/bridge.jpg' | ||
); | ||
|
||
$this->assertTrue($decompressionResult); | ||
|
||
unlink(__DIR__.'/Images/bridge.lep'); | ||
} | ||
|
||
/** | ||
* Advanced compress and decompress test | ||
*/ | ||
public function testAdvancedCompressionAndDecompressionSuccess() | ||
{ | ||
$lepton = new Lepton('/usr/local/bin/lepton'); | ||
$compressionResult = $lepton->compress( | ||
__DIR__.'/Images/newYork.jpg', | ||
__DIR__.'/Images/newYork.lep', | ||
array( | ||
'-allowprogressive', | ||
) | ||
); | ||
|
||
$this->assertTrue($compressionResult); | ||
$this->assertTrue(file_exists(__DIR__.'/Images/newYork.lep')); | ||
|
||
$decompressionResult = $lepton->decompress( | ||
__DIR__.'/Images/newYork.lep', | ||
__DIR__.'/Images/newYork.jpg', | ||
array( | ||
'-allowprogressive', | ||
) | ||
); | ||
|
||
$this->assertTrue($decompressionResult); | ||
|
||
unlink(__DIR__.'/Images/newYork.lep'); | ||
} | ||
|
||
/** | ||
* Simple compress and decompress exception test | ||
*/ | ||
public function testSimpleCompressionException() | ||
{ | ||
$lepton = new Lepton('/usr/local/bin/lepton'); | ||
|
||
try { | ||
$compressionResult = $lepton->compress( | ||
__DIR__.'/Images/bridge.png', | ||
__DIR__.'/Images/bridge.lep' | ||
); | ||
} catch (Exception $e) { | ||
$this->assertEquals($e->getMessage(), 'Input file must be a jpg or jpeg'); | ||
|
||
return; | ||
} | ||
|
||
$this->fail('Expected Exception has not been raised'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
$loader = require __DIR__.'/../vendor/autoload.php'; | ||
|
||
$loader->add('PHPLepton\Tests', __DIR__); |