Skip to content

Commit

Permalink
Rounded rectangle problem solved (#267)
Browse files Browse the repository at this point in the history
* Rounded rectangle problem solved

* Calculate 'thickness' half in and half out.
  • Loading branch information
maPer77 authored Oct 30, 2020
1 parent 7343f97 commit 5d0f097
Showing 1 changed file with 37 additions and 10 deletions.
47 changes: 37 additions & 10 deletions src/claviska/SimpleImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -1334,21 +1334,48 @@ public function roundedRectangle($x1, $y1, $x2, $y2, $radius, $color, $thickness
$this->arc($x1 + $radius, $y2 - $radius, $radius * 2, $radius * 2, 90, 180, $color, 'filled');
$this->arc($x2 - $radius, $y2 - $radius, $radius * 2, $radius * 2, 360, 90, $color, 'filled');
} else {
// Draw the rectangle outline without edges
$this->line($x1 + $radius, $y1, $x2 - $radius, $y1, $color, $thickness);
$this->line($x1 + $radius, $y2, $x2 - $radius, $y2, $color, $thickness);
$this->line($x1, $y1 + $radius, $x1, $y2 - $radius, $color, $thickness);
$this->line($x2, $y1 + $radius, $x2, $y2 - $radius, $color, $thickness);
// Fill in the edges with arcs
$this->arc($x1 + $radius, $y1 + $radius, $radius * 2, $radius * 2, 180, 270, $color, $thickness);
$this->arc($x2 - $radius, $y1 + $radius, $radius * 2, $radius * 2, 270, 360, $color, $thickness);
$this->arc($x1 + $radius, $y2 - $radius, $radius * 2, $radius * 2, 90, 180, $color, $thickness);
$this->arc($x2 - $radius, $y2 - $radius, $radius * 2, $radius * 2, 360, 90, $color, $thickness);
$offSet = $thickness/2;
$x1 -= $offSet;
$x2 += $offSet;
$y1 -= $offSet;
$y2 += $offSet;
$radius = self::keepWithin( $radius, 0, min(($x2-$x1)/2, ($y2-$y1)/2 )-1 );
$radius = floor($radius);
$thickness = self::keepWithin( $thickness, 1, min(($x2-$x1)/2, ($y2-$y1)/2) );
// New temp image
$tempImage = new SimpleImage();
$tempImage->fromNew($this->getWidth(), $this->getHeight(), 'transparent');
// Draw a large rectangle filled with $color.
$tempImage->roundedRectangle($x1, $y1, $x2, $y2, $radius, $color,'filled');
// Draw a smaller rectangle filled with red|blue (-$thickness pixels on each side).
$tempColor = ($color == 'red') ? 'blue' : 'red';
$radius = $radius - $thickness;
$radius = self::keepWithin($radius, 0, $radius);
$tempImage->roundedRectangle($x1+$thickness, $y1+$thickness, $x2-$thickness, $y2-$thickness, $radius, $tempColor, 'filled');
// Replace the color of the smaller rectangle with 'transparent'.
$tempImage->excludeInsideColor(($x2+$x1)/2, ($y2+$y1)/2, $color);
// Apply $tempImage over image.
$this->overlay($tempImage);
}

return $this;
}

/**
* Exclude inside color.
* Used only for roundedRectangle()
*
* @param number $x certer x of rectangle.
* @param number $y certer y of rectangle.
* @param string|array $borderColor The color of border.
*/
private function excludeInsideColor($x, $y, $borderColor) {
$borderColor = $this->allocateColor($borderColor);
$transparent = $this->allocateColor('transparent');
imagefilltoborder($this->image, $x, $y, $borderColor, $transparent);
return $this;
}

//////////////////////////////////////////////////////////////////////////////////////////////////
// Filters
//////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 5d0f097

Please sign in to comment.