9951 explained code solutions for 126 technologies


php-gdHow to make image grayscale


<?php

$file = '/var/www/examples/heroine.png';
$im = imagecreatefrompng($file);

imagefilter($im, IMG_FILTER_GRAYSCALE);

imagePng($im, '/tmp/image.png');ctrl + c
/var/www/examples/heroine.png

path to image to grayscale

imagecreatefrompng

creates GD image object from given PNG image

imagefilter

applies filter to image

IMG_FILTER_GRAYSCALE

converts colors for our image to grayscale

imagePng

saves image in PNG format to the given path


How to make image grayscale, php gd

Usage example

<?php

$file = '/var/www/examples/heroine.png';
$im = imagecreatefrompng($file);

imagefilter($im, IMG_FILTER_GRAYSCALE);

imagepng($im, '/tmp/image.png');