Skip to content

Commit

Permalink
ImageMagick class updates and tests (#3115)
Browse files Browse the repository at this point in the history
ImageMagick class updates and tests
  • Loading branch information
michalsn authored Jun 18, 2020
1 parent f0e6f60 commit a4917b1
Show file tree
Hide file tree
Showing 3 changed files with 401 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ before_install:
- mysql -e "CREATE DATABASE IF NOT EXISTS test;" -uroot;
- mysql -e "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'test';" -uroot
- psql -c 'CREATE DATABASE test;' -U postgres
- sudo apt-get install ghostscript
- yes | pecl install imagick

before_script:
- echo 'extension = memcached.so' >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
Expand Down
31 changes: 22 additions & 9 deletions system/Images/Handlers/ImageMagickHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ public function _resize(bool $maintainRatio = false)
$escape = '';
}

$action = $maintainRatio === true ? ' -resize ' . $this->width . 'x' . $this->height . ' "' . $source . '" "' . $destination . '"' : ' -resize ' . $this->width . 'x' . $this->height . "{$escape}! \"" . $source . '" "' . $destination . '"';
$action = $maintainRatio === true
? ' -resize ' . ($this->width ?? 0) . 'x' . ($this->height ?? 0) . ' "' . $source . '" "' . $destination . '"'
: ' -resize ' . ($this->width ?? 0) . 'x' . ($this->height ?? 0) . "{$escape}! \"" . $source . '" "' . $destination . '"';

$this->process($action);

Expand All @@ -126,7 +128,13 @@ public function _crop()
$source = ! empty($this->resource) ? $this->resource : $this->image()->getPathname();
$destination = $this->getResourcePath();

$action = ' -crop ' . $this->width . 'x' . $this->height . '+' . $this->xAxis . '+' . $this->yAxis . ' "' . $source . '" "' . $destination . '"';
$extent = ' ';
if ($this->xAxis >= $this->width || $this->yAxis > $this->height)
{
$extent = ' -background transparent -extent ' . ($this->width ?? 0) . 'x' . ($this->height ?? 0) . ' ';
}

$action = ' -crop ' . ($this->width ?? 0) . 'x' . ($this->height ?? 0) . '+' . ($this->xAxis ?? 0) . '+' . ($this->yAxis ?? 0) . $extent . escapeshellarg($source) . ' ' . escapeshellarg($destination);

$this->process($action);

Expand All @@ -151,7 +159,7 @@ protected function _rotate(int $angle)
$source = ! empty($this->resource) ? $this->resource : $this->image()->getPathname();
$destination = $this->getResourcePath();

$action = ' ' . $angle . ' "' . $source . '" "' . $destination . '"';
$action = ' ' . $angle . ' ' . escapeshellarg($source) . ' ' . escapeshellarg($destination);

$this->process($action);

Expand All @@ -172,12 +180,12 @@ protected function _rotate(int $angle)
*/
public function _flatten(int $red = 255, int $green = 255, int $blue = 255)
{
$flatten = "-background RGB({$red},{$green},{$blue}) -flatten";
$flatten = "-background 'rgb({$red},{$green},{$blue})' -flatten";

$source = ! empty($this->resource) ? $this->resource : $this->image()->getPathname();
$destination = $this->getResourcePath();

$action = ' ' . $flatten . ' "' . $source . '" "' . $destination . '"';
$action = ' ' . $flatten . ' ' . escapeshellarg($source) . ' ' . escapeshellarg($destination);

$this->process($action);

Expand All @@ -201,7 +209,7 @@ public function _flip(string $direction)
$source = ! empty($this->resource) ? $this->resource : $this->image()->getPathname();
$destination = $this->getResourcePath();

$action = ' ' . $angle . ' "' . $source . '" "' . $destination . '"';
$action = ' ' . $angle . ' ' . escapeshellarg($source) . ' ' . escapeshellarg($destination);

$this->process($action);

Expand Down Expand Up @@ -300,7 +308,7 @@ public function save(string $target = null, int $quality = 90): bool

// Copy the file through ImageMagick so that it has
// a chance to convert file format.
$action = '"' . $this->resource . '" "' . $target . '"';
$action = escapeshellarg($this->resource) . ' ' . escapeshellarg($target);

$result = $this->process($action, $quality);

Expand Down Expand Up @@ -335,6 +343,11 @@ protected function getResourcePath()

$this->resource = WRITEPATH . 'cache/' . time() . '_' . bin2hex(random_bytes(10)) . '.png';

$name = basename($this->resource);
$path = pathinfo($this->resource, PATHINFO_DIRNAME);

$this->image()->copy($path, $name);

return $this->resource;
}

Expand Down Expand Up @@ -462,7 +475,7 @@ protected function _text(string $text, array $options = [])
*/
public function _getWidth()
{
return imagesx($this->resource);
return imagesx(imagecreatefromstring(file_get_contents($this->resource)));
}

/**
Expand All @@ -472,7 +485,7 @@ public function _getWidth()
*/
public function _getHeight()
{
return imagesy($this->resource);
return imagesy(imagecreatefromstring(file_get_contents($this->resource)));
}

}
Loading

0 comments on commit a4917b1

Please sign in to comment.