简体   繁体   中英

saving imagejpeg output the file

Is there something I am missing here? If I change imagejpeg($thumb, $newImage); to imagejpeg($thumb); it echos a load of unreadable characters. The thumb image directory exists.

My ultimate aim is to copy an image at a lower quality.

$filename = $imageDirectory . '/1.jpg';
$percent = 0.5;
$newImage = $imageDirectory . '/thumbs/1.jpeg';    
echo "image: <br>";
echo $filename;
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;    
// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);    
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);    
// Output
imagejpeg($thumb, $newImage);

UPDATE: I now realize that the second parameter must be an image location with the new name. So... I have redefined $newImage. The path is fine... if I upload an image named 1.jpg to that location manually it exists at that path.

You'll have to add the correct Content-Type header for the response if you want to output the image to the browser:

header('Content-Type: image/jpeg');
imagejpeg($yourimage);

Please check the first example in the documentation for more information. If you want to decrease the quality before outputting to your browser check the third example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM