2011-05-02 5 views
2

안녕 얘들 아, 사용자 프로필 사진을 가져 와서 이미지를 조작 한 다음 수정 된 이미지를 프로필 사진 앨범에 게시 할 수있는 작은 앱을 만들려고합니다. (이상적으로 프로필 사진으로 설정되지만, 이것은 가능하다고 생각 하는가 ???).어떻게하면 페이 스북 프로필 사진을 임시로 저장할 수 있습니까?

제가하는 데 문제는 ... 이미지가

<img src="http://profile.ak.fbcdn.net/hprofile-ak-snc4/[some_user_id].jpg" /> 작동하지 않습니다 지역의 경우를 제외하고 내가 이미지를 변경하기 위해 사용하고 자바 스크립트가 작동하지 않습니다,하지만 <img src="img/image.jpg" />는 것입니다

이것을 달성 할 수있는 방법이 있습니까?

<?php 

$aResponse = $facebook->api('/me', array(
    'fields' => 'picture', 
    'type' => 'large' 
)); 
echo "<img src='".$aResponse["picture"]."' />"; 
?> 
:

다음
<?php 
require_once 'library/facebook.php'; 

$app_id = "###"; 
$app_secret = "###"; 

$facebook = new Facebook(array(
    'appId' => $app_id, 
    'secret' => $app_secret, 
    'cookie' => true 
)); 

if(is_null($facebook->getUser())) 
{ 
    header("Location:{$facebook->getLoginUrl(array('req_perms' => 'user_status,publish_stream,user_photos'))}"); 
    exit; 
} 

이미지를 표시합니다 :

페이스 북에 연결하려면 :

I는 사용자의 사진을 잡아 사용하고있는 방법은 이것이다

많은 감사!

+0

코딩. – Pointy

답변

0

귀하의 답변에 감사드립니다. 나는 그와 매우 비슷한 것을하고있는 누군가를 보았습니다. 그러나 다시 (단지 나의 행운) 나는 그것에 문제가있었습니다. 어쨌든 나는 그것을 해결하기 위해 관리하는 방법이었다

function save_image($inPath,$outPath) 
{ //Download images from remote server 
    $in= fopen($inPath, "rb"); 
    $out= fopen($outPath, "wb"); 
    while ($chunk = fread($in,8192)) 
    { 
     fwrite($out, $chunk, 8192); 
    } 
    fclose($in); 
    fclose($out); 
} 

// This is just pulling the user id to use for the filename 
$id = $get_id['id']; 

save_image($aResponse['picture'],'tmp/'.$id.'.jpg'); 
1

조작하려는 이미지를 쿼리 매개 변수로 사용하고 이미지 콘텐츠 만 출력하는 프록시 이미지 서버를 직접 작성하십시오. 사용자의 그림에 직접적으로 액세스하는 것보다 약간 느리지 만 창의적이면 이미지를 로컬에서 캐시하여 후속로드를 더 빠르게 할 수 있습니다.

는이 같은 것 할 수있는 간단한 방법 :

프론트 엔드 :

<img src="image_server.php?img=<?= urlencode($aResponse['picture']); ?>"> 

백 엔드 :

<?php 
if (!empty($_GET['img'])) 
{ 
    //make sure this is a file on the facebook content delivery network 
    //and not our /etc/passwd or database connection config, or something 
    //else completely malicious. 
    if (preg_match("#^https?://profile\.ak\.fbcdn\.net/#i", $_GET['img'])) 
    { 
     $img_path = $_GET['img']; 
    } 
    else 
    { 
     //do something with someone that entered a bad image, probably just 
     //display a "no image" image. 
     die('bad user. bad.'); 
    } 

    readfile($img_path); 
    exit; 
} 
else 
{ 
    //no image was specified. output an anonymous/no image image. 
    die('an image file must be specified.'); 
} 

당신은보다 조금 더 복잡 얻을 수도 있습니다 그 ...하지만 그게 기본 요지 야.

참고 : php 코드는 php.ini에서 fopen wrappers가 활성화되어 있다고 가정하므로 (웹 URL을 포함 할 수 있음).

+0

짐 감사합니다! 그러나, 나는 그 시간에 다른 방법으로 그것을 해결했다, 아래 내 대답을 한번보세요. 대단히 고마워! –

0
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); 

function curl_redir_exec($ch) 
    { 
     static $curl_loops = 0; 
     static $curl_max_loops = 20; 
     if ($curl_loops++ >= $curl_max_loops) 
     { 
      $curl_loops = 0; 
      return FALSE; 
     } 
     curl_setopt($ch, CURLOPT_HEADER, true); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
     $data = curl_exec($ch); 
     @list($header, $data) = @explode("\n\n", $data, 2); 
     $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
     if ($http_code == 301 || $http_code == 302) 
     { 
      $matches = array(); 
      preg_match('/Location:(.*?)\n/', $header, $matches); 
      $url = @parse_url(trim(array_pop($matches))); 
      if (!$url) 
      { 
       //couldn't process the url to redirect to 
       $curl_loops = 0; 
       return $data; 
      } 
      $last_url = parse_url(curl_getinfo($ch, CURLINFO_EFFECTIVE_URL)); 
      if (!$url['scheme']) 
       $url['scheme'] = $last_url['scheme']; 
      if (!$url['host']) 
       $url['host'] = $last_url['host']; 
      if (!$url['path']) 
       $url['path'] = $last_url['path']; 
      $new_url = $url['scheme'] . '://' . $url['host'] . $url['path'] . (@$url['query']?'?'.$url['query']:''); 
      return $new_url; 
     } else { 
      $curl_loops=0; 
      return $data; 
     } 
    } 

    function get_right_url($url) { 
     $curl = curl_init($url); 
     curl_setopt($curl, CURLOPT_HEADER, false); 
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
     return curl_redir_exec($curl); 
    } 




    $url = 'http://graph.facebook.com/' . $fbid . '/picture?type=large'; 

    $file_handler = fopen('/img/avatar/'.$fbid.'.jpg', 'w'); 
    $curl = curl_init(get_right_url($url)); 
    curl_setopt($curl, CURLOPT_FILE, $file_handler); 
    curl_setopt($curl, CURLOPT_HEADER, false); 
    curl_exec($curl); 

    curl_close($curl); 
    fclose($file_handler); 

// 해피 별도의 도메인에서 가져온 이미지에서 이미지 데이터에 액세스하는 방법에 대한 브라우저 보안 규칙이 있습니다