PDA

View Full Version : Saving Images on the fly?


angst
07-29-2005, 08:33 PM
Hello,
I'm using this script to generate thumb nails, ( width, height )

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

angst
07-29-2005, 09:07 PM
hmm, ok.

i've found the imagejpeg function, but 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

Fou-Lu
07-30-2005, 09:45 PM
Hiya Ken,
The trick to this is to use output buffering to capture your data in a variable. Normally, the imagepng, imagegif, imagejpeg etc functions will flush it to the browser. This has been modified for your use - originally it was used to store information in a database, but it will work fine for your purposes.

// This one is yours:
$source = imagecreatefromjpeg($filename);
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
return imagejpeg($thumb);
// Edit it like so:
$source = imagecreatefromjpeg($filename);
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); // Damn, I forgot how long these were :P
ob_start();
imagegif($thumb);
$created_image = ob_get_contents();
return ob_end_clean();
/* This one... Im not certain will work for you.
If you experience difficulties doing this, just return your $created_image instead. I have never tested a function returning an ob_end_* function before, so I'm not certain of its effect. */


Let me know if that works for you, specifically the returned section. I assume from this point you know how to write this to a file correct? The $created_image is binary btw.


Sorry, I changed this as well. I removed the need for the jpeg simply because your are resizing it into a palett instead. Therefore, I would suggest a gif as your new thumb format.