Skip to content

Commit

Permalink
Update SimpleImage.php
Browse files Browse the repository at this point in the history
Improved the desaturate function with a specifiable amount (0-1)
  • Loading branch information
Francesco Marino committed Apr 8, 2015
1 parent 0a4c4d5 commit bc04506
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/abeautifulsite/SimpleImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,13 +309,26 @@ function crop($x1, $y1, $x2, $y2) {
}

/**
* Desaturate (grayscale)
* Desaturate (grayscale if nothing specified)
*
* @return SimpleImage
*
*/
function desaturate() {
imagefilter($this->image, IMG_FILTER_GRAYSCALE);
function desaturate($amount=1) {
// Determine amount
$amount = $this->keep_within($amount, 0, 1) * 100;

// Make a desaturated copy of the image
$new = imagecreatetruecolor($this->width, $this->height);
imagealphablending($new, false);
imagesavealpha($new, true);
imagecopy($new, $this->image, 0, 0, 0, 0, $this->width, $this->height);
imagefilter($new, IMG_FILTER_GRAYSCALE);

// Merge with specified amount
$this->imagecopymerge_alpha($this->image, $new, 0, 0, 0, 0, $this->width, $this->height, $amount);
imagedestroy($new);

return $this;
}

Expand Down

0 comments on commit bc04506

Please sign in to comment.