Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to get facebook profile picture by Facebook App

    • 0
    • 0
    • 0
    • 0
    • 2
    • 0
    • 0
    • 0
    • 1.93k
    Comment on it

    If you would like to get your profile picture in facebook app. Please user the below code, you can download facebook.js from facebook SDK.

        <script src="//code.jquery.com/jquery-latest.min.js"></script>
        <script src="//connect.facebook.net/en_US/all.js"></script>
        <script src="<?= BASE_URL ?>/js/facebook.js"></script>
        <script>
         // Additional JS functions here
            window.fbAsyncInit = function () {
                FB.init({
                    appId: fbAppId, // PUT YOUR FACEBOOK APP ID HERE
                    status: true, // check login status
                    cookie: true, // enable cookies to allow the
                    // server to access the session
                    xfbml: true, // parse page for xfbml or html5
                    // social plugins like login button below
                    version: 'v2.0', // Specify an API version                   
                });
    
                // Put additional init code here
                FB.login(function (response) {
                    if (response.status === 'connected') {
                        console.info(response.authResponse);
                        //alert("Your UID is " + response.authResponse.userID);
                        // getFBData();
                        $('#fb_id').val(response.authResponse.userID);
                        getUserPicture(response.authResponse.userID)
                        // UploadPicture(response.authResponse.userID);
    
                    }
                }, {scope: 'publish_actions,user_hometown,user_location,user_photos,user_videos'});
            };
    
        // Load the SDK Asynchronously
        (function (d, s, id) {
            var js, fjs = d.getElementsByTagName(s)[0];
            if (d.getElementById(id)) {
                return;
            }
            js = d.createElement(s);
            js.id = id;
            js.src = "//connect.facebook.net/en_US/sdk.js";
            fjs.parentNode.insertBefore(js, fjs);
        }(document, 'script', 'facebook-jssdk'));
    
        function getFBData() {
            FB.api('/me', function (data) {
                // alert(data.first_name + data.last_name + data.id);
            })
        }
    
        function getUserPicture(user_id) {
            FB.api(
                    "/" + user_id + "/picture?type=large&redirect=true",
                    function (response) {
                        if (response && !response.error) {
                            $('#profilepic').attr('src', response.data.url);
    
                            $.post("saveimages",
                                    {
                                        url: response.data.url,
                                        fid: user_id
                                    },
                            function (data, status) {
                                $('#profilepic').attr('src', data);
                            });
                        }
                    }
            );
        }
    
    
    </script>
    <img id="profilepic" src="vivek.jpeg">
    

 2 Comment(s)

  • Hello Friend,

    Please review function getUserPicture(), I have made a AJAX call in this function

    $.post("saveimages",
    {
    url: response.data.url,
    fid: user_id
    }
    

    I am using Codelgniter framework to save this profile picture on server, you can make your changes accordingly.

    public function saveimages(){
    $url = $this->input->post('url');
    $fid = $this->input->post('fid');

    // SERVER URL TO SAVE IMAGE $save_image_path = public_path.'uploads/userprofilepics/'.$fid.'_profilepic.jpg'; // REPLACE THIS PATH WITH YOUR SERVER PATH TO SAVE PROFILE PIC

    $source_image = file_get_contents($url); file_put_contents($save_image_path, $source_image); }

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: