Arkane Posted June 30, 2010 Share Posted June 30, 2010 I'm trying to create a script that will automatically dull out a percentage of an image, specified via a GET value. I have the image created using imagecopy() and it is working fine, but I'm not sure how to get it to set GRAYSCALE to the percentage specified. I considered copying the image twice, copying the first x% to one resource, and the remaining % to the other, greyscaling it, then recombining the two, but I wasn't entirely sure how to, and thought there may be a better way to do it anyway. Can anyone help me out, or point me towards the correct solution. Much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/206330-add-a-filter-to-a-percentage-of-an-image/ Share on other sites More sharing options...
Psycho Posted June 30, 2010 Share Posted June 30, 2010 How are you planning to apply the percentage? If the percentage is 50% do you want the top half of the image to be grayscale or do you want the inner 1/2 to be grayscale? Here is a tutorial on converting an image from true color to grayscale using PHP. http://www.phpfreaks.com/forums/index.php/topic,302768.0.html It looks like it has to process each pixel, so altering that to only convert a percentage of the pixels should be straitforward. Quote Link to comment https://forums.phpfreaks.com/topic/206330-add-a-filter-to-a-percentage-of-an-image/#findComment-1079366 Share on other sites More sharing options...
Arkane Posted June 30, 2010 Author Share Posted June 30, 2010 Hey mjdamato, cheers for the reply. I want the image to be coloured on the left, and greyed on the right. The link you left was just pointing to this thread. I've not had any experience converting the images to greyscale myself, I've generally just used imagesfilter($img, IMG_FILTER_GRAYSCALE) to do it for me, but then I have no idea about resources that that may use in comparison. I guess if I do it with my own function then it's probably the same as adding the filter, so would be less resource intensive to only do half. I just have a tendency to prefer built ins. Spoilt really. Quote Link to comment https://forums.phpfreaks.com/topic/206330-add-a-filter-to-a-percentage-of-an-image/#findComment-1079373 Share on other sites More sharing options...
Psycho Posted June 30, 2010 Share Posted June 30, 2010 Sorry, I pasted the wrong link. Here is the correct one: http://bubble.ro/How_to_convert_an_image_to_grayscale_using_PHP.html I took that script and created a function that will do as you need. Just pass the function a path to the source image and the percentage in decimal form. I have also attached the image generated from the code below. <?php function convertImageToGrayscale($source_file, $percentage) { $outputImage = ImageCreateFromJpeg($source_file); $imgWidth = imagesx($outputImage); $imgHeight = imagesy($outputImage); $grayWidth = round($percentage * $imgWidth); $grayStartX = $imgWidth-$grayWidth; for ($xPos=$grayStartX; $xPos<$imgWidth; $xPos++) { for ($yPos=0; $yPos<$imgHeight; $yPos++) { // Get the rgb value for current pixel $rgb = ImageColorAt($outputImage, $xPos, $yPos); // extract each value for r, g, b $rr = ($rgb >> 16) & 0xFF; $gg = ($rgb >> & 0xFF; $bb = $rgb & 0xFF; // Get the gray Value from the RGB value $g = round(($rr + $gg + $bb) / 3); // Set the grayscale color identifier $val = imagecolorallocate($outputImage, $g, $g, $g); // Set the gray value for the pixel imagesetpixel ($outputImage, $xPos, $yPos, $val); } } return $outputImage; } $image = convertImageToGrayscale("otter.jpg", .25); header('Content-type: image/jpeg'); imagejpeg($image); ?> [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/206330-add-a-filter-to-a-percentage-of-an-image/#findComment-1079380 Share on other sites More sharing options...
Arkane Posted June 30, 2010 Author Share Posted June 30, 2010 Thanks again mjdamato. I tried that, but I got an error first off. It seems you've missed a slight bit in the middle. The line "$gg = ($rgb >> & 0xFF;" should be "$gg = ($rgb >> & 0xFF; Anyway, it does greyscale, except that I lose my transparent background with it, which turns completely black. Other than that, the rest of it looks very good, and so I think it would work perfectly for complete images. Cheers man. Quote Link to comment https://forums.phpfreaks.com/topic/206330-add-a-filter-to-a-percentage-of-an-image/#findComment-1079393 Share on other sites More sharing options...
Psycho Posted July 1, 2010 Share Posted July 1, 2010 It seems the forum screwed with the code in trying to convert the code to a smiley. As for the transparency, the code should only work on a jpeg (but I'm sure it could be converted to work for other file types) so I'm not sure what transparency you would lose since jpeg's dont support transparency. Quote Link to comment https://forums.phpfreaks.com/topic/206330-add-a-filter-to-a-percentage-of-an-image/#findComment-1079501 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.