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

Improved trim performance

This commit is contained in:
Vasily Khayrulin
2014-03-28 14:39:22 +04:00
parent b77e857334
commit 143a86a4a4

View File

@@ -84,6 +84,7 @@ class Image
public $encoded; public $encoded;
/** /**
* Create a new instance of Image class * Create a new instance of Image class
* *
@@ -769,7 +770,7 @@ class Image
} }
// pick base color // pick base color
$color = $this->pickColor($base_x, $base_y, 'array'); $color = imagecolorsforindex($this->resource, imagecolorat($this->resource, $base_x, $base_y));
$top_x = 0; $top_x = 0;
$top_y = 0; $top_y = 0;
@@ -779,8 +780,8 @@ class Image
if (in_array('top', $away)) { if (in_array('top', $away)) {
for ($y=0; $y < $this->height; $y++) { for ($y=0; $y < $this->height; $y++) {
for ($x=0; $x < $this->width; $x++) { for ($x=0; $x < $this->width; $x++) {
$checkColor = $this->pickColor($x, $y, 'array'); $checkColor = imagecolorsforindex($this->resource, imagecolorat($this->resource, $x, $y));
if (($checkColor != $color && $checkTransparency == false) or ($checkColor['a'] != 0 && $checkTransparency == true)) { if (($checkTransparency == false && $checkColor != $color) or ($checkTransparency == true && $checkColor['alpha'] != 127)) {
$top_y = $y; $top_y = $y;
break 2; break 2;
} }
@@ -791,8 +792,8 @@ class Image
if (in_array('left', $away)) { if (in_array('left', $away)) {
for ($x=0; $x < $this->width; $x++) { for ($x=0; $x < $this->width; $x++) {
for ($y=$top_y; $y < $this->height; $y++) { for ($y=$top_y; $y < $this->height; $y++) {
$checkColor = $this->pickColor($x, $y, 'array'); $checkColor = imagecolorsforindex($this->resource, imagecolorat($this->resource, $x, $y));
if (($checkColor != $color && $checkTransparency == false) or ($checkColor['a'] != 0 && $checkTransparency == true)) { if (($checkTransparency == false && $checkColor != $color) or ($checkTransparency == true && $checkColor['alpha'] != 127)) {
$top_x = $x; $top_x = $x;
break 2; break 2;
} }
@@ -803,8 +804,8 @@ class Image
if (in_array('bottom', $away)) { if (in_array('bottom', $away)) {
for ($y=($this->height-1); $y >= 0; $y--) { for ($y=($this->height-1); $y >= 0; $y--) {
for ($x=$top_x; $x < $this->width; $x++) { for ($x=$top_x; $x < $this->width; $x++) {
$checkColor = $this->pickColor($x, $y, 'array'); $checkColor = imagecolorsforindex($this->resource, imagecolorat($this->resource, $x, $y));
if (($checkColor != $color && $checkTransparency == false) or ($checkColor['a'] != 0 && $checkTransparency == true)) { if (($checkTransparency == false && $checkColor != $color) or ($checkTransparency == true && $checkColor['alpha'] != 127)) {
$bottom_y = $y+1; $bottom_y = $y+1;
break 2; break 2;
} }
@@ -815,8 +816,8 @@ class Image
if (in_array('right', $away)) { if (in_array('right', $away)) {
for ($x=($this->width-1); $x >= 0; $x--) { for ($x=($this->width-1); $x >= 0; $x--) {
for ($y=$top_y; $y < $bottom_y; $y++) { for ($y=$top_y; $y < $bottom_y; $y++) {
$checkColor = $this->pickColor($x, $y, 'array'); $checkColor = imagecolorsforindex($this->resource, imagecolorat($this->resource, $x, $y));
if (($checkColor != $color && $checkTransparency == false) or ($checkColor['a'] != 0 && $checkTransparency == true)) { if (($checkTransparency == false && $checkColor != $color) or ($checkTransparency == true && $checkColor['alpha'] != 127)) {
$bottom_x = $x+1; $bottom_x = $x+1;
break 2; break 2;
} }
@@ -1553,7 +1554,7 @@ class Image
$color = imagecolorat($this->resource, $x, $y); $color = imagecolorat($this->resource, $x, $y);
// format color // format color
switch (strtolower($format)) { switch ($format) {
case 'rgb': case 'rgb':
$color = imagecolorsforindex($this->resource, $color); $color = imagecolorsforindex($this->resource, $color);
$color = sprintf('rgb(%d, %d, %d)', $color['red'], $color['green'], $color['blue']); $color = sprintf('rgb(%d, %d, %d)', $color['red'], $color['green'], $color['blue']);
@@ -1579,7 +1580,6 @@ class Image
case 'integer': case 'integer':
# in gd2 library color already is int... # in gd2 library color already is int...
break; break;
default: default:
case 'array': case 'array':
$color = imagecolorsforindex($this->resource, $color); $color = imagecolorsforindex($this->resource, $color);
@@ -1587,7 +1587,7 @@ class Image
'r' => $color['red'], 'r' => $color['red'],
'g' => $color['green'], 'g' => $color['green'],
'b' => $color['blue'], 'b' => $color['blue'],
'a' => $this->alpha2rgba($color['alpha']) 'a' => round(1 - $color['alpha'] / 127, 2)
); );
break; break;
} }
@@ -1736,16 +1736,7 @@ class Image
*/ */
private function alpha2rgba($input) private function alpha2rgba($input)
{ {
$range_input = range(0, 127); return round(1 - $input / 127, 2);
$range_output = range(1, 0, 1/127);
foreach ($range_input as $key => $value) {
if ($value >= $input) {
return round($range_output[$key], 2);
}
}
return 1;
} }
/** /**