2

When sending a message on Facebook, if you include a URL it generally grabs a picture from the webpage and adds it at the bottom as a thumbnail. You then have the ability to select through a number of pictures featured on the site.

I can see how this could be built, but to save me the hassle I wonder if somebody has already done it already in a publicly available format?

Thanks!

  • 1
    Had you asked for an implementation, you'd probably have one by now :) – Tatu Ulmanen Jan 06 '10 at 00:34
  • i thought i was asking for an implementation :) –  Jan 07 '10 at 14:26
  • See: - http://stackoverflow.com/questions/125951/command-line-program-to-create-website-screenshots-on-linux - http://stackoverflow.com/questions/968201/convert-web-page-to-image – gabrielk Jan 06 '10 at 03:53

2 Answers2

4

Alright, the code sample I prepared for you was too long to add as a comment of the first. So here's the correct code, verified to work on my local PHP environment (5.3.1):

<?php
/**
 * Sample CURL Image Extractor
 * 
 * Prepared for stackoverflow.com 
 *
 * @author Sam Skjonsberg <skoneberg@gmail.com>
 **/

if(!function_exists('json_encode'))
{
    die('You need at least PHP 5.2 to use this script.');
}

//
// JSON & No-Cache Headers
// Uncoment when implemented as an actual JSON service
//
//header('Cache-Control: no-cache, must-revalidate');
// Date in the past to ensure it isn't cached
//header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
//header('Content-type: application/json');
//

//$url      =   parse_url($_REQUEST['url']);
// Harcoded url for demonstration
// Shameless self-plug :)
$url        =   'http://www.codeviking.net';

if(!empty($url))
{       

    if(!preg_match('%^https?://%i', $url))
    {
        echo get_json_error('Invalid URL');
    }

    $ch     =   curl_init();

    curl_setopt_array(  
                        $ch,    
                        array
                        (
                            CURLOPT_URL             =>  $url,
                            CURLOPT_RETURNTRANSFER  =>  true,
                            CURLOPT_FOLLOWLOCATION  =>  true
                        )
                     );

    $html   =   curl_exec($ch);

    if(!empty($html)) 
    {
        $doc                =   new DOMDocument($html);

        $doc->loadHTML($html);

        $images             =   $doc->getElementsByTagName('img');

        $image_srcs         =   array();

        foreach($images as $img) {
            foreach($img->attributes as $attribute_name => $attribute_node)
            {
                if($attribute_name == 'src')
                {
                    $src            =   $attribute_node->nodeValue;

                    // Parse image into absolute URL
                    if(!preg_match('%^https?://%i', $src))
                    {
                        $src    =   $url . '/' . preg_replace('%^\.?/%', '', $src);                         
                    }

                    $image_srcs[]   =   $src;

                }
            }
        }

        echo json_encode($image_srcs);  

        // And there you have it, a collection of image
        // paths to parse through on the client and add <img src="image_src" /> for each.
        //
        // So, for instance as your ajax success callback
        //
        //
        // var images = parseJSON(image_json);
        // for(var i = 0; i < images.length; i++)
        // {
        //  image_src = images[i];
        //  /* Requires jQuery */
        //  $('body').append($('<img />').attr('src', image_src));
        // }
    } 
    else 
    {
        echo get_json_error('Invalid URL');
    }
} 
else 
{
    echo get_json_error('Invalid URL');
}

function get_json_error($msg)
{   
    $error  =   array('error' => $msg);
    return json_encode($error);
}

That really should work. Also I'd appreciate a vote up on the answer as I'm trying to break the 100 point mark! Thanks and good luck!

Uyghur Lives Matter
  • 18,820
  • 42
  • 108
  • 144
Skone
  • 745
  • 4
  • 13
1

Really shouldn't be hard to implement. Coding is fun, take this and roll with it:

$ch = curl_init();

curl_setopt_array($ch, array(CURLOPT_URL => $_POST['url'], CURLOPT_FOLLOWLOCATION => true, CURLOPT_RETURNTRANSFER, true));

$results = curl_exec($ch);

$doc = new DOMDocument();

$doc->loadHTML($results);

$images = $doc->getElementsByTagName('img');

This should return a DOMNodeList I believe -- from there you iterate through and pull out the src attribute for each image, stick it into json_encode(), and then write a nice webservice to submit a url and return the nice little collection of images.

I realize its not what you're asking for, but its a start.

Skone
  • 745
  • 4
  • 13
  • thanks mate, it's more so the snazzy UI that FB has that I think will take the time to get built.. no big deal if i have to build it from scratch i guess... just always try and avoid reinventing the wheel. –  Jan 07 '10 at 11:56
  • I'm trying to run the example,I have tried to implement a 'basic' script: $ch = curl_init(); $url = "http://www.altavista.com"; curl_setopt_array($ch, array(CURLOPT_URL => $url, CURLOPT_FOLLOWLOCATION => true, CURLOPT_RETURNTRANSFER, true)); $results = curl_exec($ch); $doc = new DOMDocument(); $doc->loadHTML($results); $images = $doc->getElementsByTagName('img'); var_dump($images); ?> however i get this back: Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: Empty string supplied as input in /Applications/MAMP/htdocs/raw.php on line 11 object(DOMNodeList)#2 (0) { } –  Jan 07 '10 at 15:21
  • i wish i knew how to paste code here, sorry i'm a newbie.. spent 20 minutes trying to work it out :( –  Jan 07 '10 at 15:22
  • Give me till this evening when I'm off -- I'll take a look and help you out :) – Skone Jan 07 '10 at 22:22