简体   繁体   English

如何临时存储 facebook 头像?

[英]How do I temporarily store facebook profile picture?

Hey guys i'm trying to build a little app that pulls in the users profile picture, allows them to manipulate the image and then publish the modified image to their profile pictures album (ideally set as their profile pic, but i don't think this is possible???).嘿,伙计们,我正在尝试构建一个小应用程序,该应用程序可以提取用户的个人资料图片,允许他们操纵图像,然后将修改后的图像发布到他们的个人资料图片相册(理想情况下设置为他们的个人资料图片,但我不认为这个有可能???)。

The problem I'm having is that the javascript i'm using to alter the image will not work unless the image is local我遇到的问题是,除非图像是本地的,否则我用来更改图像的 javascript 将不起作用

ie <img src="http://profile.ak.fbcdn.net/hprofile-ak-snc4/[some_user_id].jpg" /> will not work, but <img src="img/image.jpg" /> will...<img src="http://profile.ak.fbcdn.net/hprofile-ak-snc4/[some_user_id].jpg" />不起作用,但是<img src="img/image.jpg" />将要...

Is there any way of achieving this?有没有办法做到这一点?

The method I am using to get hold of the user picture is this:我用来获取用户图片的方法是这样的:

To connect to facebook:要连接到 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;
}

Then to display the image:然后显示图像:

<?php

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

Many thanks!非常感谢!

Write yourself a proxy image server which which takes the the image you want to manipulate as a query parameter and just outputs the image content.为自己编写一个代理图像服务器,它将您要操作的图像作为查询参数并仅输出图像内容。 It's a little slower than directly accessing the user's picture, but if you get creative you could cache that image locally to make subsequent loads faster.它比直接访问用户的图片要慢一些,但是如果您有创意,您可以在本地缓存该图片以加快后续加载速度。

a simple way to do this would be something like this:一个简单的方法是这样的:

front end:前端:

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

back end:后端:

<?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.');
}

You might want to get a little more complex than that...but that's the basic gist.您可能想要比这更复杂一些……但这是基本要点。

note: The php code assumes you have fopen wrappers enabled in your php.ini (so you can include web urls).注意: php 代码假定您在 php.ini 中启用了 fopen 包装器(因此您可以包含 web 网址)。

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

// Happy Coding // 快乐编码

Thanks Jim for your response, I had seen someone doing something very similar to that, but again (just my luck) I was having problems with it.感谢吉姆的回复,我看到有人在做与此非常相似的事情,但是(只是我的运气)我又遇到了问题。 Anyway the way I managed to solve it was:无论如何,我设法解决它的方式是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM