Jump to content

Add a filter to a percentage of an image


Arkane

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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 >> 8) & 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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.