Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions filesystem/GD.php
Original file line number Diff line number Diff line change
Expand Up @@ -486,14 +486,16 @@ public function paddedResize($width, $height, $backgroundColor = "FFFFFF") {
}

/**
* Make the image greyscale
* $rv = red value, defaults to 38
* $gv = green value, defaults to 36
* $bv = blue value, defaults to 26
* Based (more or less entirely, with changes for readability) on code from
* http://www.teckis.com/scriptix/thumbnails/teck.html
* Make the image greyscale.
* Default color weights are based on standard BT.601 (those used in PAL, NTSC and many software packages, also see
* https://en.wikipedia.org/wiki/Grayscale#Luma_coding_in_video_systems )
*
* $R = red weight, defaults to 299
* $G = green weight, defaults to 587
* $B = blue weight, defaults to 114
* $brightness = brightness in percentage, defaults to 100
*/
public function greyscale($rv=38, $gv=36, $bv=26) {
public function greyscale($R=299, $G=587, $B=114, $brightness=100) {
$width = $this->width;
$height = $this->height;
$newGD = imagecreatetruecolor($this->width, $this->height);
Expand All @@ -502,15 +504,18 @@ public function greyscale($rv=38, $gv=36, $bv=26) {
imagealphablending($newGD, false);
imagesavealpha($newGD, true);

$rt = $rv + $bv + $gv;
$rr = ($rv == 0) ? 0 : 1/($rt/$rv);
$br = ($bv == 0) ? 0 : 1/($rt/$bv);
$gr = ($gv == 0) ? 0 : 1/($rt/$gv);
$rt = $R + $G + $B;
// if $rt is 0, bad parameters are provided, so result will be a black image
$rr = $rt ? $R/$rt : 0;
$gr = $rt ? $G/$rt : 0;
$br = $rt ? $B/$rt : 0;
// iterate over all pixels and make them grey
for($dy = 0; $dy < $height; $dy++) {
for($dx = 0; $dx < $width; $dx++) {
$pxrgb = imagecolorat($this->gd, $dx, $dy);
$heightgb = ImageColorsforIndex($this->gd, $pxrgb);
$newcol = ($rr*$heightgb['red']) + ($br*$heightgb['blue']) + ($gr*$heightgb['green']);
$newcol = min(255, $newcol*$brightness/100);
$setcol = ImageColorAllocateAlpha($newGD, $newcol, $newcol, $newcol, $heightgb['alpha']);
imagesetpixel($newGD, $dx, $dy, $setcol);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/filesystem/GDTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ protected function assertGreyscale($samples, $alphaBits = 0, $tolerance = 0) {
}

// check various sample points
$this->assertColourEquals(96, $samples[0]['red'], $tolerance);
$this->assertColourEquals(91, $samples[2]['red'], $tolerance);
$this->assertColourEquals(76, $samples[0]['red'], $tolerance);
$this->assertColourEquals(149, $samples[2]['red'], $tolerance);
$this->assertColourEquals(0, $samples[8]['red'], $tolerance);
$this->assertColourEquals(127, $samples[9]['red'], $tolerance);

Expand Down