user1452407
user1452407

Reputation: 110

Facebook PHP SDK feed pictures resize?

Hi I am finishing up a facebook feed for a site and have noticed that the SDK returns very low quality images.

In the past I have done things like str replace the s.jpg to n.jpg or o.jpg. I know of these fixes. They don't work in this instance.

Additionally, in the past I have also grabbed the s130x130 segment and changed it to resize. Again, that doesn't work in my case.

The url's I am being returned in the facebook feed have formatting I have never seen before.

https://fbcdn-sphotos-a-a.akamaihd.net/hphotos-ak-xpa1/v/t1.0-9/s130x130/10426213_842375602447918_6490185390368658086_n.jpg?oh=a2db80558f6e5075b1b5c0cfbe9f6d09&oe=5487ACF5&__gda__=1418998035_89dfe352a7b600215d335f6317ed621d

They have all the usual elements, however, they also have they have ?oh&oe&gda. Which are obviously forms of GET data. These elements seem to have to be there, and the right one for the instance. It seems to be these things that prevent from re-sizing. Let me explain:

I know that there is multiple sizes of one image because I can access one at

http://fbcdn-sphotos-a-a.akamaihd.net/hphotos-ak-xpa1/v/t1.0-9/s720x720/10426213_842375602447918_6490185390368658086_n.jpg?oh=a23ee08c91265d6396c3f19e9dee5a22&oe=5493DB4F&__gda__=1422978473_f94a2eba685581687bc048a255c26ca4

My first thought was to just try and grab the image like so

http://fbcdn-sphotos-a-a.akamaihd.net/hphotos-ak-xpa1/v/t1.0-9/s720x720/10426213_842375602447918_6490185390368658086_n.jpg

or

http://fbcdn-sphotos-a-a.akamaihd.net/hphotos-ak-xpa1/v/t1.0-9/s130x130/10426213_842375602447918_6490185390368658086_n.jpg

However, that returns an error "An error occurred while processing your request. Reference #50.e4bf6d8.1411232880.1d669acb"

I also get the same response if I try to edit the previous links (marked up as code) and change the sizes.

So ultimately, how does one re-size or get a larger image size from a facebook feed? or how can I modify what I have to get something in the 250 or 500 px ball park? It seems that Oh, gdp, and oe authenticate in some way and I really have no clue how to generate the correct answers. Everywhere I look it seems people are only attempting to re-size the profile picture, but never the content.

Here is my full code below

<?php
// include the facebook sdk
require_once('resources/facebook-php-sdk-master/src/facebook.php');

// connect to app
$config = array();
$config['appId'] = 'APP_ID';
$config['secret'] = 'APP_SECRET';
$config['fileUpload'] = false; // optional

// instantiate
$facebook = new Facebook($config);

// set page id
$pageid = "PAGE_ID";

// now we can access various parts of the graph, starting with the feed
$pagefeed = $facebook->api("/" . $pageid . "/feed");
    //get page picture
    $page_picture = 'http://graph.facebook.com/' . $pageid . "/picture?app_id=" . $config['appId'];
   if(isset($fb_post_limit)){
        $numb_of_posts = $fb_post_limit;
    }else{
        $numb_of_posts = 7;
    }
    // now we can access various parts of the graph, starting with the feed
    $pagefeed = $facebook->api("/" . $pageid . "/feed");

    function check_picture($post){
        // if there is a picture post it
        if(!empty($post['picture'])){
            echo '<div class="row">';
               echo '<img src="' . $post['picture'] . '"  class="facebook-img col-xs-12"/>';
            echo '</div>';
        }
    }

?>



 <?php

