[英]Imagecreatefromjpeg returns a black image after resize
I have a script to resize an uploaded image, but when I use it, it just returns a black square. 我有一个脚本来调整上传图像的大小,但是当我使用它时,它只会返回一个黑色正方形。 All error messages are pointing at this function:
所有错误消息都指向此函数:
function resizeImage($image,$width,$height,$scale) {
$newImageWidth = ceil($width * $scale);
$newImageHeight = ceil($height * $scale);
$newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
$source = imagecreatefromjpeg($image);
imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width,$height);
imagejpeg($newImage,$image,90);
chmod($image, 0777);
return $image;
}
My error logs: 我的错误日志:
PHP Warning: imagecreatefromjpeg() [<a href='function.imagecreatefromjpeg'>function.imagecreatefromjpeg</a>]: gd-jpeg: JPEG library reports unrecoverable error
PHP Warning: imagecreatefromjpeg() [<a href='function.imagecreatefromjpeg'>function.imagecreatefromjpeg</a>]: 'img/[hidden].jpg' is not a valid JPEG file
PHP Warning: imagecopyresampled(): supplied argument is not a valid Image resource
imagecreatetruecolor
succeeded. imagecreatetruecolor
成功。 If the new image is "large" it could exceed the PHP memory_limit. imagecreatefromjpeg()
. imagecreatefromjpeg()
同上。 The two individual images may fit within the memory limit but together could be too large. imagecopyresampled()
failed - it also returns FALSE on failure. imagecopyresampled()
失败-失败时还返回FALSE。 imagejpeg()
failed - maybe you don't have write permissions on whatever file you're specifying in $image
. imagejpeg()
失败-也许您没有对$image
指定的任何文件的写权限。 And again, this function returns FALSE on failure. According to Marc B's answer you could probably make a check if the file is a JPG file. 根据Marc B的回答,您可能可以检查该文件是否为JPG文件。 (JPEG, JPG, jpg, jpeg extensions).
(JPEG,JPG,JPG,JPEG扩展名)。
it could be something like: 可能是这样的:
$file = explode(".", $_POST['file']);
$file_ext = $file[count($file)]; // Get the last thing in the array - in this way the filename can containg dots (.)
$allowed_ext = array('jpg', 'JPG', 'jpeg', 'jpg');
if( in_array($file_ext, $allowed_ext )
{
// The code for creating the image here.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.