4

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 Answers 3

4

From the manual:

imagejpeg($im, 'img/bw/myimage.jpg');
Sign up to request clarification or add additional context in comments.

1 Comment

so simple so useful! Thanks a lot! :) :)
1

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

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

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.