Skip to content

Commit

Permalink
Add $calcuateOffsetFromEdge parameter to overlay method (#263)
Browse files Browse the repository at this point in the history
* Adds $ borderOffset parameter to overlay ().
Calculates the offset for the image edges.

* Use $calcuateOffsetFromEdge in "overlay()".
Update doc
  • Loading branch information
maPer77 authored Oct 19, 2020
1 parent 50d3164 commit d9234da
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ Place an image on top of the current image.
- `$opacity` (float) - The opacity level of the overlay 0-1 (default 1).
- `$xOffset` (int) - Horizontal offset in pixels (default 0).
- `$yOffset` (int) - Vertical offset in pixels (default 0).
- `$calcuateOffsetFromEdge` (bool) - Calculate Offset referring to the edges of the image. $xOffset and $yOffset have no effect in center anchor. (default false).

Returns a SimpleImage object.

Expand Down
27 changes: 14 additions & 13 deletions src/claviska/SimpleImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -708,9 +708,10 @@ public function maxColors($max, $dither = true) {
* @param float $opacity The opacity level of the overlay 0-1 (default 1).
* @param integer $xOffset Horizontal offset in pixels (default 0).
* @param integer $yOffset Vertical offset in pixels (default 0).
* @param bool $calcuateOffsetFromEdge Calculate Offset referring to the edges of the image (default false).
* @return \claviska\SimpleImage
*/
public function overlay($overlay, $anchor = 'center', $opacity = 1, $xOffset = 0, $yOffset = 0) {
public function overlay($overlay, $anchor = 'center', $opacity = 1, $xOffset = 0, $yOffset = 0, $calcuateOffsetFromEdge = false) {
// Load overlay image
if(!($overlay instanceof SimpleImage)) {
$overlay = new SimpleImage($overlay);
Expand All @@ -726,36 +727,36 @@ public function overlay($overlay, $anchor = 'center', $opacity = 1, $xOffset = 0
$y = $yOffset;
break;
case 'top right':
$x = $this->getWidth() - $overlay->getWidth() + $xOffset;
$x = $this->getWidth() - $overlay->getWidth() + ($calcuateOffsetFromEdge ? -$xOffset : $xOffset);
$y = $yOffset;
break;
case 'top':
$x = ($this->getWidth() / 2) - ($overlay->getWidth() / 2) + $xOffset;
$x = ($this->getWidth() / 2) - ($overlay->getWidth() / 2) + ($calcuateOffsetFromEdge ? 0 : $xOffset);
$y = $yOffset;
break;
case 'bottom left':
$x = $xOffset;
$y = $this->getHeight() - $overlay->getHeight() + $yOffset;
$y = $this->getHeight() - $overlay->getHeight() + ($calcuateOffsetFromEdge ? -$yOffset : $yOffset);
break;
case 'bottom right':
$x = $this->getWidth() - $overlay->getWidth() + $xOffset;
$y = $this->getHeight() - $overlay->getHeight() + $yOffset;
$x = $this->getWidth() - $overlay->getWidth() + ($calcuateOffsetFromEdge ? -$xOffset : $xOffset);
$y = $this->getHeight() - $overlay->getHeight() + ($calcuateOffsetFromEdge ? -$yOffset : $yOffset);
break;
case 'bottom':
$x = ($this->getWidth() / 2) - ($overlay->getWidth() / 2) + $xOffset;
$y = $this->getHeight() - $overlay->getHeight() + $yOffset;
$x = ($this->getWidth() / 2) - ($overlay->getWidth() / 2) + ($calcuateOffsetFromEdge ? 0 : $xOffset);
$y = $this->getHeight() - $overlay->getHeight() + ($calcuateOffsetFromEdge ? -$yOffset : $yOffset);
break;
case 'left':
$x = $xOffset;
$y = ($this->getHeight() / 2) - ($overlay->getHeight() / 2) + $yOffset;
$y = ($this->getHeight() / 2) - ($overlay->getHeight() / 2) + ($calcuateOffsetFromEdge ? 0 : $yOffset);
break;
case 'right':
$x = $this->getWidth() - $overlay->getWidth() + $xOffset;
$y = ($this->getHeight() / 2) - ($overlay->getHeight() / 2) + $yOffset;
$x = $this->getWidth() - $overlay->getWidth() + ($calcuateOffsetFromEdge ? -$xOffset : $xOffset);
$y = ($this->getHeight() / 2) - ($overlay->getHeight() / 2) + ($calcuateOffsetFromEdge ? 0 : $yOffset);
break;
default:
$x = ($this->getWidth() / 2) - ($overlay->getWidth() / 2) + $xOffset;
$y = ($this->getHeight() / 2) - ($overlay->getHeight() / 2) + $yOffset;
$x = ($this->getWidth() / 2) - ($overlay->getWidth() / 2) + ($calcuateOffsetFromEdge ? 0 : $xOffset);
$y = ($this->getHeight() / 2) - ($overlay->getHeight() / 2) + ($calcuateOffsetFromEdge ? 0 : $yOffset);
break;
}

Expand Down

0 comments on commit d9234da

Please sign in to comment.