note 68819 rejected from ref.image by nicobn
| From: | nicobn@php.net | Date: | Sun, 01 Jul 2007 14:54:01 +0000 |
| Subject: | note 68819 rejected from ref.image by nicobn | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-128139@lists.php.net to get a copy of this message | ||
Note Submitter: japidoff isliev nl
----
quite a hassle, but here it is:
gd "Sepia" effect, also called colorisation...
all the math (rgb<->hsl) comes from
http://www.easyrgb.com/math.php
it is NOT optimised, it takes about 10sec for a 500x400 jpeg @2ghz
<?
Function RGB_to_HSL($RGB){
$var_R = $RGB[0] / 255; //Where RGB values = 0 ÷ 255
$var_G = $RGB[1] / 255;
$var_B = $RGB[2] / 255;
$var_Min = min( $var_R, $var_G, $var_B ); //Min. value of RGB
$var_Max = max( $var_R, $var_G, $var_B ); //Max. value of RGB
$del_Max = $var_Max - $var_Min; //Delta RGB value
$L = ($var_Max + $var_Min) / 2;
if ($del_Max == 0){ //This is a gray, no chroma...
$H = 0; //HSL results = 0 ÷ 1
$S = 0;
}else{ //Chromatic data...
if ($L < 0.5 ){
$S = $del_Max / ($var_Max + $var_Min);
}else{
$S = $del_Max / (2 - $var_Max - $var_Min);
}
$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;
}elseif ($var_G == $var_Max){
$H = (1 / 3) + $del_R - $del_B;
}elseif($var_B == $var_Max){
$H = (2 / 3) + $del_G - $del_R;
}
if ($H < 0){
$H++;
}
if($H > 1){
$H--;
}
}
Return array($H, $S, $L);
}
Function HSL_to_RGB($HSL){
$H = $HSL[0];
$S = $HSL[1];
$L = $HSL[2];
if ($S == 0){ //HSL values = 0 ÷ 1
$R = $L * 255; //RGB results = 0 ÷ 255
$G = $L * 255;
$B = $L * 255;
}else{
if ($L < 0.5){
$var_2 = $L * (1 + $S);
}else{
$var_2 = ($L + $S)-($S * $L);
}
$var_1 = 2 * $L - $var_2;
$R = Round(255 * Hue_2_RGB($var_1, $var_2, $H + (1/3)));
$G = Round(255 * Hue_2_RGB($var_1, $var_2, $H));
$B = Round(255 * Hue_2_RGB($var_1, $var_2, $H - (1/3)));
}
Return array($R, $G, $B);
}
Function Hue_2_RGB($v1, $v2, $vH){ //Function Hue_2_RGB
if ($vH < 0){
$vH++;
}
if ($vH > 1){
$vH--;
}
if ((6 * $vH) < 1){
Return ($v1 + ($v2 - $v1) * 6 * $vH);
}
if ((2 * $vH) < 1){
Return ($v2);
}
if ((3 * $vH) < 2){
return ($v1 + ($v2 - $v1) * ((2 / 3) - $vH) * 6);
}
return ($v1);
}
Function RGB_to_gray($RGB){
$val = round((0.299 * $RGB[0]) + (0.587 * $RGB[1]) + (0.114 * $RGB[2]));
Return array($val, $val, $val);
}
$old_image =imagecreatefromjpeg("./test.jpg");
$old_x = imagesx($old_image);
$old_y = imagesy($old_image);
$new_image = imagecreatetruecolor($old_x, $old_y);
for( $x = 0; $x < $old_x; $x++){
for( $y = 0; $y < $old_y; $y++){
$_rgb = ImageColorAt($old_image, $x, $y); //original color
$RGB[0] = ($_rgb >> 16) & 0xFF; //split 24 bit color in RGB values
$RGB[1] = ($_rgb >> 8) & 0xFF;
$RGB[2] = $_rgb & 0xFF;
$RGB = RGB_to_gray($RGB); //convert to grayscale
$HSL = RGB_to_HSL($RGB); //convert to HSL
$to_hsl = RGB_to_HSL(array(94,38,18)); //set "sepia" color
$HSL[0] = $to_hsl[0]; //change Hue and Saturation values from pixel
$HSL[1] = $to_hsl[1];
$RGB = HSL_to_RGB($HSL); //convert from HSL to RGB
$_rgb = intval(($RGB[2]) + ($RGB[1] << 8) + ($RGB[0] << 16)); //merge from RGB to 24
bit
imagesetpixel($new_image, $x, $y, $_rgb); //draw pixel in new image
}
}
header('Content-Control: cache');
header('Content-type: image/jpeg');
imagejpeg($new_image);