简体   繁体   English

imagecreatefromstring(): gd-jpeg, libjpeg: 可恢复错误: JPEG 文件过早结束

[英]imagecreatefromstring(): gd-jpeg, libjpeg: recoverable error: Premature end of JPEG file

Hello guys i have the following error:大家好,我有以下错误:

imagecreatefromstring(): gd-jpeg, libjpeg: recoverable error: Premature end of JPEG file imagecreatefromstring(): gd-jpeg, libjpeg: 可恢复错误: JPEG 文件过早结束

With the following Images:使用以下图像: 在此处输入图片说明

在此处输入图片说明

I am using the following code:我正在使用以下代码:

imagecreatefromstring(base64_decode($str));

I found that this error only appears with large image dimensions.我发现此错误仅在大图像尺寸时出现。

I have tried to re-size the image to 1027*768 and it worked perfectly but i don't want to re-size the image.我曾尝试将图像大小重新调整为 1027*768 并且效果很好,但我不想重新调整图像大小。

Also i have tried the following code:我也尝试了以下代码:

@imagecreatefromstring(base64_decode($str));

It worked, but the images screwed up它有效,但图像搞砸了

在此处输入图片说明在此处输入图片说明

Is there any idea for solving this problem and thanks.有没有解决这个问题的想法,谢谢。

As you mention in your comment, you send the base64 to a mysql table, and as mentioned, the data before and after is 100% the same, ruling out the possibily that your mysql field is not large enough (although you could check to be sure).正如您在评论中提到的,您将 base64 发送到 mysql 表,并且如前所述,前后的数据 100% 相同,排除了您的 mysql 字段不够大的可能性(尽管您可以检查当然)。

Since you are using ob_start() and ob_get_contents(), it could be an output buffer related issue (max buffer size / output buffer handlers etc.), so i would suggest rewriting the code to use a (temporary) output file in imagejpeg() first and checking the results with that setup由于您使用的是 ob_start() 和 ob_get_contents(),这可能是与输出缓冲区相关的问题(最大缓冲区大小/输出缓冲区处理程序等),因此我建议重写代码以在 imagejpeg() 中使用(临时)输出文件) 首先并使用该设置检查结果

I had the same problem when I was trying to get data from user inputed image.当我尝试从用户输入的图像中获取数据时遇到了同样的问题。

The problem was that I stored the base64 coded string in to input field.问题是我将 base64 编码字符串存储到输入字段中。 There might be some kind of limit how much data input field can hold.可能存在某种限制,输入字段可以容纳多少数据。

I used textarea instead of input field and my code worked just fine!我使用textarea而不是输入字段,我的代码工作得很好!

Try this one:试试这个:

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // Get data from textarea
    $data = $_POST['image'];
    $data = explode("base64,", $data);
    // Decode the string
    $data = base64_decode($data[1]);

    $im = imagecreatefromstring($data);
    if ($im !== false) {
        // Saves file
        imagejpeg($im, 'image.jpg');
        imagedestroy($im);
    }
    else {
        // Show errors
        echo 'An error occurred.'; 
    }
}
?>
<!doctype html>
<html>
<head>
    <title>Image</title>
    <meta charset="utf-8">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>

<img src="" id="image-preview" style="width: 400px;" alt="">

<form action="" method="post">
    <textarea name="image" id="image"></textarea>
    <input type="submit" value="Make Image">
</form>

<input id="uploadImage" type="file" name="myPhoto" onchange="PreviewImage();" />
<script type="text/javascript">
    // Get's the data from file field and shows it in img tag
    function PreviewImage() {
        var oFReader = new FileReader();
        oFReader.readAsDataURL(document.getElementById("uploadImage").files[0]);

        oFReader.onload = function(oFREvent) {
            document.getElementById("image-preview").src = oFREvent.target.result;
            $("textarea").text(oFREvent.target.result);
        };
    };
</script>
</body>
</html>

I had this same problem and it turns out increasing the php ini setting for memory_limit solved it.我遇到了同样的问题,结果证明增加 memory_limit 的 php ini 设置解决了它。

The memory_limit was set to 32 MB but when I increased it to 500 MB the problem went away. memory_limit 设置为 32 MB,但是当我将其增加到 500 MB 时,问题就消失了。 Which is a little bit strange because the file I couldn't upload was only 2 MB but imagecreatefromstring clearly uses a lot more memory than just the file size.这有点奇怪,因为我无法上传的文件只有 2 MB,但 imagecreatefromstring 显然使用了比文件大小更多的内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM