file_get_contents jpg corrupt download

心已入冬 提交于 2019-12-24 07:31:06

问题


I am downloading this photo using function: file_get_contents(), and it seems to be corrupted. But original photo looks fine.

Original photo: original photo

Downloaded photo: Download photo

Code:

$current = file_get_contents($image);
$name = '/tmp/img/' . uniqid().".jpg";
file_put_contents($name, $current);
$tmpImages[] = $name;

回答1:


Try this.

function savephoto($urlpath,$savepath)
{ //Download images from url
    $in =    fopen($urlpath, "rb");
    $out =   fopen($savepath, "wb");
    while ($chunk = fread($in,8192))
    {
        fwrite($out, $chunk, 8192);
    }
    fclose($in);
    fclose($out);
}

savephoto('http://i.stack.imgur.com/pwMiA.jpg','newname.jpg');


来源:https://stackoverflow.com/questions/37030224/file-get-contents-jpg-corrupt-download

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!