doudu2591 2014-01-14 14:12
浏览 111

图像不是有效的JPEG

I have this code:

$image = imagecreatefromjpeg($filename);
$size = getimagesize($filename, $info);

var_dump($image);
var_dump($size);

And I receive this output:

Warning: imagecreatefromjpeg(): gd-jpeg: JPEG library reports unrecoverable error
Warning: imagecreatefromjpeg(): 'my_image.jpg' is not a valid JPEG file

bool(false)

array(7) {
  [0]=>
  int(22)
  [1]=>
  int(30)
  [2]=>
  int(2)
  [3]=>
  string(22) "width="22" height="30""
  ["bits"]=>
  int(8)
  ["channels"]=>
  int(3)
  ["mime"]=>
  string(10) "image/jpeg"
}

I got warn, that image is not a valid JPEG file and don't load that file, but getimagesize says that is JPEG file (mime type). What is happening and how can I fix this?

I tried this, but it didn't help me:

  1. check *memory_limit* in php.ini
  2. check space in the disk
  3. set *gd.jpeg_ignore_warning*

Interestingly, on the local server everything works good (with the same image of course).

  • 写回答

1条回答 默认 最新

  • dongshi8038 2016-06-29 21:02
    关注

    Create Function isPNG

    <?php
    
    function isPNG($file){
        return preg_match('/'.quotemeta('PNG').'/i', file_get_contents($file));
    }
    
    ?>
    

    Edit your code

    <?php
    
        if(preg_match('/\.(png)$/i', $filename) || isPNG($filename)){
            $image = imagecreatefrompng($filename);
        }else{
            $image = imagecreatefromjpeg($filename);
        }
    
        $size = getimagesize($filename, $info);
    
        var_dump($image);
        var_dump($size);
    ?>
    

    Good luck!

    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题