0

I'm using the following code from a SO thread, but the file that is outputted is a corrupt PNG file. I can't view it in any image editor. The image I'm trying to grab can be found at this direct link.

<?php
function getimg($url) {         
        $headers[] = 'Accept: image/gif, image/png, image/x-bitmap, image/jpeg, image/pjpeg';   
        $headers[] = 'Connection: Keep-Alive';         
        $headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';         
        $user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31';         
        $process = curl_init($url);         
        curl_setopt($process, CURLOPT_HTTPHEADER, $headers);         
        curl_setopt($process, CURLOPT_HEADER, 0);         
        curl_setopt($process, CURLOPT_USERAGENT, $user_agent);         
        curl_setopt($process, CURLOPT_TIMEOUT, 30);         
        curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);         
        curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);         
        $return = curl_exec($process);         
        curl_close($process);         
        return $return;     
    } 

    $imgurl = 'http://download.videos.framebase.io/5a94a14f-aca6-4fb0-abd3-f880966358ab/6bf94ebb-4712-4895-9c85-fb8d4a19d50c/8f8b778f-6335-4351-82e7-6f50fd7fdd06/1351620000000-000020/thumbs/00001.png'; 
    if(file_exists($imgsave)){continue;} 
    $image = getimg($imgurl); 
    file_put_contents($imgsave,$image); 
?>

What am I missing? Is it the headers I'm using? Is it the file? I tried saving the file just manually within my browser, and it loaded fine with my image editors.

any ideas would be much appreciated, thanks! -- 24x7

edit: I know I marked this as solved, but on second thought, I still can't view the PNG's the server is saving. Here's what had been suggested so far:

<?php
            function grab_image($url){
                echo $url;
                  $ch = curl_init ($url);
                  curl_setopt($ch, CURLOPT_HEADER, 0);
                  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                  curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
                  $raw=curl_exec($ch);
                  curl_close ($ch);
                  $saveto = 'recent-image.png';
                  if(file_exists($saveto)){
                      unlink($saveto);
                  }

                  $fp = fopen($saveto,'x');
                  fwrite($fp, $raw);
                  fclose($fp);
                }   

    grab_image("http://download.videos.framebase.io/2acd363b-ab1f-4305-8f1c-c1eaa6ebcc2a/abb6a3b7-414c-461b-a84c-56032060575d/e3682943-6959-456d-89db-8cd96647ddd4/1351620000000-000020/thumbs/00001.png");
?>

I've ran the script over and over again to no luck, once again, any help would be appreciated. Here's a sample of the PNG it outputs. thanks in advanced.

Community
  • 1
  • 1
24x7
  • 409
  • 1
  • 8
  • 23
  • Curious, your code worked fine for me aside from needing to set `$imgsave`. Can you place this corrupt PNG somewhere for examination? – EPB Apr 18 '13 at 05:47
  • Your code works fine for me. Forgive the obvious question, but are you deleting the destination file every time you test your script? Since your code tests for the destination file, if you're not deleting the local file every time, the getimg() would only have run the first time you ran the script. – Victor Bondarenko Apr 18 '13 at 05:49
  • Thank you for your responses, I've been manually deleting the file every time I run the script just for test purposes. You can find the corrupt sample here, [on my personal server](http://www.quizmakr.com/image_name.png) let me know what you think thank you! – 24x7 Apr 18 '13 at 05:54
  • It's an XML file saying access denied. – EPB Apr 18 '13 at 05:57
  • hmmm, I really have no idea... it might have been the headers I was setting at one point... thanks for your help though! – 24x7 Apr 18 '13 at 06:01
  • Quite odd, glad you got something worked out though. I'm wondering if you like hit a temporary request limit or something. – EPB Apr 18 '13 at 06:02
  • is this still possible ... I really can't view the images saved – 24x7 Apr 18 '13 at 19:01

1 Answers1

2

you can use normal function like

function grab_image($url,$saveto){
    $ch = curl_init ($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
    $raw=curl_exec($ch);
    curl_close ($ch);
     $saveto = "D:/test.png";
    if(file_exists($saveto)){
        unlink($saveto);
    }

$fp = fopen($saveto,'x');
fwrite($fp, $raw);
fclose($fp);

}

grab_image('http://download.videos.framebase.io/5a94a14f-aca6-4fb0-abd3-f880966358ab/6bf94ebb-4712-4895-9c85-fb8d4a19d50c/8f8b778f-6335-4351-82e7-6f50fd7fdd06/1351620000000-000020/thumbs/00001.png','');

and make sure that in php.ini allow_url_fopen is enable..

liyakat
  • 11,825
  • 2
  • 40
  • 46