Skip to content

Commit

Permalink
Merge pull request #2963 from jreklund/image-copy
Browse files Browse the repository at this point in the history
Improvements to Image Manipulation Class
lonnieezell authored May 11, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents 46a5ba2 + 08a971f commit 0752ac4
Showing 5 changed files with 111 additions and 22 deletions.
36 changes: 16 additions & 20 deletions system/Images/Handlers/BaseHandler.php
Original file line number Diff line number Diff line change
@@ -181,26 +181,7 @@ public function withFile(string $path)
/**
* Make the image resource object if needed
*/
protected function ensureResource()
{
if ($this->resource === null)
{
$path = $this->image()->getPathname();
// if valid image type, make corresponding image resource
switch ($this->image()->imageType)
{
case IMAGETYPE_GIF:
$this->resource = imagecreatefromgif($path);
break;
case IMAGETYPE_JPEG:
$this->resource = imagecreatefromjpeg($path);
break;
case IMAGETYPE_PNG:
$this->resource = imagecreatefrompng($path);
break;
}
}
}
protected abstract function ensureResource();

//--------------------------------------------------------------------

@@ -268,6 +249,21 @@ public function getResource()

//--------------------------------------------------------------------

/**
* Load the temporary image used during the image processing.
* Some functions e.g. save() will only copy and not compress
* your image otherwise.
*
* @return $this
*/
public function withResource()
{
$this->ensureResource();
return $this;
}

//--------------------------------------------------------------------

/**
* Resize the image
*
36 changes: 36 additions & 0 deletions system/Images/Handlers/GDHandler.php
Original file line number Diff line number Diff line change
@@ -320,6 +320,16 @@ public function save(string $target = null, int $quality = 90): bool
{
$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))
{
$name = basename($target);
$path = pathinfo($target, PATHINFO_DIRNAME);

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

switch ($this->image()->imageType)
{
case IMAGETYPE_GIF:
@@ -426,6 +436,32 @@ protected function createImage(string $path = '', string $imageType = '')

//--------------------------------------------------------------------

/**
* Make the image resource object if needed
*/
protected function ensureResource()
{
if ($this->resource === null)
{
$path = $this->image()->getPathname();
// if valid image type, make corresponding image resource
switch ($this->image()->imageType)
{
case IMAGETYPE_GIF:
$this->resource = imagecreatefromgif($path);
break;
case IMAGETYPE_JPEG:
$this->resource = imagecreatefromjpeg($path);
break;
case IMAGETYPE_PNG:
$this->resource = imagecreatefrompng($path);
break;
}
}
}

//--------------------------------------------------------------------

/**
* Add text overlay to an image.
*
12 changes: 12 additions & 0 deletions system/Images/Handlers/ImageMagickHandler.php
Original file line number Diff line number Diff line change
@@ -340,6 +340,18 @@ protected function getResourcePath()

//--------------------------------------------------------------------

/**
* Make the image resource object if needed
*
* @throws \Exception
*/
protected function ensureResource()
{
$this->getResourcePath();
}

//--------------------------------------------------------------------

/**
* Handler-specific method for overlaying text on an image.
*
39 changes: 38 additions & 1 deletion tests/system/Images/GDHandlerTest.php
Original file line number Diff line number Diff line change
@@ -321,14 +321,51 @@ public function testImageCreation()

//--------------------------------------------------------------------

public function testImageSave()
public function testImageCopy()
{
foreach (['gif', 'jpeg', 'png'] as $type)
{
$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(
file_get_contents($this->origin . 'ci-logo.' . $type),
$this->root->getChild('work/ci-logo.' . $type)->getContent()
);
}
}

public function testImageCompressionGetResource()
{
foreach (['gif', 'jpeg', 'png'] as $type)
{
$this->handler->withFile($this->origin . 'ci-logo.' . $type);
$this->handler->getResource(); // make sure resource is loaded
$this->handler->save($this->start . 'work/ci-logo.' . $type);
$this->assertTrue($this->root->hasChild('work/ci-logo.' . $type));

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

public function testImageCompressionWithResource()
{
foreach (['gif', 'jpeg', 'png'] as $type)
{
$this->handler->withFile($this->origin . 'ci-logo.' . $type)
->withResource() // make sure resource is loaded
->save($this->start . 'work/ci-logo.' . $type);

$this->assertTrue($this->root->hasChild('work/ci-logo.' . $type));

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

10 changes: 9 additions & 1 deletion user_guide_src/source/libraries/images.rst
Original file line number Diff line number Diff line change
@@ -98,10 +98,19 @@ only applies to JPEG images and will be ignored otherwise::

$image = \Config\Services::image()
->withFile('/path/to/image/mypic.jpg')
// processing methods
->save('/path/to/image/my_low_quality_pic.jpg', 10);

.. note:: Higher quality will result in larger file sizes. See also https://www.php.net/manual/en/function.imagejpeg.php

If you are only interested in changing the image quality without doing any processing.
You will need to include the image resource or you will end up with an exact copy::

$image = \Config\Services::image()
->withFile('/path/to/image/mypic.jpg')
->withResource()
->save('/path/to/image/my_low_quality_pic.jpg', 10);

Processing Methods
==================

@@ -324,4 +333,3 @@ The possible options that are recognized are as follows:

.. note:: The ImageMagick driver does not recognize full server path for fontPath. Instead, simply provide the
name of one of the installed system fonts that you wish to use, i.e. Calibri.

0 comments on commit 0752ac4

Please sign in to comment.