PHP - save a converted image into a folder

5.8k views Asked by At

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?

3

There are 3 answers

1
Daniel W. On BEST ANSWER

From the manual:

imagejpeg($im, 'img/bw/myimage.jpg');
0
Manish Patel On

you can use like this

$tmp=imagecreatetruecolor($newwidth,$newheight);

    $newwidth1=120;
    $newheight1=150;
    $tmp1=imagecreatetruecolor($newwidth1,$newheight1);

    imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,
     $width,$height);

    imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1, 
    $width,$height);

    $filename = "../Advertisement/". $_FILES['img']['name'];
    $filename1 = "../Advertisement/small". $_FILES['img']['name'];

    $filename2 = $_FILES['img']['name'];
    imagejpeg($tmp,$filename,100);
    imagejpeg($tmp1,$filename1,100);

    imagedestroy($src);
    imagedestroy($tmp);
    imagedestroy($tmp1);
0
mlepinay On

You can use imagejpeg in this way to store your image into a folder :

imagejpeg($image, "/path/to/store/file.jpg");