chombone Posted July 31, 2007 Share Posted July 31, 2007 Firstly, if there's a GD forum, I couldn't find it, so please accept my apologies! I've written a fairly standard function to greyscale a jpg image. When I adapted it to do the same for png or gif (non-animated) formats, using switch to open and create the files according to their type, the images came out very dark although the jpg still came out OK. I'm new to the GD image functions and don't know why this happens. Am I missing something very obvious? The main guts of the function is as follows : <?php //get source file switch($filetype) {case "jpg" : case "jpeg" : $source = imagecreatefromjpeg($filename); break; case "png" : $source = imagecreatefrompng($filename); break; case "gif" : $source = imagecreatefromgif($filename); break; default : echo "Unrecognised file type - unable to get source file<br />"; break; } //get file info and create target $size = getimagesize($filename); $width = $size[0]; $height = $size[1]; //template image $target = imagecreate($width,$height); //create palette for template image for ($x=0;$x<256;$x++) { $palette[$x] = imagecolorallocate($target,$x,$x,$x); } //scan image and update target for ($x=0;$x<$width;$x++) { for ($y=0;$y<$height;$y++) { //get the pixel rgb values $rgb = imagecolorat($source,$x,$y); $r = $rgb >> 16 & 0xFF; $g = $rgb >> 8 & 0xFF; $b = $rgb & 0xFF; //convert to greyscale value $grey = (($r*0.299)+($g*0.587)+($b*0.114)); //set the pixel imagesetpixel($target,$x,$y,$palette[$grey]); } } //create a new image file switch($filetype) {case "jpg" : case "jpeg" : imagejpeg($target,$newfile); break; case "png" : imagepng($target,$newfile); break; case "gif" : imagegif($target,$newfile); break; default : echo "Unrecognised file type - unable to generate file<br />"; break; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/62582-solved-greyscaling-a-pnggif-image/ Share on other sites More sharing options...
deadimp Posted July 31, 2007 Share Posted July 31, 2007 There's nothing I see in your code that could really make the image darker on GIFs... All I can guess is that it has something to do with the palettes, but it seems like you've handled most of that. A quick thing, though: When you access $palette using the $grey value, be sure you cast it to an int ($grey=(int)... so it goes to the integer index instead of possibly going to an associative index of floating-point type. Quote Link to comment https://forums.phpfreaks.com/topic/62582-solved-greyscaling-a-pnggif-image/#findComment-311604 Share on other sites More sharing options...
chombone Posted July 31, 2007 Author Share Posted July 31, 2007 Thanks for the reply. I've tried casting to an integer but it had no effect. I was wondering if I need to make some kind of adjustment for the alpha channel? Quote Link to comment https://forums.phpfreaks.com/topic/62582-solved-greyscaling-a-pnggif-image/#findComment-311708 Share on other sites More sharing options...
chombone Posted July 31, 2007 Author Share Posted July 31, 2007 I've solved the problem, but I don't understand why it's there in the first place, as everybody seems to think the original code should work. I've posted what I did in case it helps anybody else. Original code (only works properly for JPG files): $rgb = imagecolorat($source,$x,$y); $r = $rgb >> 16 & 0xFF; $g = $rgb >> 8 & 0xFF; $b = $rgb & 0xFF; New code (works for all file types): $rgb = imagecolorat($source,$x,$y); $col = imagecolorsforindex($source, $rgb); $r = $col['red']; $g = $col['green']; $b = $col['blue']; The second set of values are accurate rgb values but the first set was completely wrong if the image was PNG or GIF. e.g. first gives 96 183 40 for a JPG 0 0 20 for a GIF 0 0 49 for a PNG second gives 96 183 40 for a JPG 100 183 46 for a GIF 100 183 46 for a PNG Any explanation welcome. Quote Link to comment https://forums.phpfreaks.com/topic/62582-solved-greyscaling-a-pnggif-image/#findComment-312305 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.