简体   繁体   中英

PHP imagejpeg save file doesn't work

I have php code to trim white outer border and resize. When I use imagejpeg($newImage) to output it the browser it works fine but when I try to save to using imagejpeg($newImage, 'test.jpg') it doesn't get saved anywhere. Help please?

$im = imagecreatefromjpeg($src);
$bg = imagecolorallocate($im,$rgb,$rgb,$rgb);


 // Set the header and output image.
header('Content-type: image/jpeg');
imagetrim($im,$bg);
$width = imagesx($im);
$height = imagesy($im);
$newHeight = $height * $newWidth/$width;

$newImage = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($newImage, $im, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); 
//imagejpeg($newImage);
//header('Content-Type: image/jpeg');
imagejpeg($newImage, 'test.jpg');
imagejpeg($newImage);
imagedestroy($im);
imagedestroy($newImage);

Just to put an answer in the answer box the problem is that the file permissions were not good. Before writing a file in PHP, do not forget to test the place you want to save files by using is_writable

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