echo "<div class=\"fb-feed\">";

        // set counter to 0, because we only want to display $numb posts
        $i = 0;
        foreach($pagefeed['data'] as $post) {

              if($post['from']['name'] == 'PAGE NAME'){
                if ($post['type'] == 'status' || $post['type'] == 'link' || $post['type'] == 'photo') {
                    // open up an fb-update div
                    echo "<div class=\"fb-update col-xs-12\">";


                    $ids = explode('_', $post['id']);

                        // check if post type is a status
                        if ($post['type'] == 'status') {
                            if(!$post['story'] == 'PAGE NAME commented on a post.' ){

                                check_picture($post);
                                if (empty($post['story']) === false) {
                                   echo '<img src="' . $page_picture . '" class="fb-profile" >' ;
                                    echo "<p class=\"fb-message\" >" . $post['story'] . "</p>";


                                } elseif (empty($post['message']) === false) {
                                    echo '<img src="' . $page_picture . '" class="fb-profile" >';
                                    echo "<p class=\"fb-message\">" . $post['message'] . "</p>";

                                }
                                 // post the time

                                 echo "<h5 class=\"fb-date\">Status updated: " . date("jS M, Y", (strtotime($post['created_time']))) . "</h5>";
                                 echo '<a class="facebook link btn btn-default btn-lrg" href="http://www.facebook.com/' . $pageid . '/posts/' . $ids[1] . '" >View Post</a>';
                            } else {
                                $i--;
                            }

                        }
                        // check if post type is a link
                        if ($post['type'] == 'link') {
                            echo '<img src="' . $page_picture . '" class="fb-profile" >';
                            if(!empty($post['message'])){  echo "<p class=\"fb-message\">" . str_replace($post['link'], '', $post['message']) . "</p>";}
                            echo "<h5 class=\"fb-date\">Link posted on: " . date("jS M, Y", (strtotime($post['created_time']))) . "</h5>";
                            check_picture($post);
                            echo '<p class="fb-link-desc"' . $post['name'] . '</p>';
                            if (empty($post['story']) === false) {

                                echo "<p class=\"fb-message\" >" . $post['story'] . "</p>";
                            }
                            echo "<p ><a href=\"" . $post['link'] . "\" target=\"_blank\">" . $post['link'] . "</a></p>";
                            echo '<a class="fb-link btn btn-default btn-lrg" href="http://www.facebook.com/' . $pageid . '/posts/' . $ids[1] . '" >View Post</a>';
                        }

                        // check if post type is a photo
                        if ($post['type'] == 'photo') {echo '<img src="' . $page_picture . '" class="fb-profile" >';
                            check_picture($post);
                            echo "<h5 class=\"fb-date\">Photo posted on: " . date("jS M, Y", (strtotime($post['created_time']))) . "</h5>";
                            if (empty($post['story']) === false) {
                                echo "<p>" . $post['story'] . "</p>";

                            } elseif (empty($post['message']) === false) {
                                echo "<p>" . $post['message'] . "</p>";
                            }

                            echo '<a class="facebook link btn btn-default btn-lrg" href="http://www.facebook.com/' . $pageid . '/posts/' . $ids[1] . '" >View Post</a>';
                        }



                    echo "</div>"; // close fb-update div

                    $i++; // add 1 to the counter if our condition for $post['type'] is met
                }

                //  break out of the loop if counter has reached $numb
                if ($i == $numb_of_posts) {
                    break;
                }
             }   // end the foreach statement
        }

        echo "</div>";

    ?>

Upvotes: 0

Views: 3430

Answers (2)

user1452407
user1452407

Reputation: 110

After a long while of experimenting, I have found a solution that works. this is the much more simplified version of it

require_once(facebook/facebook.php");
$config = array();
$config['appId'] = 'XXXXXXXXXXX';
$config['secret'] = 'XXXXXXXXXXXXXXXXXXXXXXXXX';
$config['fileUpload'] = false; // optional

global $facebook;
$facebook = new Facebook($config);

// get feed
function get_fbfeed(){
    global $facebook;
    $pageid = 'XXXXXXX';
    $numberOfPosts = $num;
    $posts = $facebook->api("/" . $pageid . "/feed?limit=" . $numberOfPosts);
    return $posts;
}

function get_fb_image($image){
    global $facebook;
    $post = $facebook->api("/" . $image);
    return $post;
}

and then within my loops using

  <?php $picture = get_fb_image($fb['object_id']); ?>
  <img src="<?php echo $picture['source']; ?>" alt="" />

Which will then get me the default or higher res variation every time.

The only issue with this approach is that if a user posts a gallery it will crash out. I am looking for a fix in another thread.

Upvotes: 2

williamtx
williamtx

Reputation: 81

I'm maintaining code from a long-gone developer, and I'm having the same problem. (I'm working with code based on this: http://www.ipragmatech.com/create-facebook-yiiframework-graphapi.html )

I haven't found a 100% reliable solution, but some of FB feed pictures may have a big resolution available. Using the URL you posted, remove the following URL parts: /v/t1.0-9/s130x130

This should leave you with a URL that displays the big picture:

If you want to use small and large sizes without a lot of URL manupulation, do the above but then change the "_n" in the URL to either "_s" or "_b":

You should be able to use str_replace to do this on the fly with URLs from the API:

str_replace("/v/t1.0-9/s130x130","", $post['picture'])

However, this will only work for photos that follow this specific URL structure. I have seen other feed photos that use a completely different path, so those still display small-size until we can figure them out..

Hope this helps!

Some links where I have been reading:

Upvotes: 1

Related Questions