PHP imagejpeg 保存文件不起作用
我有 php 代码来修剪白色外边框并调整大小。当我使用 imagejpeg($newImage) 将其输出到浏览器时,它工作正常,但是当我尝试使用 imagejpeg($newImage, 'test.jpg') 保存时,它不会保存在任何地方。请帮忙?
$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);
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只是将答案放入答案框中,问题是文件权限不好。
在用 PHP 编写文件之前,不要忘记使用 可写
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