Alex N
Alex N

Reputation: 1121

How to save image in a file?

On my page I use a script to get a new image:

    $targ_w = $targ_h = 150;
    $jpeg_quality = 90;

    $src = $_POST['image'];
    $img_r = imagecreatefromjpeg($src);
    $dst_r = ImageCreateTrueColor( $targ_w, $targ_h );

    imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],
    $targ_w,$targ_h,$_POST['w'],$_POST['h']);

    header('Content-type: image/jpeg');
    imagejpeg($dst_r,null,$jpeg_quality);

Currently, the script receives $_POST data, extracts an image from it, then displays that image back to the user.

Now I would like to add logic to save the image in a file, but I don't know how to go about it. Any hints will be appreciated.

Upvotes: 0

Views: 107

Answers (4)

Alexandre Danault
Alexandre Danault

Reputation: 8672

imagejpeg($dst_r,'/path/to/file/new.jpg',$jpeg_quality);

Upvotes: 0

Eth
Eth

Reputation: 240

Just specify the location rather than NULL

imagejpeg($strImage, <NEW LOCATION>, 100);

Upvotes: 0

danielrsmith
danielrsmith

Reputation: 4060

imagejpeg() takes an optional filename to save the jpeg to file:

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

http://php.net/manual/en/function.imagejpeg.php

Upvotes: 0

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167162

You need:

imagejpeg($dst_r, "filename", $jpeg_quality);

Check the manual of imagejpeg().

bool imagejpeg ( resource $image [, string $filename [, int $quality ]] )

filename

The path to save the file to. If not set or NULL, the raw image stream will be outputted directly.

To skip this argument in order to provide the quality parameter, use NULL.

Upvotes: 4

Related Questions