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
8 changes: 6 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: php

sudo: false
sudo: required

branches:
only:
Expand All @@ -11,7 +11,7 @@ cache:
- $HOME/.composer/cache

php:
- 5.3.3
- 5.3
- 5.4
- 5.5
- 5.6
Expand All @@ -25,6 +25,10 @@ matrix:
- php: hhvm

before_install:
- sudo add-apt-repository -y ppa:moti-p/cc
- sudo apt-get update
- sudo apt-get -y --reinstall install imagemagick
- if [ "$TRAVIS_PHP_VERSION" = "hhvm" ]; then sudo apt-get -y --reinstall install libmagickwand4; fi;
- composer self-update

install:
Expand Down
14 changes: 11 additions & 3 deletions Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
*/
class Command
{
const RUN_NORMAL = null;
const RUN_BACKGROUND = ' > /dev/null 2>&1';
const RUN_DEBUG = ' 2>&1';

/**
* @var array The list of allowed ImageMagick binaries
Expand Down Expand Up @@ -105,13 +108,18 @@ public function __construct($imageMagickPath = '/usr/bin', $referencesDirectory
/**
* Executes the command and returns its response
*
* @param bool $getErrors If set to true, STDERR will be redirected to STDOUT
* @param null $runMode
*
* @return CommandResponse
*
*/
public function run($getErrors = false)
public function run($runMode = self::RUN_NORMAL)
{
exec($this->env.' '.$this->command.' '.$this->commandToAppend.($getErrors?' 2>&1':''), $output, $code);
if (!in_array($runMode, array(self::RUN_NORMAL, self::RUN_BACKGROUND, self::RUN_DEBUG))) {
throw new \InvalidArgumentException('The run mode must be one of '.__CLASS__.'::RUN_* constants.');
}

exec($this->env.' '.$this->command.' '.$this->commandToAppend.$runMode, $output, $code);

return new CommandResponse($output, $code);
}
Expand Down
74 changes: 56 additions & 18 deletions ReferenceClasses/Geometry.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@
* Represents an ImageMagick geometry parameter.
* Each value is optional so a big regex is used to parse it.
*
* How to build the monstruous regexp? Check the gist link.
*
* @link https://gist.github.com/Pierstoval/eac8d182d2c51c93202f
* @link http://www.imagemagick.org/script/command-line-processing.php#geometry
*/
class Geometry
{
const REGEX_VALIDATE = '~^(?:x?[0-9]+(?:\.[0-9]+)?%?|[0-9]+(?:\.[0-9]+)?%?x[0-9]+(?:\.[0-9]+)?%?[\^!<>]?)?(?:[+-][0-9]+[+-][0-9]+)?$~';
const REGEX_VALIDATE = '~(?<size>(?<w>(?:\d*(?:\.\d+)?)?%?)?(?:(?<whseparator>x)(?<h>(?:\d*(?:\.\d+)?)?%?))?)(?<aspect>[!><@^])?(?<offset>(?<x>[+-]\d*(?:\.\d+)?)?(?<y>[+-]\d*(?:\.\d+)?)?)~';

const RATIO_NONE = null;
const RATIO_MIN = '^';
Expand All @@ -33,17 +36,6 @@ class Geometry
*/
private $value = '';

public function __construct($width = null, $height = null, $x = null, $y = null, $aspectRatio = self::RATIO_NONE)
{
$args = func_get_args();
if (count(array_map(null, $args)) > 1) {
$geometry = call_user_func_array(array($this, 'createFromParameters'), $args);
} else {
$geometry = $width;
}
$this->value = $geometry;
}

/**
* @param string|int $width Can be both
* @param string|int $height
Expand All @@ -56,8 +48,17 @@ public function __construct($width = null, $height = null, $x = null, $y = null,
public static function createFromParameters($width = null, $height = null, $x = null, $y = null, $aspectRatio = self::RATIO_NONE)
{
$geometry = $width;

// If we have a height
// Or if we have width, no height and an offset
// If width is 100, it will result in 100x{offset}
// else, 100{offset} is incorrect
if (null !== $height || $width && !$height && (null !== $x || null !== $y)) {
$geometry .= 'x';
}

if (null !== $height) {
$geometry .= 'x'.$height;
$geometry .= $height;
}

if ($aspectRatio && !in_array($aspectRatio, self::$validRatios)) {
Expand All @@ -82,19 +83,56 @@ public static function createFromParameters($width = null, $height = null, $x =
return $geometry;
}

public function __construct($width = null, $height = null, $x = null, $y = null, $aspectRatio = self::RATIO_NONE)
{
$args = func_get_args();
if (count(array_map(null, $args)) > 1) {
$geometry = call_user_func_array(array($this, 'createFromParameters'), $args);
} else {
$geometry = $width;
}
$this->value = $geometry;
}

/**
* @throws \InvalidArgumentException
* @return string
*/
public function validate()
{
if (
!preg_match(static::REGEX_VALIDATE, $this->value)
) {
$errors = array();

if (!preg_match(static::REGEX_VALIDATE, $this->value, $matches)) {
$errors[] = 'Invalid regexp.';
}

$w = isset($matches['w']) && '' !== $matches['w'] ? $matches['w'] : null;
$h = isset($matches['h']) && '' !== $matches['h'] ? $matches['h'] : null;
$x = isset($matches['x']) && '' !== $matches['x'] ? $matches['x'] : null;
$y = isset($matches['y']) && '' !== $matches['y'] ? $matches['y'] : null;
$offset = isset($matches['offset']) && '' !== $matches['offset'] ? $matches['offset'] : null;
$whseparator = $matches['whseparator'];
$aspect = $matches['aspect'];

// The next checks will perform post-regexp matching that is impossible with preg_match natively

if ('0' === $w || '0' === $h) {
$errors[] = 'Cannot specify zero width nor height.';
}
if ($aspect && !$w && !$h) {
$errors[] = 'Aspect can be used only with width and/or height.';
}

if ($w && !$h && ($x || $y) && !$whseparator) {
$errors[] = 'When using offsets and only width, you must specify the "x" separator like this: '.$w.'x'.$offset;
}

if (count($errors)) {
throw new \InvalidArgumentException(sprintf(
"The specified geometry (%s) is invalid.\n".
"Please refer to ImageMagick command line documentation about geometry:\n%s",
"The specified geometry (%s) is invalid.\n%s\n".
"Please refer to ImageMagick command line documentation about geometry:\n%s\n",
$this->value,
implode("\n", $errors),
'http://www.imagemagick.org/script/command-line-processing.php#geometry'
));
}
Expand Down
40 changes: 40 additions & 0 deletions Tests/AbstractTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/*
* This file is part of the OrbitaleImageMagickPHP package.
*
* (c) Alexandre Rock Ancelet <alex@orbitale.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Orbitale\Component\ImageMagick\Tests;

class AbstractTestCase extends \PHPUnit_Framework_TestCase
{

protected $resourcesDir;

public function __construct($name = null, array $data = array(), $dataName = '')
{
parent::__construct($name, $data, $dataName);
if (!defined('IMAGEMAGICK_DIR') || !defined('TEST_RESOURCES_DIR')) {
throw new \RuntimeException(
"The \"IMAGEMAGICK_DIR\" constant is not defined.\n" .
"The bootstrap must be correctly included before executing test suite."
);
}
$this->resourcesDir = TEST_RESOURCES_DIR;
}

public function setUp()
{
$dir = TEST_RESOURCES_DIR.'/outputs';
foreach (scandir($dir) as $file) {
if ('.' !== $file && '..' !== $file && '.gitkeep' !== $file) {
unlink($dir.'/'.$file);
}
}
}

}
26 changes: 1 addition & 25 deletions Tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,9 @@

use Orbitale\Component\ImageMagick\Command;

class CommandTest extends \PHPUnit_Framework_TestCase
class CommandTest extends AbstractTestCase
{

private $resourcesDir;

public function __construct($name = null, array $data = array(), $dataName = '')
{
parent::__construct($name, $data, $dataName);
if (!defined('IMAGEMAGICK_DIR') || !defined('TEST_RESOURCES_DIR')) {
throw new \RuntimeException(
"The \"IMAGEMAGICK_DIR\" constant is not defined.\n" .
"The bootstrap must be correctly included before executing test suite."
);
}
$this->resourcesDir = TEST_RESOURCES_DIR;
}

public function setUp()
{
$dir = TEST_RESOURCES_DIR.'/outputs';
foreach (scandir($dir) as $file) {
if ('.' !== $file && '..' !== $file && '.gitkeep' !== $file) {
unlink($dir.'/'.$file);
}
}
}

/**
* @dataProvider provideWrongConvertDirs
*/
Expand Down
Loading