1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Adding Images to Autoblog

Discussion in 'Blogging' started by snarf2000, Jul 20, 2009.

  1. snarf2000

    snarf2000 Newbie

    Joined:
    Aug 9, 2008
    Messages:
    48
    Likes Received:
    11
    I have been looking for a good script that can take a keyword and scrape a related image to go along with my auto post. The only one I have found, I can't get to work. I think the author put in some coding errors to weed out the php newbs. Can anyone take a look at this script - it looks promising. Or do you know of another one that will do this?

    Code:
    <?php
    
    /**
     *  GoogleImages - Google Image Scraper
     *
     * Scrapes images off Google image search based on a specified query term.
     * 
     * @copyright  2008 The Dirty Frenchman
     * @license    http://www.gnu.org/licenses/gpl.html GNU General Public License
     * @version    0.1
     * @link       http://www.thedirtyfrenchman.com
     */
    
    class GoogleImages
    {    
         /**
         * getImages : Returns an array containing a thumbnail link, fullsize image link
         * and ALT text link for specified keyword
         * 
         */
        function getImages($keyword)
        {
            $pics = array();
    
            $searchURL = 'http://images.google.com/images?svnum=10&um=1&hl=en&safe=off&q=';
            $searchURL .= urlencode($keyword) . '&btnG=Search+Images"';
            
            $results = file_get_contents($searchURL);
            
            preg_match_all('/dyn.Img.*?;/', $results, $string, PREG_SET_ORDER );
    
            $x=0;
            
            foreach($string as $img)
            {
                $image = explode(',', str_replace("\"",'',$img[0]));
                
                  // Create array based on size
                  if (count($image)==16)
                        {
                                $pics[$x][thumb] = $image[13] . '?q=tbn:' . $image[2] . $image[3];
                                $pics[$x][alt] = str_replace('...','',str_replace('\x3cb\x3e','',(str_replace('\x3c/b\x3e','',$image[6]))));
                                $pics[$x][link] = $image[4];
                        }
                  if (count($image)==17)
                        {
                                $pics[$x][thumb] = $image[14] . '?q=tbn:' . $image[2] . $image[3];
                                $pics[$x][alt] = str_replace('...','',str_replace('\x3cb\x3e','',(str_replace('\x3c/b\x3e','',$image[6]))));
                                $pics[$x][link] = $image[3];
                        }
                  if (count($image)==18)
                        {
                                $pics[$x][thumb] = $image[15] . '?q=tbn:' . $image[2] . $image[3];
                                $pics[$x][alt] = str_replace('...','',str_replace('\x3cb\x3e','',(str_replace('\x3c/b\x3e','',$image[6]))));
                                $pics[$x][link] = $image[3];
                        }
                        $x++;
            }
                    shuffle($pics);
                    return $pics;
        }
    }
    ?>
     
  2. undeterminederror

    undeterminederror BANNED BANNED

    Joined:
    Mar 31, 2008
    Messages:
    630
    Likes Received:
    457
    I'd very appreciate this too :D
     
  3. Migisv2

    Migisv2 Registered Member

    Joined:
    Jun 4, 2009
    Messages:
    87
    Likes Received:
    4
    Occupation:
    Student
    Location:
    Boston
    me as well
     
  4. snarf2000

    snarf2000 Newbie

    Joined:
    Aug 9, 2008
    Messages:
    48
    Likes Received:
    11
    I think I came up with a decent solution. I am cleaning up the script now. It basically passes the title of your post to my script which then scrapes the google images page and randomly selects the url for one of the top 3 images. It then pulls that url from the original host, not google, so you won't get the scraper ip ban hammer. So far, it pulls a relevant image about 80-90% of the time. Sometimes it chooses a bad one depending on what your post title is. One downside is that pages take slightly longer to load sinc eyou are hotlinking the image. I only use this on single post pages, not the main index.

    You can see it working here: http://www.wotdaily.com/2009/07/20/apollo-11-video-restored-national-geographic/
    Relaod it a few times and you will see the image change.

    I'll post the script as soon as it is done.
     
    Last edited: Jul 21, 2009
  5. iglow

    iglow Elite Member

    Joined:
    Feb 20, 2009
    Messages:
    2,081
    Likes Received:
    856
    Home Page:
    Dont seem to work..