php - PNG file saved using CURL is corrupt -


i'm using following code so thread, file outputted corrupt png file. can't view in image editor. image i'm trying grab can found @ 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, 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 missing? headers i'm using? file? tried saving file manually within browser, , loaded fine image editors.

any ideas appreciated, thanks! -- 24x7

edit: know marked solved, on second thought, still can't view png's server saving. here's had been suggested 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 script on , on again no luck, once again, appreciated. here's sample of png outputs. in advanced.

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 in php.ini allow_url_fopen enable..


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -