2 //=========================================================================
4 // Written by: Johan Persson (johanp@aditus.nu)
5 // Ver: $Id: adjimg.php,v 1.1 2002/04/20 19:31:33 aditus Exp $
8 // (Unsupported) Utility to take an image and adjust it's brightness,
9 // contrast and saturation. The modified image is the displayed.
10 // The original file is untouched.
12 // Usage: adjimg.php?file=name&b=value&c=value&s=scale&sat=saturation
15 // Copyright (C) 2001,2002 Johan Persson
16 //=========================================================================
18 function LoadImage($filename,$format="png") {
19 $f = "imagecreatefrom".$format;
20 $img = @$f($filename);
22 die("Error: Can't read image file: $filename");
27 function AdjSat($img,$sat) {
28 $nbr = imagecolorstotal ($img);
29 for( $i=0; $i<$nbr; ++$i ) {
30 $colarr = imagecolorsforindex ($img,$i);
31 $rgb[0]=$colarr["red"];
32 $rgb[1]=$colarr["green"];
33 $rgb[2]=$colarr["blue"];
34 $rgb = AdjRGBSat($rgb,$sat);
35 imagecolorset ($img, $i, $rgb[0], $rgb[1], $rgb[2]);
39 function AdjBrightContrast($img,$bright,$contr) {
40 $nbr = imagecolorstotal ($img);
41 for( $i=0; $i<$nbr; ++$i ) {
42 $colarr = imagecolorsforindex ($img,$i);
43 $r = AdjRGBBrightContrast($colarr["red"],$bright,$contr);
44 $g = AdjRGBBrightContrast($colarr["green"],$bright,$contr);
45 $b = AdjRGBBrightContrast($colarr["blue"],$bright,$contr);
46 imagecolorset ($img, $i, $r, $g, $b);
50 function AdjRGBBrightContrast($rgb,$bright,$contr) {
51 // First handle contrast, i.e change the dynamic range around grey
54 $adj = abs($rgb-128) * (-$contr);
63 $rgb = $rgb - ($rgb * $contr);
65 $rgb = $rgb + ((255-$rgb) * $contr);
68 // Add (or remove) various amount of white
77 // Adjust saturation for RGB array $u. $sat is a value between -1 and 1
78 // Note: Due to GD inability to handle true color the RGB values are only between
79 // 8 bit. This makes saturation quite sensitive for small increases in parameter sat.
81 // Tip: To get a grayscale picture set sat=-100, values <-100 changes the colors
82 // to the complementary colors.
84 // Implementation note: The saturation is implemented directly in the RGB space
85 // by adjusting the perpendicular distance between the RGB point and the "grey"
86 // line (1,1,1). Setting $sat>0 moves the point away from the line along the perp.
87 // distance and a negative value moves the point closer to the line.
88 // The values are truncated when the color point hits the bounding box along the
90 // DISCLAIMER: I'm not 100% sure this is he correct way to imeplemen a saturation
91 // function in RGB space.
92 function sign($a) {if( $a>=0) return 1; else return -1;}
93 function AdjRGBSat($rgb,$sat) {
98 $dot = $rgb[0]*$v[0]+$rgb[1]*$v[1]+$rgb[2]*$v[2];
100 // Normalize dot product
103 // Direction vector between $u and its projection onto $v
104 for($i=0; $i<3; ++$i)
105 $r[$i] = $rgb[$i] - $normdot*$v[$i];
107 // Adjustment factor so that sat==1 sets the highest RGB value to 255
110 for( $i=0; $i<3; ++$i) {
111 if( sign($r[$i]) == 1 && $r[$i]>0)
112 $m=max($m,(255-$rgb[$i])/$r[$i]);
120 for($i=0; $i<3; ++$i) {
121 $un[$i] = round($rgb[$i] + $tadj*$r[$i]);
123 // Truncate color when they reach 0
124 if( $un[$i]<0 ) $un[$i]=0;
126 // Avoid potential rounding error
127 if( $un[$i]>255 ) $un[$i]=255;
135 die("<b>Usage:</b><br>r.php?file=name&[b=value][&c=value][&s=scale][&sat=saturation]<p>
136 file= Filename, must end with the image format, i.e. .png, .jpg, .gif<br>
137 b = Brightness value [-1, 1]<br>
138 c = Contrast value [-1, 1]<br>
140 sat= Color saturation value.<br>");
142 if(strstr($file,"png"))
143 $bkg = LoadImage($file);
144 elseif( strstr($file,"jpg"))
145 $bkg = LoadImage($file,"jpeg");
146 elseif( strstr($file,"gif"))
147 $bkg = LoadImage($file,"gif");
149 if( empty($b) ) $b=0;
150 if( empty($c) ) $c=0;
151 if( empty($s) ) $s=1;
152 if( empty($sat) ) $sat=0;
154 // Adjust contrast and brightness of background color
156 AdjBrightContrast($bkg,$b,$c);
158 // Adjust color saturation
162 // Get width & height
170 $img = imagecreate($w,$h);
171 imagecopyresized($img,$bkg,0,0,0,0,$w,$h,$bw,$bh);
172 $background_color = ImageColorAllocate ($img, 255, 255, 255);
174 if(strstr($file,"png")) {
175 header ("Content-type: image/png");
178 elseif( strstr($file,"jpg")) {
179 header ("Content-type: image/jpeg");
182 elseif( strstr($file,"gif")) {
183 header ("Content-type: image/gif");
187 die("Unknown graphic format in file $file");