B-219 Sec-55 Noida, India
+918010221733

Convert a image to black and white using PHP

Below is the code for converting a image to black and white

<?php
$img = imagecreatefrompng(‘images.png’);
imagefilter($img,IMG_FILTER_GRAYSCALE);
imagepng($img,’bw.png’);
imagedestroy($img);
?>

Here images.png is original image and bw.png is the converted black and white image.
In the first line of code use imagecreatefromgif() if you are converting a gif image ,use imagecreatefromjpeg()  if you are converting a jpeg image, user imagecreatefrombmp() if you are converting a bmp image

Similarly in line 3 use imagegif() for creating black and white gif image, use imagejpeg() for jpeg image,imagebmp for bmp image.

Say thanks in comment if it helps you 🙂

(Visited 1,155 times, 1 visits today)

Leave a reply

You must be logged in to post a comment.