php 警告:imagejpeg() [function:imagejpeg]: gd-jpeg: JPEG 库报告不可恢复的错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10611883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:36:23  来源:igfitidea点击:

Warning: imagejpeg() [function:imagejpeg]: gd-jpeg: JPEG library reports unrecoverable error

phpwordpress

提问by mack

I have wordpress installation on my root folder,

我在我的根文件夹上安装了 wordpress,

until yesterday it was working fine, but today it gives following error for i guess generating thumbnail images,

直到昨天它工作正常,但今天它给出了以下错误,因为我想生成缩略图,

Warning: imagejpeg() [function:imagejpeg]: gd-jpeg: JPEG library reports unrecoverable error: in public_html/wp-includes/media.php on line 459

do anyone have any idea regarding this warning??

有没有人对这个警告有任何想法?

Please help me

请帮我

following code is on line 459

以下代码在第 459 行

if ( !imagejpeg( $newimage, $destfilename, apply_filters( 'jpeg_quality', $jpeg_quality, 'image_resize' ) ) )

采纳答案by Fernando

1) Check the space in disk

1)检查磁盘空间

Your system must have enough disk space

您的系统必须有足够的磁盘空间

2) Check the memory limit

2)检查内存限制

Set more memory in your php:

在你的 php 中设置更多内存:

ini_set("memory_limit","256M");

3) Check the post_max_size and upload_max_filesize

3) 检查 post_max_size 和 upload_max_filesize

Set more in your htaccess file:

在您的 htaccess 文件中设置更多:

php_value post_max_size 16M
php_value upload_max_filesize 6M

4) put @ in front of the function

4) 把@放在函数前面

@imagejpeg(..............);

Point 1) worked for me.

第 1 点)为我工作。

回答by Web developer BG

You probably tried to create an image from jpegthat is not jpeg.

您可能试图从jpeg不是jpeg.

I got the same error when I was testing a thumbnail script in PHP. Then I found that the header of my input file was pngalthough its extension was .jpg.

我在PHP 中测试缩略图脚本时遇到了同样的错误。然后我发现我的输入文件的标题png虽然它的扩展名是.jpg.

So, I edited my script so that if there is an error in creating an image from jpeg, it tries to create it from png(or gifif another error occurs).

因此,我编辑了我的脚本,以便如果从 中创建图像时出现错误jpeg,它会尝试从png(或gif如果发生另一个错误)创建它。

回答by shashik493

I have a Same error .But now i am solved the same issue.

我有同样的错误。但现在我解决了同样的问题。

Answer is :We are uploading png and it converts to jpg .
Please check with jpg it works.And we need to convert png to jpg so other functions are available please check on same.

答案是:我们正在上传 png 并转换为 jpg 。
请用 jpg 检查它是否有效。我们需要将 png 转换为 jpg 以便其他功能可用,请检查相同。

Below code will be useful to convert images using GD Library.

下面的代码将有助于使用 GD 库转换图像。

//variable declare or parameter use
$originalImage ="1.jpg";
$quality=100; // for jpg good quality
$outputImage;   //for source file save.  


// jpg, png, gif or bmp?
    $exploded = explode('.',$originalImage);
    $ext = $exploded[count($exploded) - 1]; 

    if (preg_match('/jpg|jpeg/i',$ext))
        $imageTmp=imagecreatefromjpeg($originalImage);
    else if (preg_match('/png/i',$ext))
        $imageTmp=imagecreatefrompng($originalImage);
    else if (preg_match('/gif/i',$ext))
        $imageTmp=imagecreatefromgif($originalImage);
    else if (preg_match('/bmp/i',$ext))
        $imageTmp=imagecreatefrombmp($originalImage);
    else
        return 0;

    // quality is a value from 0 (worst) to 100 (best)
    imagejpeg($imageTmp, $outputImage, $quality);
    imagedestroy($imageTmp);

回答by Muthu Kumar

You have to use a function to correctly determine mime type of image. A png image that have jpg extension will lead to this error.

您必须使用函数来正确确定图像的 MIME 类型。具有 jpg 扩展名的 png 图像将导致此错误。

To avoid this error, you have to get correct mime typeof image.

为避免此错误,您必须获得正确的 MIME 类型的图像。

function getImage($path) {
switch(mime_content_type($path)) {
  case 'image/png':
    $img = imagecreatefrompng($path);
    break;
  case 'image/gif':
    $img = imagecreatefromgif($path);
    break;
  case 'image/jpeg':
    $img = imagecreatefromjpeg($path);
    break;
  case 'image/bmp':
    $img = imagecreatefrombmp($path);
    break;
  default:
    $img = null; 
  }
  return $img;
}

回答by Simon Epskamp

On PHP7 imagecreatefromjpegstarted throwing fatal errors when you try to open an invalid file, which even the @sign cannot catch.

在 PHP7 上,imagecreatefromjpeg当您尝试打开无效文件时,开始抛出致命错误,即使是@符号也无法捕捉到。

Use the following instead:

请改用以下内容:

$im = imagecreatefromstring(file_get_contents($filename));
if ($im !== false) { ... }