Skip to content

Commit

Permalink
Merge pull request #68 from jdecool/save-error-management
Browse files Browse the repository at this point in the history
Throw an exception when save process failed
  • Loading branch information
jdecool committed Mar 27, 2015
2 parents 72c4b37 + 8a163e6 commit 92c8c6e
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 31 deletions.
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
"name": "Clément Guillemain",
"homepage": "http://clementguillemain.fr",
"role": "Developer / Freelancer"
},
{
"name": "ImageWorkshop Community",
"homepage": "https://github.com/Sybio/ImageWorkshop/graphs/contributors"
}
],
"require": {
Expand Down
92 changes: 62 additions & 30 deletions src/PHPImageWorkshop/Core/ImageWorkshopLayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,21 @@ class ImageWorkshopLayer
* @var integer
*/
const ERROR_NEGATIVE_NUMBER_USED = 5;

/**
* @var integer
*/
const ERROR_NOT_WRITABLE_FOLDER = 6;

/**
* @var integer
*/
const ERROR_NOT_SUPPORTED_FORMAT = 7;

/**
* @var integer
*/
const ERROR_UNKNOW = 8;

// ===================================================================================
// Methods
Expand Down Expand Up @@ -1497,51 +1512,68 @@ public function getResult($backgroundColor = null)
*/
public function save($folder, $imageName, $createFolders = true, $backgroundColor = null, $imageQuality = 75, $interlace = false)
{
if (!is_file($folder)) {
if (is_file($folder)) {
throw new ImageWorkshopLayerException(sprintf('Destination folder "%s" is a file.', $folder), self::ERROR_NOT_WRITABLE_FOLDER);
}

if (is_dir($folder) || $createFolders) {
if ((!is_dir($folder) && !$createFolders)) {
throw new ImageWorkshopLayerException(sprintf('Destination folder "%s" not exists.', $folder), self::ERROR_NOT_WRITABLE_FOLDER);
}

// Creating the folders if they don't exist
if (!is_dir($folder) && $createFolders) {
$oldUmask = umask(0);
mkdir($folder, 0777, true);
umask($oldUmask);
chmod($folder, 0777);
}
if (is_dir($folder) && !is_writable($folder)) {
throw new ImageWorkshopLayerException(sprintf('Destination folder "%s" not writable.', $folder), self::ERROR_NOT_WRITABLE_FOLDER);
}

$extension = explode('.', $imageName);
$extension = strtolower($extension[count($extension) - 1]);
// Creating the folders if they don't exist
if (!is_dir($folder) && $createFolders) {
if (!mkdir($folder, 0777, true)) {
throw new ImageWorkshopLayerException(sprintf('Unable to create destination folder "%s".', $folder), self::ERROR_NOT_WRITABLE_FOLDER);
}

$oldUmask = umask(0);
umask($oldUmask);
chmod($folder, 0777);
}

$filename = $folder.'/'.$imageName;
$extension = explode('.', $imageName);
$extension = strtolower($extension[count($extension) - 1]);

if (($extension == 'jpg' || $extension == 'jpeg' || $extension == 'gif') && (!$backgroundColor || $backgroundColor == 'transparent')) {
$backgroundColor = 'ffffff';
}
$filename = $folder.'/'.$imageName;

$image = $this->getResult($backgroundColor);
if (($extension == 'jpg' || $extension == 'jpeg' || $extension == 'gif') && (!$backgroundColor || $backgroundColor == 'transparent')) {
$backgroundColor = 'ffffff';
}

imageinterlace($image, (int) $interlace);
$image = $this->getResult($backgroundColor);

if ($extension == 'jpg' || $extension == 'jpeg') {
imageinterlace($image, (int) $interlace);

imagejpeg($image, $filename, $imageQuality);
unset($image);
if ($extension == 'jpg' || $extension == 'jpeg') {

} elseif ($extension == 'gif') {
$isSaved = imagejpeg($image, $filename, $imageQuality);

imagegif($image, $filename);
unset($image);
} elseif ($extension == 'gif') {

} elseif ($extension == 'png') {
$isSaved = imagegif($image, $filename);

$imageQuality = $imageQuality / 10;
$imageQuality -= 1;
} elseif ($extension == 'png') {

$imageQuality = $imageQuality / 10;
$imageQuality -= 1;

$isSaved = imagepng($image, $filename, intval($imageQuality));

} else {

throw new ImageWorkshopLayerException(sprintf('Image format "%s" not supported.', $extension), self::ERROR_NOT_SUPPORTED_FORMAT);

imagepng($image, $filename, $imageQuality);
unset($image);
}
}
}

if (!$isSaved) {
throw new ImageWorkshopLayerException(sprintf('Error occurs when save image "%s".', $folder), self::ERROR_UNKNOW);
}

unset($image);
}

// Checkers
Expand Down
77 changes: 76 additions & 1 deletion tests/Core/ImageWorkshopLayerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,23 @@
*/
class ImageWorkshopLayerTest extends \PHPUnit_Framework_TestCase
{
/** @var string */
protected $workspace = null;

protected function setUp()
{
$this->umask = umask(0);
$this->workspace = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.time().rand(0, 1000);
mkdir($this->workspace, 0777, true);
$this->workspace = realpath($this->workspace);
}

protected function tearDown()
{
$this->clean($this->workspace);
umask($this->umask);
}

// Tests
// ===================================================================================

Expand Down Expand Up @@ -1424,7 +1441,49 @@ public function testRotate()
$this->assertTrue($layer->getWidth() <= 121 && $layer->getWidth() >= 119, 'Expect $layer to have a width around 120px');
$this->assertTrue($layer->getHeight() <= 107 && $layer->getHeight() >= 105, 'Expect $layer to have a height around 106px');
}


public function testSaveWithDirectoryAsFile()
{
$destinationFolder = $this->workspace.DIRECTORY_SEPARATOR.'fileDestination';

$this->setExpectedException(
'PHPImageWorkshop\Core\Exception\ImageWorkshopLayerException',
'Destination folder "'.$destinationFolder.'" is a file.',
6
);

touch($destinationFolder);

$layer = $this->initializeLayer();
$layer->save($destinationFolder, 'test.png', false);
}

public function testSaveWithNonExistDirectory()
{
$destinationFolder = $this->workspace.DIRECTORY_SEPARATOR.'nonExistFolder';

$this->setExpectedException(
'PHPImageWorkshop\Core\Exception\ImageWorkshopLayerException',
'Destination folder "'.$destinationFolder.'" not exists.',
6
);

$layer = $this->initializeLayer();
$layer->save($destinationFolder, 'test.png', false);
}

public function testSaveWithNonSupportedFileExtension()
{
$this->setExpectedException(
'PHPImageWorkshop\Core\Exception\ImageWorkshopLayerException',
'Image format "tif" not supported.',
7
);

$layer = $this->initializeLayer();
$layer->save($this->workspace, 'test.tif', false);
}

// Internals
// ===================================================================================

Expand Down Expand Up @@ -1465,4 +1524,20 @@ protected function initializeLayer($method = 1)

return $layer;
}

/**
* @param string $file
*/
protected function clean($file)
{
if (is_dir($file) && !is_link($file)) {
$dir = new \FilesystemIterator($file);
foreach ($dir as $childFile) {
$this->clean($childFile);
}
rmdir($file);
} else {
unlink($file);
}
}
}

0 comments on commit 92c8c6e

Please sign in to comment.