Convert image to grayscale php

In this article, I am going to tell you how to convert a normal colored or any RGB image to a grayscale image in PHP.

Before I go through, let me tell you what is actually a Grayscale image is.

A grayscale image is a type of image that only contains various levels of gray in that image. These types of images can be expressed as RGB.

See the two images below:

Convert image to grayscale php

On the left-hand side, it is a, as usual, colored image which is called RGB image. On the right-hand side, we can see the grayscale image converted from the same image.

Many of us call the grayscale image “black and white”. But actually this is not a black and white image. Black and white image is different than the grayscale image. Black and white can only be formed in black and white colors.

So grayscale is the proper type name for these images that you can see on the right side of the above picture as an example.

PHP code to convert an RGB image to Grayscale image

Now, let’s see the necessary PHP code that required to convert a colored image to a grayscale image. We are going to use PHP in-built functions to perform our tasks and will not use any type of library.

Below is our PHP code that can do that:

In the above code, we have used the imagecreatefrompng to create our image. To convert our image into grayscale, we have used the imagefilter() PHP function to filter our image. To get the grayscale image, we have passed IMG_FILTER_GRAYSCALE as the filter type.

Here, the important role to get a grayscale image is played by the imagefilter() function and the IMG_FILTER_GRAYSCALE parameter as the filter type.

The IMG_FILTER_GRAYSCALE has the ability to turn our original image into a grayscale image by changing the red, green and blue components to their weighted sum of the image using some mathematics task.

Also, read:

  • How to resize image in PHP?
  • Add watermark text to image in PHP

As the final output of our code, we can see a new image created having the file name “forest_gray.png”. If we open the image, we can see a grayscale image.

If we give the image name as the same “forest.png” than our original image will be removed and it will be replaced with our grayscale image.

Convert image to grayscale php

In this article, we will discuss different methods to convert an image into a grayscale image in PHP. We will discuss both the predefined method as well as the user-defined methods.

Convert an image to grayscale in PHP using predefined methods

PHP introduced predefined effects for image manipulation.

Example:-

Using imagefilter() methods,  such as-IMG_FILTER_GRAYSCALE, IMG_FILTER_COLORIZE  etc.

For example, let’s take below image-

Convert image to grayscale php

before

Let’s jump into the scripting part,

Output:-

Grayscale conversion successful

Convert image to grayscale php

after

Note:-

In this method, the original image is replaced by the grayscale image. So before running the script, make a backup.

How to convert to grayscale using the user-defined method

There are many ways to add a grayscale effect, I am discussing one of them as follows-

> 16) & 0xFF;
                $g = ($rgb >> 8) & 0xFF;
                $b = $rgb & 0xFF;

                // get value from rgb scale

                $gr = round(($r + $g + $b) / 3);

                // gray values have r=g=b=gr

                $val = imagecolorallocate($img, $gr, $gr, $gr);

                // set the gray value

                imagesetpixel ($img, $i, $j, $val);
        }
}

header('Content-type: image/jpeg');
imagejpeg($img);
?>

Output:-

Convert image to grayscale php

After

Note:-

There are different methods that are there for opening and displaying different image types like- for .jpeg  files the opening method is ImageCreateFromJpeg().

This is how we can add a grayscale effect to an image using PHP.

Also read,

  • Make Grayscale or Black and White Image using CSS
  • Add text to image in PHP – Watermarking

How to convert image to grayscale in php?

PHP code to convert an RGB image to Grayscale image

How do I convert an image to grayscale?

Change a picture to grayscale or to black-and-white Right-click the picture that you want to change, and then click Format Picture on the shortcut menu. Click the Picture tab. Under Image control, in the Color list, click Grayscale or Black and White.