Skip to content

Commit

Permalink
Merge pull request #3268 from michalsn/fix/image_save
Browse files Browse the repository at this point in the history
Fix Image::save() when target value is null
  • Loading branch information
michalsn authored Jul 11, 2020
2 parents 246ab82 + 8f9ef32 commit ee0c554
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 6 deletions.
12 changes: 10 additions & 2 deletions system/Images/Handlers/GDHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,18 +278,26 @@ protected function process(string $action)
*/
public function save(string $target = null, int $quality = 90): bool
{
$target = empty($target) ? $this->image()->getPathname() : $target;
$original = $target;
$target = empty($target) ? $this->image()->getPathname() : $target;

// If no new resource has been created, then we're
// simply copy the existing one.
if (empty($this->resource))
if (empty($this->resource) && $quality === 100)
{
if ($original === null)
{
return true;
}

$name = basename($target);
$path = pathinfo($target, PATHINFO_DIRNAME);

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

$this->ensureResource();

switch ($this->image()->imageType)
{
case IMAGETYPE_GIF:
Expand Down
12 changes: 10 additions & 2 deletions system/Images/Handlers/ImageMagickHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,18 +299,26 @@ protected function process(string $action, int $quality = 100): array
*/
public function save(string $target = null, int $quality = 90): bool
{
$target = empty($target) ? $this->image() : $target;
$original = $target;
$target = empty($target) ? $this->image()->getPathname() : $target;

// If no new resource has been created, then we're
// simply copy the existing one.
if (empty($this->resource))
if (empty($this->resource) && $quality === 100)
{
if ($original === null)
{
return true;
}

$name = basename($target);
$path = pathinfo($target, PATHINFO_DIRNAME);

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

$this->ensureResource();

// Copy the file through ImageMagick so that it has
// a chance to convert file format.
$action = escapeshellarg($this->resource) . ' ' . escapeshellarg($target);
Expand Down
23 changes: 22 additions & 1 deletion tests/system/Images/GDHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,17 +331,38 @@ public function testImageCopy()
{
foreach (['gif', 'jpeg', 'png', 'webp'] as $type)
{
if ($type === 'webp' && ! function_exists('imagecreatefromwebp'))
{
$this->expectException(ImageException::class);
$this->expectExceptionMessage('Your server does not support the GD function required to process this type of image.');
}

$this->handler->withFile($this->origin . 'ci-logo.' . $type);
$this->handler->save($this->start . 'work/ci-logo.' . $type);
$this->assertTrue($this->root->hasChild('work/ci-logo.' . $type));

$this->assertEquals(
$this->assertNotEquals(
file_get_contents($this->origin . 'ci-logo.' . $type),
$this->root->getChild('work/ci-logo.' . $type)->getContent()
);
}
}

public function testImageCopyWithNoTargetAndMaxQuality()
{
foreach (['gif', 'jpeg', 'png', 'webp'] as $type)
{
$this->handler->withFile($this->origin . 'ci-logo.' . $type);
$this->handler->save(null, 100);
$this->assertTrue(file_exists($this->origin . 'ci-logo.' . $type));

$this->assertEquals(
file_get_contents($this->origin . 'ci-logo.' . $type),
file_get_contents($this->origin . 'ci-logo.' . $type)
);
}
}

public function testImageCompressionGetResource()
{
foreach (['gif', 'jpeg', 'png', 'webp'] as $type)
Expand Down
23 changes: 22 additions & 1 deletion tests/system/Images/ImageMagickHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,17 +327,38 @@ public function testImageCopy()
{
foreach (['gif', 'jpeg', 'png', 'webp'] as $type)
{
if ($type === 'webp' && ! in_array('WEBP', \Imagick::queryFormats()))
{
$this->expectException(ImageException::class);
$this->expectExceptionMessage('Your server does not support the GD function required to process this type of image.');
}

$this->handler->withFile($this->origin . 'ci-logo.' . $type);
$this->handler->save($this->root . 'ci-logo.' . $type);
$this->assertTrue(file_exists($this->root . 'ci-logo.' . $type));

$this->assertEquals(
$this->assertNotEquals(
file_get_contents($this->origin . 'ci-logo.' . $type),
file_get_contents($this->root . 'ci-logo.' . $type)
);
}
}

public function testImageCopyWithNoTargetAndMaxQuality()
{
foreach (['gif', 'jpeg', 'png', 'webp'] as $type)
{
$this->handler->withFile($this->origin . 'ci-logo.' . $type);
$this->handler->save(null, 100);
$this->assertTrue(file_exists($this->origin . 'ci-logo.' . $type));

$this->assertEquals(
file_get_contents($this->origin . 'ci-logo.' . $type),
file_get_contents($this->origin . 'ci-logo.' . $type)
);
}
}

public function testImageCompressionGetResource()
{
foreach (['gif', 'jpeg', 'png', 'webp'] as $type)
Expand Down

0 comments on commit ee0c554

Please sign in to comment.