1
0
mirror of https://github.com/Intervention/image.git synced 2025-01-17 20:28:21 +01:00

Optimized trim

This commit is contained in:
Oliver Vogel 2014-03-25 20:45:09 +01:00
parent fd955c0658
commit 9c05f9fd41
2 changed files with 166 additions and 80 deletions

View File

@ -740,6 +740,11 @@ class Image
$away = array($away);
}
// lower border names
foreach ($away as $key => $value) {
$away[$key] = strtolower($value);
}
// define base color position
switch (strtolower($base)) {
case 'transparent':
@ -766,61 +771,60 @@ class Image
// pick base color
$color = $this->pickColor($base_x, $base_y, 'array');
// search for values that are not base color
$x_values = array();
$y_values = array();
$top_x = 0;
$top_y = 0;
$bottom_x = $this->width;
$bottom_y = $this->height;
for ($y=0; $y < $this->height; $y++) {
if (in_array('top', $away)) {
for ($y=0; $y < $this->height; $y++) {
for ($x=0; $x < $this->width; $x++) {
$checkColor = $this->pickColor($x, $y, 'array');
if (($checkColor != $color && $checkTransparency == false) or ($checkColor['a'] != 0 && $checkTransparency == true)) {
$top_y = $y;
break 2;
}
}
}
}
if (in_array('left', $away)) {
for ($x=0; $x < $this->width; $x++) {
$checkColor = $this->pickColor($x, $y, 'array');
if (($checkColor != $color && $checkTransparency == false) or ($checkColor['a'] != 0 && $checkTransparency == true)) {
$x_values[] = $x;
$y_values[] = $y;
for ($y=0; $y < $this->height; $y++) {
$checkColor = $this->pickColor($x, $y, 'array');
if (($checkColor != $color && $checkTransparency == false) or ($checkColor['a'] != 0 && $checkTransparency == true)) {
$top_x = $x;
break 2;
}
}
}
}
// define area to crop out
if (count($x_values)) {
sort($x_values);
$src_x = reset($x_values);
$width = end($x_values) - $src_x + 1;
if (in_array('bottom', $away)) {
for ($y=($this->height-1); $y >= 0; $y--) {
for ($x=0; $x < $this->width; $x++) {
$checkColor = $this->pickColor($x, $y, 'array');
if (($checkColor != $color && $checkTransparency == false) or ($checkColor['a'] != 0 && $checkTransparency == true)) {
$bottom_y = $y+1;
break 2;
}
}
}
}
if (count($y_values)) {
sort($y_values);
$src_y = reset($y_values);
$height = end($y_values) - $src_y + 1;
}
// check if top border should be trimmed away
if ( ! in_array('top', $away) && ! in_array('TOP', $away)) {
$height = $height + $src_y;
$src_y = 0;
}
// check if bottom border should be trimmed away
if ( ! in_array('bottom', $away) && ! in_array('BOTTOM', $away)) {
$height = $this->height - $src_y;
}
// check if left border should be trimmed away
if ( ! in_array('left', $away) && ! in_array('LEFT', $away)) {
$width = $width + $src_x;
$src_x = 0;
}
// check if right border should be trimmed away
if ( ! in_array('right', $away) && ! in_array('RIGHT', $away)) {
$width = $this->width - $src_x;
}
// modify image if all values are set
if (isset($width) && isset($height) && isset($src_x) && isset($src_y)) {
$this->modify(0, 0, $src_x, $src_y, $width, $height, $width, $height);
if (in_array('right', $away)) {
for ($x=($this->width-1); $x >= 0; $x--) {
for ($y=0; $y < $this->height; $y++) {
$checkColor = $this->pickColor($x, $y, 'array');
if (($checkColor != $color && $checkTransparency == false) or ($checkColor['a'] != 0 && $checkTransparency == true)) {
$bottom_x = $x+1;
break 2;
}
}
}
}
$this->modify(0, 0, $top_x, $top_y, ($bottom_x-$top_x), ($bottom_y-$top_y), ($bottom_x-$top_x), ($bottom_y-$top_y));
return $this;
}

View File

@ -1788,6 +1788,112 @@ class ImageTest extends PHPUnit_Framework_Testcase
$this->assertEquals('#ffa601', $img->pickColor(6, 6, 'hex'));
$this->assertEquals('#ffa601', $img->pickColor(21, 21, 'hex'));
$img = Image::make('public/trim.png');
$img->trim('transparent');
$this->assertEquals($img->width, 50);
$this->assertEquals($img->height, 50);
$this->assertEquals('#00aef0', $img->pickColor(6, 6, 'hex'));
$this->assertEquals('#ffa601', $img->pickColor(21, 21, 'hex'));
$img = Image::make('public/trim.png');
$img->trim('top-left', array('top', 'bottom'));
$this->assertEquals($img->width, 50);
$this->assertEquals($img->height, 28);
$this->assertEquals('#00aef0', $img->pickColor(6, 6, 'hex'));
$this->assertEquals('#f6a609', $img->pickColor(25, 0, 'hex'));
$img = Image::make('public/trim.png');
$img->trim('top-left', array('left', 'right'));
$this->assertEquals($img->width, 28);
$this->assertEquals($img->height, 50);
$this->assertEquals('#f6a609', $img->pickColor(0, 24, 'hex'));
$this->assertEquals('#00aef0', $img->pickColor(27, 49, 'hex'));
$img = Image::make('public/trim.png');
$img->trim('top-left', array('bottom', 'right'));
$this->assertEquals($img->width, 39);
$this->assertEquals($img->height, 39);
$this->assertEquals('#4eaca7', $img->pickColor(38, 20, 'hex'));
$this->assertEquals('#88aa71', $img->pickColor(28, 38, 'hex'));
$img = Image::make('public/trim.png');
$img->trim('top-left', array('bottom', 'left'));
$this->assertEquals($img->width, 39);
$this->assertEquals($img->height, 39);
$this->assertEquals('#b8a844', $img->pickColor(0, 22, 'hex'));
$this->assertEquals('#b8a844', $img->pickColor(11, 11, 'hex'));
$img = Image::make('public/trim.png');
$img->trim('top-left', array('top', 'left'));
$this->assertEquals($img->width, 39);
$this->assertEquals($img->height, 39);
$this->assertEquals('#f6a609', $img->pickColor(14, 0, 'hex'));
$this->assertEquals('#b8a844', $img->pickColor(0, 16, 'hex'));
$img = Image::make('public/trim.png');
$img->trim('top-left', array('top', 'right'));
$this->assertEquals($img->width, 39);
$this->assertEquals($img->height, 39);
$this->assertEquals('#f6a609', $img->pickColor(24, 0, 'hex'));
$this->assertEquals('#b8a844', $img->pickColor(11, 11, 'hex'));
$img = Image::make('public/trim.png');
$img->trim('top-left', array('top', 'right', 'bottom'));
$this->assertEquals($img->width, 39);
$this->assertEquals($img->height, 28);
$this->assertEquals('#f6a609', $img->pickColor(24, 0, 'hex'));
$this->assertEquals('#b8a844', $img->pickColor(11, 11, 'hex'));
$img = Image::make('public/trim.png');
$img->trim('top-left', array('right', 'bottom', 'left'));
$this->assertEquals($img->width, 28);
$this->assertEquals($img->height, 39);
$this->assertEquals('#b8a844', $img->pickColor(0, 22, 'hex'));
$this->assertEquals('#b8a844', $img->pickColor(27, 27, 'hex'));
$img = Image::make('public/trim.png');
$img->trim('top-left', array('bottom', 'left', 'top'));
$this->assertEquals($img->width, 39);
$this->assertEquals($img->height, 28);
$this->assertEquals('#f6a609', $img->pickColor(13, 0, 'hex'));
$this->assertEquals('#88aa71', $img->pickColor(0, 17, 'hex'));
$img = Image::make('public/trim.png');
$img->trim('top-left', array('left', 'top', 'right'));
$this->assertEquals($img->width, 28);
$this->assertEquals($img->height, 39);
$this->assertEquals('#f6a609', $img->pickColor(13, 0, 'hex'));
$this->assertEquals('#ffa601', $img->pickColor(7, 7, 'hex'));
$this->assertEquals('#88aa71', $img->pickColor(0, 17, 'hex'));
$img = Image::make('public/trim.png');
$img->trim('top-left', array('bottom'));
$this->assertEquals($img->width, 50);
$this->assertEquals($img->height, 39);
$this->assertEquals('#00aef0', $img->pickColor(6, 6, 'hex'));
$this->assertEquals('#f6a609', $img->pickColor(11, 24, 'hex'));
$img = Image::make('public/trim.png');
$img->trim('top-left', array('right'));
$this->assertEquals($img->width, 39);
$this->assertEquals($img->height, 50);
$this->assertEquals('#00aef0', $img->pickColor(6, 6, 'hex'));
$this->assertEquals('#f6a609', $img->pickColor(11, 25, 'hex'));
$img = Image::make('public/trim.png');
$img->trim('top-left', 'right');
$this->assertEquals($img->width, 39);
$this->assertEquals($img->height, 50);
$this->assertEquals('#00aef0', $img->pickColor(6, 6, 'hex'));
$this->assertEquals('#f6a609', $img->pickColor(11, 25, 'hex'));
$img = Image::make('public/trim.png');
$img->trim('bottom-right', 'right');
$this->assertEquals($img->width, 39);
$this->assertEquals($img->height, 50);
$this->assertEquals('#00aef0', $img->pickColor(6, 6, 'hex'));
$this->assertEquals('#f6a609', $img->pickColor(11, 25, 'hex'));
$img = Image::make('public/mask1.png');
$img->trim('bottom-right');
$this->assertEquals($img->width, 17);
@ -1809,34 +1915,6 @@ class ImageTest extends PHPUnit_Framework_Testcase
$this->assertEquals('rgba(255, 255, 255, 0.72)', $img->pickColor(6, 6, 'rgba'));
$this->assertEquals('rgba(0, 0, 0, 0.00)', $img->pickColor(19, 19, 'rgba'));
$img = Image::make('public/trim.png');
$img->trim('top-left', array('top', 'bottom'));
$this->assertEquals($img->width, 50);
$this->assertEquals($img->height, 28);
$this->assertEquals('#00aef0', $img->pickColor(6, 6, 'hex'));
$this->assertEquals('#f6a609', $img->pickColor(25, 0, 'hex'));
$img = Image::make('public/trim.png');
$img->trim('top-left', array('left', 'right'));
$this->assertEquals($img->width, 28);
$this->assertEquals($img->height, 50);
$this->assertEquals('#f6a609', $img->pickColor(0, 24, 'hex'));
$this->assertEquals('#00aef0', $img->pickColor(27, 49, 'hex'));
$img = Image::make('public/trim.png');
$img->trim('top-left', array('bottom'));
$this->assertEquals($img->width, 50);
$this->assertEquals($img->height, 39);
$this->assertEquals('#00aef0', $img->pickColor(6, 6, 'hex'));
$this->assertEquals('#f6a609', $img->pickColor(11, 24, 'hex'));
$img = Image::make('public/trim.png');
$img->trim('top-left', array('right'));
$this->assertEquals($img->width, 39);
$this->assertEquals($img->height, 50);
$this->assertEquals('#00aef0', $img->pickColor(6, 6, 'hex'));
$this->assertEquals('#f6a609', $img->pickColor(11, 25, 'hex'));
$img = Image::make('public/mask2.png');
$img->trim('transparent', array('TOP', 'BOTTOM'));
$this->assertEquals($img->width, 32);
@ -1849,12 +1927,16 @@ class ImageTest extends PHPUnit_Framework_Testcase
$this->assertEquals($img->width, 16);
$this->assertEquals($img->height, 16);
$img = Image::make('public/trim.png');
$img->trim('top-left', 'right');
$this->assertEquals($img->width, 39);
$this->assertEquals($img->height, 50);
$this->assertEquals('#00aef0', $img->pickColor(6, 6, 'hex'));
$this->assertEquals('#f6a609', $img->pickColor(11, 25, 'hex'));
// trim selfmade image
$img = Image::canvas(1, 1, '000000');
$img->resizeCanvas(25, 25, 'center', false, 'ffffff');
$this->assertEquals($img->width, 25);
$this->assertEquals($img->height, 25);
$this->assertEquals('#ffffff', $img->pickColor(0, 0, 'hex'));
$img->trim();
$this->assertEquals($img->width, 1);
$this->assertEquals($img->height, 1);
$this->assertEquals('#000000', $img->pickColor(0, 0, 'hex'));
}
public function testEncoded()