Lab #5

9. Write a PHP script to create images to grayscale

index.php

<?php
$imgcreate = imagecreatefromjpeg('tree.jpg'); // paste image path, here same path

if($imgcreate && imagefilter($imgcreate, IMG_FILTER_GRAYSCALE))
{
echo 'Grayscale image generated.';
imagejpeg($imgcreate, 'tree_gray.jpg'); // paste path where you have to save image, here same path
}
else
{
echo 'Grayscale conversion of image failed.';
}

imagedestroy($imgcreate);
?>

Output

Open this script to the browser. If you get "Grayscale image generated" output then the image is converted successfully then open the file and see the image.

The image looks like the following after conversion.



Happy Coding :)