Skip to content

Commit dd20410

Browse files
committed
Add gifsicle to optimize GIFs
1 parent c8dddfa commit dd20410

File tree

8 files changed

+104
-23
lines changed

8 files changed

+104
-23
lines changed

Diff for: composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"doctrine/cache": "^1.6"
1010
},
1111
"require-dev": {
12-
"ext-gd": "*"
12+
"ext-gd": "*",
13+
"phpunit/phpunit": "^5.0"
1314
},
1415
"autoload": {
1516
"psr-0": {

Diff for: phpunit.xml

+2-22
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/5.2/phpunit.xsd"
4-
backupGlobals="false"
5-
backupStaticAttributes="false"
6-
bootstrap="tests/bootstrap.php"
7-
cacheTokens="false"
8-
colors="false"
9-
convertErrorsToExceptions="true"
10-
convertNoticesToExceptions="true"
11-
convertWarningsToExceptions="true"
12-
forceCoversAnnotation="false"
13-
printerClass="PHPUnit_TextUI_ResultPrinter"
14-
processIsolation="false"
15-
stopOnError="false"
16-
stopOnFailure="false"
17-
stopOnIncomplete="false"
18-
stopOnSkipped="false"
19-
stopOnRisky="false"
20-
testSuiteLoaderClass="PHPUnit_Runner_StandardTestSuiteLoader"
21-
timeoutForSmallTests="1"
22-
timeoutForMediumTests="10"
23-
timeoutForLargeTests="60"
24-
verbose="false">
3+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/5.7/phpunit.xsd"
4+
bootstrap="tests/bootstrap.php">
255
<testsuites>
266
<testsuite name="ImageStack Tests Suite">
277
<file>tests/ImagePathTests.php</file>
+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
namespace ImageStack\ImageOptimizer;
3+
4+
use ImageStack\ImageOptimizer\Exception\ImageOptimizerException;
5+
6+
/**
7+
* Gifsicle image optimizer.
8+
*/
9+
class GifsicleImageOptimizer extends AbstractExternalImageOptimizer
10+
{
11+
12+
/**
13+
* {@inheritDoc}
14+
* @see \ImageStack\ImageOptimizer\ImageOptimizerInterface::getSupportedMimeTypes()
15+
*/
16+
public function getSupportedMimeTypes()
17+
{
18+
return [
19+
'image/gif',
20+
];
21+
}
22+
23+
/**
24+
* {@inheritDoc}
25+
* @see \ImageStack\ImageOptimizer\AbstractExternalImageOptimizer::getInputFileExtension()
26+
*/
27+
protected function getInputFileExtension()
28+
{
29+
return 'gif';
30+
}
31+
32+
/**
33+
* Return the output file extension.
34+
* @return string
35+
*/
36+
protected function getOutputFileExtension()
37+
{
38+
return 'gif';
39+
}
40+
41+
/**
42+
* {@inheritDoc}
43+
* @see \ImageStack\ImageOptimizer\AbstractExternalImageOptimizer::checkIsMimeTypeSupported()
44+
*/
45+
protected function checkIsMimeTypeSupported($mimeType)
46+
{
47+
return 'image/gif' == $mimeType;
48+
}
49+
50+
/**
51+
* {@inheritDoc}
52+
* @see \ImageStack\ImageOptimizer\AbstractExternalImageOptimizer::execExternalOptimizer()
53+
*/
54+
protected function execExternalOptimizer($if, &$binaryContent)
55+
{
56+
$of = $this->getTempnam("of", $this->getOutputFileExtension());
57+
$cmd = [
58+
$this->getOption('gifsicle', 'gifsicle'),
59+
$this->getOption('gifsicle_options', '-O3'),
60+
$if,
61+
'-o',
62+
$of,
63+
];
64+
$output = [];
65+
$ret = null;
66+
exec(implode(' ', $cmd) . ' 2>&1', $output, $ret);
67+
if ($ret !== 0) {
68+
throw new ImageOptimizerException(sprintf('Exec error gifsicle (%d): %s', $ret, implode("\n", $output)), ImageOptimizerException::EXEC_ERROR);
69+
}
70+
if (false === ($binaryContent = file_get_contents($of))) {
71+
throw new ImageOptimizerException(sprintf('Cannot read tmpfile : %s', $of), ImageOptimizerException::CANNOT_READ_TMPFILE);
72+
}
73+
unlink($of);
74+
return 'image/gif';
75+
}
76+
77+
}

Diff for: tests/ImageOptimizerTests.php

+23
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use ImageStack\Image;
55
use ImageStack\ImageOptimizer\JpegtranImageOptimizer;
66
use ImageStack\ImageOptimizer\PngcrushImageOptimizer;
7+
use ImageStack\ImageOptimizer\GifsicleImageOptimizer;
78

89
class ImageOptimizerTests extends \PHPUnit_Framework_TestCase
910
{
@@ -42,4 +43,26 @@ public function testPngcrushOptimizer()
4243

4344
$this->assertStringEqualsFile(__DIR__ . '/resources/optimizer/cat2_pngcrush.png', $image->getBinaryContent());
4445
}
46+
47+
public function testGifsicleOptimizer()
48+
{
49+
$image = new Image(file_get_contents(__DIR__ . '/resources/photos/eclipse256.gif'));
50+
51+
$optimizer = new GifsicleImageOptimizer();
52+
53+
$optimizer->optimizeImage($image);
54+
55+
$this->assertStringEqualsFile(__DIR__ . '/resources/optimizer/eclipse256.gif', $image->getBinaryContent());
56+
}
57+
58+
public function testGifsicleOptimizerAnim()
59+
{
60+
$image = new Image(file_get_contents(__DIR__ . '/resources/photos/animated.gif'));
61+
62+
$optimizer = new GifsicleImageOptimizer();
63+
64+
$optimizer->optimizeImage($image);
65+
66+
$this->assertStringEqualsFile(__DIR__ . '/resources/optimizer/animated.gif', $image->getBinaryContent());
67+
}
4568
}

Diff for: tests/resources/optimizer/animated.gif

895 KB
Loading

Diff for: tests/resources/optimizer/eclipse256.gif

10.2 KB
Loading

Diff for: tests/resources/photos/animated.gif

901 KB
Loading

Diff for: tests/resources/photos/eclipse256.gif

10.2 KB
Loading

0 commit comments

Comments
 (0)