Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/Imagine/GD/Command/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ public function process(\Imagine\Image $image)
$path = $this->path ?: $image->getPath();
$saveParams = array_merge(array($image->getResource(), $path), $this->saveParams);

if(array_key_exists('quality', $saveParams)) {
$saveParams['quality'] = Utils::getSaveQuality($this->type ?: $image->getType(), $saveParams['quality']);
}

if (! is_dir($pathDir = dirname($path))) {
if (! @mkdir($pathDir, 0777, true)) {
throw new \RuntimeException('Cannot create directory: ' . $pathDir);
Expand Down
24 changes: 23 additions & 1 deletion lib/Imagine/GD/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,28 @@ public static function getSaveFunction($type)
}
}

/**
* Get the save quality for an image type.
*
* @param int $type
* @param int $quality between 0 and 100 inclusive
* @return int
* @throws \InvalidArgumentException
*/
public static function getSaveQuality($type, $quality)
{
if($quality < 0 || $quality > 100) {
throw new \InvalidArgumentException(sprintf('Save quality must be comprised between 0 and 100 inclusive, "%d" given'));
}

if(IMAGETYPE_PNG === $type) {
// Transform quality to PNG compression level
$quality = abs(($quality - 100) / 11.111111);
}

return round($quality);
}

/**
* Checks whether the parameter is a valid GD image resource.
*
Expand Down Expand Up @@ -93,4 +115,4 @@ public static function createResource($width, $height, $type = null)

return $resource;
}
}
}
64 changes: 62 additions & 2 deletions tests/Imagine/GD/Command/SaveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,69 @@

class SaveTest extends \PHPUnit_Framework_TestCase
{
const IMG_PNG = 'tests/fixtures/land.png';
const IMG_JPEG = 'tests/fixtures/land.jpg';

protected $png;
protected $jpeg;
protected $tmpFile;

public function setUp()
{
parent::setUp();
$this->png = new \Imagine\Image(self::IMG_PNG);
$this->jpeg = new \Imagine\Image(self::IMG_JPEG);
$this->tmpFile = sys_get_temp_dir().'/'.uniqid();
}

public function tearDown()
{
if(file_exists($this->tmpFile)) {
unlink($this->tmpFile);
}
}

public function testSave()
{
// TODO: implement unit tests for save command
$this->markTestIncomplete();
$this->assertFalse(file_exists($this->tmpFile));
$command = new Save($this->tmpFile);
$command->process($this->png);
$this->assertTrue(file_exists($this->tmpFile));
}

public function testSaveJpegWithHighQuality()
{
$originalFileSize = filesize(self::IMG_JPEG);
$command = new Save($this->tmpFile, null, array('quality' => 100));
$command->process($this->jpeg);
$newFileSize = filesize($this->tmpFile);
$this->assertGreaterThanOrEqual($originalFileSize, $newFileSize);
}

public function testSaveJpegWithLowQuality()
{
$originalFileSize = filesize(self::IMG_JPEG);
$command = new Save($this->tmpFile, null, array('quality' => 10));
$command->process($this->jpeg);
$newFileSize = filesize($this->tmpFile);
$this->assertLessThanOrEqual($originalFileSize, $newFileSize);
}

public function testSavePngWithHighQuality()
{
$originalFileSize = filesize(self::IMG_PNG);
$command = new Save($this->tmpFile, null, array('quality' => 100));
$command->process($this->png);
$newFileSize = filesize($this->tmpFile);
$this->assertGreaterThanOrEqual($originalFileSize, $newFileSize);
}

public function testSavePngWithLowQuality()
{
$originalFileSize = filesize(self::IMG_PNG);
$command = new Save($this->tmpFile, null, array('quality' => 0));
$command->process($this->png);
$newFileSize = filesize($this->tmpFile);
$this->assertLessThanOrEqual($originalFileSize, $newFileSize);
}
}
21 changes: 21 additions & 0 deletions tests/Imagine/GD/UtilsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Imagine\GD;

require_once 'tests/Imagine/TestInit.php';

class UtilsTest extends \PHPUnit_Framework_TestCase
{
public function testSaveQualityJpeg()
{
$this->assertEquals(20, Utils::getSaveQuality(IMAGETYPE_JPEG, 20));
$this->assertEquals(100, Utils::getSaveQuality(IMAGETYPE_JPEG, 100));
}

public function testSaveQualityPng()
{
$this->assertEquals(7, Utils::getSaveQuality(IMAGETYPE_PNG, 20));
$this->assertEquals(9, Utils::getSaveQuality(IMAGETYPE_PNG, 0));
$this->assertEquals(0, Utils::getSaveQuality(IMAGETYPE_PNG, 100));
}
}
Binary file added tests/fixtures/land.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/fixtures/land.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.