1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-13 17:34:04 +02:00

new method "backup" and bugfixes

This commit is contained in:
Oliver Vogel
2014-02-11 17:27:23 +01:00
parent c56d00ff73
commit aa75c42975
2 changed files with 55 additions and 16 deletions

View File

@@ -242,12 +242,10 @@ class Image
*/ */
private function initEmpty($width, $height, $bgcolor = null) private function initEmpty($width, $height, $bgcolor = null)
{ {
// define width + height
$this->width = is_numeric($width) ? intval($width) : 1; $this->width = is_numeric($width) ? intval($width) : 1;
$this->height = is_numeric($height) ? intval($height) : 1; $this->height = is_numeric($height) ? intval($height) : 1;
$this->original['width'] = $this->width;
$this->original['height'] = $this->height;
// create empty image // create empty image
$this->resource = imagecreatetruecolor($this->width, $this->height); $this->resource = imagecreatetruecolor($this->width, $this->height);
@@ -259,8 +257,11 @@ class Image
$bgcolor = $this->parseColor($bgcolor); $bgcolor = $this->parseColor($bgcolor);
} }
// fill with background color
imagefill($this->resource, 0, 0, $bgcolor); imagefill($this->resource, 0, 0, $bgcolor);
$this->original['bgcolor'] = $bgcolor;
// save current state as original
$this->backup();
} }
/** /**
@@ -1385,6 +1386,16 @@ class Image
return $this; return $this;
} }
/**
* Set current image as original (reset will return to this)
*
* @return void
*/
public function backup()
{
$this->original = $this->cloneResource($this->resource);
}
/** /**
* Reset to original image resource * Reset to original image resource
* *
@@ -1392,13 +1403,8 @@ class Image
*/ */
public function reset() public function reset()
{ {
if (is_null($this->dirname) && is_null($this->basename)) { if ($this->isImageResource($this->original)) {
$this->initFromResource($this->original);
$this->initEmpty($this->original['width'], $this->original['height'], $this->original['bgcolor']);
} else {
$this->initFromPath($this->dirname .'/'. $this->basename);
} }
return $this; return $this;
@@ -1744,6 +1750,19 @@ class Image
return false; return false;
} }
/**
* Clones and returns cloned image resource
*
* @param Resource $resource
* @return Resource
*/
private function cloneResource($resource)
{
ob_start();
imagegd2($resource);
return imagecreatefromstring(ob_get_clean());
}
/** /**
* Set file info from image path in filesystem * Set file info from image path in filesystem
* *
@@ -1799,6 +1818,9 @@ class Image
); );
break; break;
} }
// save current state as original
$this->backup();
} }
/** /**
@@ -1811,8 +1833,9 @@ class Image
$this->resource = $resource; $this->resource = $resource;
$this->width = imagesx($this->resource); $this->width = imagesx($this->resource);
$this->height = imagesy($this->resource); $this->height = imagesy($this->resource);
$this->original['width'] = $this->width;
$this->original['height'] = $this->height; // save current state as original
$this->backup();
} }
/** /**
@@ -1836,8 +1859,9 @@ class Image
$this->mime = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $string); $this->mime = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $string);
$this->width = imagesx($this->resource); $this->width = imagesx($this->resource);
$this->height = imagesy($this->resource); $this->height = imagesy($this->resource);
$this->original['width'] = $this->width;
$this->original['height'] = $this->height; // save current state as original
$this->backup();
} }
/** /**

View File

@@ -1338,13 +1338,28 @@ class ImageTest extends PHPUnit_Framework_Testcase
public function testResetEmptyImage() public function testResetEmptyImage()
{ {
$img = new Image(null, 800, 600); $img = new Image(null, 800, 600, '#0000ff');
$img->resize(300, 200); $img->resize(300, 200);
$img->fill('#00ff00');
$img->reset(); $img->reset();
$this->assertInternalType('int', $img->width); $this->assertInternalType('int', $img->width);
$this->assertInternalType('int', $img->height); $this->assertInternalType('int', $img->height);
$this->assertEquals($img->width, 800); $this->assertEquals($img->width, 800);
$this->assertEquals($img->height, 600); $this->assertEquals($img->height, 600);
$this->assertEquals('#0000ff', $img->pickColor(0, 0, 'hex'));
}
public function testBackup()
{
$img = new Image(null, 800, 600, '#0000ff');
$img->fill('#00ff00');
$img->backup();
$img->reset();
$this->assertInternalType('int', $img->width);
$this->assertInternalType('int', $img->height);
$this->assertEquals($img->width, 800);
$this->assertEquals($img->height, 600);
$this->assertEquals('#00ff00', $img->pickColor(0, 0, 'hex'));
} }
public function testLimitColors() public function testLimitColors()