Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
gtuk committed Jul 24, 2016
0 parents commit 2b9b5d8
Show file tree
Hide file tree
Showing 8 changed files with 289 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea
.DS_Store
/composer.lock
/vendor/
63 changes: 63 additions & 0 deletions README.md
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'
);
```

28 changes: 28 additions & 0 deletions composer.json
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"
}
}
99 changes: 99 additions & 0 deletions src/PHPLepton/Lepton.php
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;
}
}
Binary file added tests/PHPLepton/Tests/Images/bridge.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/PHPLepton/Tests/Images/newYork.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
90 changes: 90 additions & 0 deletions tests/PHPLepton/Tests/LeptonTest.php
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');
}
}
5 changes: 5 additions & 0 deletions tests/bootstrap.php
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__);

0 comments on commit 2b9b5d8

Please sign in to comment.