Skip to content

Commit

Permalink
Use percentage and only merge when < 100
Browse files Browse the repository at this point in the history
  • Loading branch information
claviska committed Apr 8, 2015
1 parent 7cdddb5 commit 2f67ff6
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions src/abeautifulsite/SimpleImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,26 +309,34 @@ function crop($x1, $y1, $x2, $y2) {
}

/**
* Desaturate (grayscale if nothing specified)
* Desaturate
*
* @param int $percentage Level of desaturization.
*
* @return SimpleImage
*
*/
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);

function desaturate($percentage = 100) {

// Determine percentage
$percentage = $this->keep_within($percentage, 0, 100);

if( $percentage === 100 ) {
imagefilter($this->image, IMG_FILTER_GRAYSCALE);
} else {
// 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 percentage
$this->imagecopymerge_alpha($this->image, $new, 0, 0, 0, 0, $this->width, $this->height, $percentage);
imagedestroy($new);

}

return $this;
}

Expand Down

0 comments on commit 2f67ff6

Please sign in to comment.