Search code examples
phpgdimage-conversiongdlib

PHP - save a converted image into a folder


I am trying to convert a jpeg image to black & white (grayscale) through the php function IMG_FILTER_GRAYSCALE . It works fine but i would like to save the image into a folder.

Below the code:

$im = imagecreatefromjpeg('pathtomyimage/myimage.jpg');
if($im && imagefilter($im, IMG_FILTER_GRAYSCALE)) {
 header('Content-Type: image/jpeg');
 imagejpeg($im);
} else 
 print 'Error during the b & w conversion';

Very simple after all...

In this way it prints the b & w image on the screen and i see it on my browser but i'm not able to save it into a folder (e.g. img/bw/myimage.jpg).

There's a way to do it? How can i do it?


Solution

  • From the manual:

    imagejpeg($im, 'img/bw/myimage.jpg');