Permalink
Browse files

Adding destroy() method to JImage

Now with 100% MORE TESTS

Adding the ability for the code to self execute and automatically destroy the image handle

adding destroy() method to crop and resize as well

Fix UT Failures

Moving the destroy method

Fix CS Errors
  • Loading branch information...
1 parent d461629 commit 901833a9a47dcca2794813b6e3fe25963dac3e2e @dongilbert committed Nov 18, 2012
Showing with 60 additions and 0 deletions.
  1. +41 −0 libraries/joomla/image/image.php
  2. +19 −0 tests/suites/unit/joomla/image/JImageTest.php
@@ -360,6 +360,9 @@ public function crop($width, $height, $left = null, $top = null, $createNew = tr
// Swap out the current handle for the new image handle.
else
{
+ // Free the memory from the current handle
+ $this->destroy();
+
$this->handle = $handle;
return $this;
@@ -496,6 +499,9 @@ public function isTransparent()
*/
public function loadFile($path)
{
+ // Destroy the current image handle if it exists
+ $this->destroy();
+
// Make sure the file exists.
if (!file_exists($path))
{
@@ -658,6 +664,9 @@ public function resize($width, $height, $createNew = true, $scaleMethod = self::
// Swap out the current handle for the new image handle.
else
{
+ // Free the memory from the current handle
+ $this->destroy();
+
$this->handle = $handle;
return $this;
@@ -714,6 +723,9 @@ public function rotate($angle, $background = -1, $createNew = true)
// Swap out the current handle for the new image handle.
else
{
+ // Free the memory from the current handle
+ $this->destroy();
+
$this->handle = $handle;
return $this;
@@ -929,4 +941,33 @@ protected function sanitizeWidth($width, $height)
return $width;
}
+
+ /**
+ * Method to destroy an image handle and
+ * free the memory associated with the handle
+ *
+ * @return boolean True on success, false on failure or if no image is loaded
+ *
+ * @since 12.3
+ */
+ public function destroy()
+ {
+ if ($this->isLoaded())
+ {
+ return imagedestroy($this->handle);
+ }
+
+ return false;
+ }
+
+ /**
+ * Method to call the destroy() method one last time
+ * to free any memory when the object is unset
+ *
+ * @see JImage::destroy()
+ */
+ public function __destruct()
+ {
+ $this->destroy();
+ }
}
@@ -964,4 +964,23 @@ public function testSanitizeOffset($input, $expected)
// Validate the correct response.
$this->assertEquals($expected, $image->sanitizeOffset($input));
}
+
+ /**
+ * Tests the JImage::destory method
+ *
+ * @return void
+ *
+ * @since 12.3
+ */
+ public function testDestroy()
+ {
+ // Create an image handle
+ $imageHandle = imagecreatetruecolor(100, 100);
+
+ // Pass created handle to JImage
+ $image = new JImage($imageHandle);
+
+ // Destroying the image should return boolean true
+ $this->assertTrue($image->destroy());
+ }
}

0 comments on commit 901833a

Please sign in to comment.