Forum Moderators: coopster

Message Too Old, No Replies

Saving Images on the fly?

         

angst

7:35 pm on Jul 29, 2005 (gmt 0)

10+ Year Member



but it just shows the images, what i would like to do it save the image from the script,

my code:

<?php
header('Content-type: image/jpeg');
$myimage = resizeImage('images/Bikini006.jpg', '120', '80');

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

print resizeImage('images/Bikini006.jpg', '120', '80');
?>

so how can i save the modified version to a directory on the server from this script?

thanks in advance for your time!
-Ken

sned

7:47 pm on Jul 29, 2005 (gmt 0)

10+ Year Member



imagejpeg [us2.php.net] has an option to output to a file ...

so in your case,

imagejpeg($thumb, '/somedir/newpicture.jpg',100);

would write that new image to a new file, at 100% quality

-sned

Longhaired Genius

7:49 pm on Jul 29, 2005 (gmt 0)

10+ Year Member



Here are a couple of lines I've just copied verbatim from a similar script of my own:

// make directory for thumbnails
mkdir($site . '/' . $gallery . '/thumbnails', 0707);
// save the image
imagejpeg($img, $site . '/' . $gallery . '/thumbnails/' . $image_file, '95');
}

You should be able to customise them to do what you want.

angst

7:59 pm on Jul 29, 2005 (gmt 0)

10+ Year Member



hmm, ok.
i've tried that and i still can't get it to work,
i'm not getting any errors, but the thumb just doesn't show up in the thumbs dir for some reason.

my code so far:

<?php
header('Content-type: image/jpeg');
$myimage = resizeImage('images/Bikini006.jpg', '120', '80');

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

imagejpeg($thumb, '/u/a/atticnet/www.domain.com/gallery/images/thumbs/Bikini006.jpg',100);

}

print resizeImage('images/Bikini006.jpg', '120', '80');

?>

any ideas what i'm doing wrong here?

thanks again for your time!
-Ken

Longhaired Genius

8:16 pm on Jul 29, 2005 (gmt 0)

10+ Year Member



It may be a permissions issue. Your script must have permission to save to yout directory. Either create the directory first and give it the necessary mode (with chmod) or do it in the script as per my example.

sned

8:46 pm on Jul 29, 2005 (gmt 0)

10+ Year Member



Another thing you might do is put the return statement after the image creation statement:

imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($thumb, '/u/a/atticnet/www.domain.com/gallery/images/thumbs/Bikini006.jpg',100);
return imagejpeg($thumb);

I can't remember for sure, but I think code placed after a return may not get executed.

-sned

angst

8:55 pm on Jul 29, 2005 (gmt 0)

10+ Year Member



ah kool,
got it working,
all i had to do was put the path inside this line:

return imagejpeg($thumb);

insted of adding a new one:-)

thanks for all your help!
-Ken