Page 1 of 1

How to save generated image

Posted: Tue Sep 30, 2008 7:11 am
by achintha
I found image resizing code. But it's only showing the resized image. But I want to save it to server.how can I do that?

Code: Select all

<?php
header('Content-type: image/jpeg');
//$myimage = resizeImage('filename', 'newwidthmax', 'newheightmax');
$myimage = resizeImage('test.jpg', '150', '120');
print $myimage;
 
function resizeImage($filename, $newwidth, $newheight){
    list($width, $height) = getimagesize($filename);
    if($width > $height && $newheight < $height){
        $newheight = $height / ($width / $newwidth);
    } else if ($width < $height && $newwidth < $width) {
        $newwidth = $width / ($height / $newheight);   
    } else {
        $newwidth = $width;
        $newheight = $height;
    }
    $thumb = imagecreatetruecolor($newwidth, $newheight);
    $source = imagecreatefromjpeg($filename);
    imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
    return imagejpeg($thumb);
}
?>

Re: How to save generated image

Posted: Tue Sep 30, 2008 7:17 am
by VladSun
http://bg.php.net/imagejpeg
filename

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