mirror of
https://github.com/codeigniter4/CodeIgniter4.git
synced 2025-02-20 11:44:28 +08:00
Merge pull request #3268 from michalsn/fix/image_save
Fix Image::save() when target value is null
This commit is contained in:
commit
ee0c554f81
@ -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:
|
||||
|
@ -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);
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user