Created
January 17, 2014 00:19
-
-
Save mckelvey/8466131 to your computer and use it in GitHub Desktop.
Image filtering methods.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// ref: http://www.phpied.com/image-fun-with-php-part-2/ | |
// ref: http://www.php.net/manual/en/function.imagefilter.php | |
require('./image.php'); | |
$value = (int) $_GET['value']; | |
$image = imagecreatefromjpeg($imagefile); | |
header("Content-type: image/jpeg"); | |
imagefilter($image, IMG_FILTER_BRIGHTNESS, $value); | |
imagejpeg($image); | |
imagedestroy($image); | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// ref: http://www.phpied.com/image-fun-with-php-part-2/ | |
// ref: http://www.php.net/manual/en/function.imagefilter.php | |
require('./image.php'); | |
$value = (int) $_GET['value']; | |
$image = imagecreatefromjpeg($imagefile); | |
header("Content-type: image/jpeg"); | |
imagefilter($image, IMG_FILTER_CONTRAST, $value); | |
imagejpeg($image); | |
imagedestroy($image); | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// ref: http://www.phpied.com/image-fun-with-php-part-2/ | |
// ref: http://www.php.net/manual/en/function.imagefilter.php | |
require('./image.php'); | |
$image = imagecreatefromjpeg($imagefile); | |
header("Content-type: image/jpeg"); | |
imagefilter($image, IMG_FILTER_GRAYSCALE); | |
imagejpeg($image); | |
imagedestroy($image); | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// ref: http://stackoverflow.com/questions/1890409/change-hue-of-an-image-with-php-gd-library | |
function imagehue(&$image, $angle) { | |
if($angle % 360 == 0) return; | |
$width = imagesx($image); | |
$height = imagesy($image); | |
for($x = 0; $x < $width; $x++) { | |
for($y = 0; $y < $height; $y++) { | |
$rgb = imagecolorat($image, $x, $y); | |
$r = ($rgb >> 16) & 0xFF; | |
$g = ($rgb >> 8) & 0xFF; | |
$b = $rgb & 0xFF; | |
$alpha = ($rgb & 0x7F000000) >> 24; | |
list($h, $s, $l) = rgb2hsl($r, $g, $b); | |
$h += $angle / 360; | |
if($h > 1) $h--; | |
list($r, $g, $b) = hsl2rgb($h, $s, $l); | |
imagesetpixel($image, $x, $y, imagecolorallocatealpha($image, $r, $g, $b, $alpha)); | |
} | |
} | |
} | |
function rgb2hsl($r, $g, $b) { | |
$var_R = ($r / 255); | |
$var_G = ($g / 255); | |
$var_B = ($b / 255); | |
$var_Min = min($var_R, $var_G, $var_B); | |
$var_Max = max($var_R, $var_G, $var_B); | |
$del_Max = $var_Max - $var_Min; | |
$v = $var_Max; | |
if ($del_Max == 0) { | |
$h = 0; | |
$s = 0; | |
} else { | |
$s = $del_Max / $var_Max; | |
$del_R = ( ( ( $var_Max - $var_R ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max; | |
$del_G = ( ( ( $var_Max - $var_G ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max; | |
$del_B = ( ( ( $var_Max - $var_B ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max; | |
if ($var_R == $var_Max) $h = $del_B - $del_G; | |
else if ($var_G == $var_Max) $h = ( 1 / 3 ) + $del_R - $del_B; | |
else if ($var_B == $var_Max) $h = ( 2 / 3 ) + $del_G - $del_R; | |
if ($h < 0) $h++; | |
if ($h > 1) $h--; | |
} | |
return array($h, $s, $v); | |
} | |
function hsl2rgb($h, $s, $v) { | |
if($s == 0) { | |
$r = $g = $B = $v * 255; | |
} else { | |
$var_H = $h * 6; | |
$var_i = floor( $var_H ); | |
$var_1 = $v * ( 1 - $s ); | |
$var_2 = $v * ( 1 - $s * ( $var_H - $var_i ) ); | |
$var_3 = $v * ( 1 - $s * (1 - ( $var_H - $var_i ) ) ); | |
if ($var_i == 0) { $var_R = $v ; $var_G = $var_3 ; $var_B = $var_1 ; } | |
else if ($var_i == 1) { $var_R = $var_2 ; $var_G = $v ; $var_B = $var_1 ; } | |
else if ($var_i == 2) { $var_R = $var_1 ; $var_G = $v ; $var_B = $var_3 ; } | |
else if ($var_i == 3) { $var_R = $var_1 ; $var_G = $var_2 ; $var_B = $v ; } | |
else if ($var_i == 4) { $var_R = $var_3 ; $var_G = $var_1 ; $var_B = $v ; } | |
else { $var_R = $v ; $var_G = $var_1 ; $var_B = $var_2 ; } | |
$r = $var_R * 255; | |
$g = $var_G * 255; | |
$B = $var_B * 255; | |
} | |
return array($r, $g, $B); | |
} | |
require('./image.php'); | |
$value = (float) $_GET['value']; | |
$image = imagecreatefromjpeg($imagefile); | |
header("Content-type: image/jpeg"); | |
imagehue($image, $value); | |
imagejpeg($image); | |
imagedestroy($image); | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
switch ($_GET['image']) { | |
case "nathalie.jpg": | |
case "mg5652.jpg": | |
$imagefile = $_GET['image']; | |
break; | |
default: | |
die(header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500)); | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// ref: http://stackoverflow.com/questions/1890409/change-hue-of-an-image-with-php-gd-library | |
function imagesaturation(&$image, $change) { | |
$width = imagesx($image); | |
$height = imagesy($image); | |
for($x = 0; $x < $width; $x++) { | |
for($y = 0; $y < $height; $y++) { | |
$rgb = imagecolorat($image, $x, $y); | |
$r = ($rgb >> 16) & 0xFF; | |
$g = ($rgb >> 8) & 0xFF; | |
$b = $rgb & 0xFF; | |
$alpha = ($rgb & 0x7F000000) >> 24; | |
list($h, $s, $l) = rgb2hsl($r, $g, $b); | |
$s = $s * $change; | |
if($s > 1) $s = 1; | |
if($s < 0) $s = 0; | |
list($r, $g, $b) = hsl2rgb($h, $s, $l); | |
imagesetpixel($image, $x, $y, imagecolorallocatealpha($image, $r, $g, $b, $alpha)); | |
} | |
} | |
} | |
function rgb2hsl($r, $g, $b) { | |
$var_R = ($r / 255); | |
$var_G = ($g / 255); | |
$var_B = ($b / 255); | |
$var_Min = min($var_R, $var_G, $var_B); | |
$var_Max = max($var_R, $var_G, $var_B); | |
$del_Max = $var_Max - $var_Min; | |
$v = $var_Max; | |
if ($del_Max == 0) { | |
$h = 0; | |
$s = 0; | |
} else { | |
$s = $del_Max / $var_Max; | |
$del_R = ( ( ( $var_Max - $var_R ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max; | |
$del_G = ( ( ( $var_Max - $var_G ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max; | |
$del_B = ( ( ( $var_Max - $var_B ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max; | |
if ($var_R == $var_Max) $h = $del_B - $del_G; | |
else if ($var_G == $var_Max) $h = ( 1 / 3 ) + $del_R - $del_B; | |
else if ($var_B == $var_Max) $h = ( 2 / 3 ) + $del_G - $del_R; | |
if ($h < 0) $h++; | |
if ($h > 1) $h--; | |
} | |
return array($h, $s, $v); | |
} | |
function hsl2rgb($h, $s, $v) { | |
if($s == 0) { | |
$r = $g = $B = $v * 255; | |
} else { | |
$var_H = $h * 6; | |
$var_i = floor( $var_H ); | |
$var_1 = $v * ( 1 - $s ); | |
$var_2 = $v * ( 1 - $s * ( $var_H - $var_i ) ); | |
$var_3 = $v * ( 1 - $s * (1 - ( $var_H - $var_i ) ) ); | |
if ($var_i == 0) { $var_R = $v ; $var_G = $var_3 ; $var_B = $var_1 ; } | |
else if ($var_i == 1) { $var_R = $var_2 ; $var_G = $v ; $var_B = $var_1 ; } | |
else if ($var_i == 2) { $var_R = $var_1 ; $var_G = $v ; $var_B = $var_3 ; } | |
else if ($var_i == 3) { $var_R = $var_1 ; $var_G = $var_2 ; $var_B = $v ; } | |
else if ($var_i == 4) { $var_R = $var_3 ; $var_G = $var_1 ; $var_B = $v ; } | |
else { $var_R = $v ; $var_G = $var_1 ; $var_B = $var_2 ; } | |
$r = $var_R * 255; | |
$g = $var_G * 255; | |
$B = $var_B * 255; | |
} | |
return array($r, $g, $B); | |
} | |
require('./image.php'); | |
$value = abs((float) $_GET['value']); | |
$image = imagecreatefromjpeg($imagefile); | |
header("Content-type: image/jpeg"); | |
imagesaturation($image, $value); | |
imagejpeg($image); | |
imagedestroy($image); | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// ref: http://us1.php.net/imageconvolution | |
// ref: http://loriweb.pair.com/8udf-basics.html | |
// ref: http://loriweb.pair.com/8udf-sharpen.html | |
require('./image.php'); | |
$value = abs((float) $_GET['value']); | |
$image = imagecreatefromjpeg($imagefile); | |
header("Content-type: image/png"); | |
$sharpenMatrix = array( | |
array(-1.2, -1, -1.2), | |
array(-1, $value, -1), | |
array(-1.2, -1, -1.2) | |
); | |
// calculate the sharpen divisor | |
$divisor = array_sum(array_map('array_sum', $sharpenMatrix)); | |
$offset = 0; | |
// apply the matrix | |
imageconvolution($image, $sharpenMatrix, $divisor, $offset); | |
imagepng($image); | |
imagedestroy($image); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment