file_get_contents jpg corrupt download

时间:2016-05-04 14:08:13

标签: php image jpeg file-get-contents

我正在使用函数:file_get_contents()下载此照片,它似乎已损坏。但原始照片看起来很好。

原始照片: original photo

下载的照片: Download photo

代码:

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

1 个答案:

答案 0 :(得分:0)

试试这个。

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');