i cannot save resized images

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I am missing something elementary I think. I am trying to batch resize
the contents of a directory, using imagecreatefromjpg() etc.

I have tried many scripts and I can only seem to corrupt the jpg. My
latest script is this...

// The file
$filename = '6.jpg';

// Set a maximum height and width
$width = 10;
$height = 10;

// Content type
header('Content-type: image/jpeg');
//header("Content-Disposition: attachment; filename=6.jpg");

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);

if ($width && ($width_orig < $height_orig)) {
   $width = ($height / $height_orig) * $width_orig;
} else {
   $height = ($width / $width_orig) * $height_orig;

// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height,
$width_orig, $height_orig);

// Output
imagejpeg($image_p, $filename, 100);

I am testing it on a single file, but it destroys the image. Does
imagejpeg write the new file in the current directory ? Or do I need to
do an fputs() ?

Thank you for any comments

Re: i cannot save resized images

Quoted text here. Click to load it

Close, it is actually the current working directory.  So if this was  
done via include it will be in that path.  Also what I suggest doing is  
checking what the program is doing.  My thought is that you have a  
notice or warning being generated and that is causing the corruption of  
the image, if it is being generated at all.

Try just letting it output to the browser without a header.  You should  
be able to see more details that way.  Also turn your error reporting to  
E_ALL via error_reporting(E_ALL); at the beginning of the script.

Mike Willbanks
Zend Certified Engineer

Site Timeline