PHP imagejpeg image file not working

I have php code to crop the white outer border and resize. When I use imagejpeg ($ newImage) to output it to the browser, it works fine, but when I try to save on using imagejpeg ($ newImage, 'test.jpg'), it is not saved anywhere. Help me 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);
+5
source share
1 answer

Just to answer the question in response, the problem is that the file permissions were not good. Before writing a file in PHP, be sure to check the place where you want to save the files using is_writable

+4
source

All Articles