Merge pull request #3268 from michalsn/fix/image_save

Fix Image::save() when target value is null
This commit is contained in:
Michal Sniatala 2020-07-11 16:04:02 +02:00 committed by GitHub
commit ee0c554f81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 64 additions and 6 deletions

View File

@ -278,18 +278,26 @@ class GDHandler extends BaseHandler
*/
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:

View File

@ -299,18 +299,26 @@ class ImageMagickHandler extends BaseHandler
*/
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);

View File

@ -331,17 +331,38 @@ class GDHandlerTest extends \CodeIgniter\Test\CIUnitTestCase
{
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)

View File

@ -327,17 +327,38 @@ class ImageMagickHandlerTest extends \CodeIgniter\Test\CIUnitTestCase
{
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)