2011-05-02 114 views
2

嘿傢伙我試圖建立一個小的應用程序,拉在用戶個人資料圖片,允許他們操縱圖像,然後發佈修改後的圖像到他們的個人資料圖片專輯(理想情況下設置爲他們的個人資料圖片,但我不' t認爲這是可能的?)。如何臨時存儲Facebook個人資料照片?

我遇到的問題是,我使用來改變圖像的JavaScript將無法工作,除非該圖像是本地

<img src="http://profile.ak.fbcdn.net/hprofile-ak-snc4/[some_user_id].jpg" />將無法​​正常工作,但<img src="img/image.jpg" />會...

有沒有辦法達到這個目的?

我使用來獲得用戶的圖片的保持方法是這樣的:

來連接Facebook:

<?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; 
} 

然後顯示圖像:

<?php 

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

非常感謝!

+0

有大約從一個單獨的域獲取的圖像獲取圖像數據瀏覽器的安全規則。 – Pointy 2011-05-02 17:45:42

回答

0

感謝Jim的回覆,我看到有人在做一些非常類似的事情,但是再次(只是我的運氣)我遇到了問題。無論如何,我設法解決這個問題的方法是:

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中啓用了包裝(因此你可以包含網址)。

+0

謝謝吉姆!不過,我已經用另一種方式解決了這個問題,請看下面的答案。雖然非常感謝! – 2011-05-04 20:14:50

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

//編碼快樂

相關問題