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

Google Image Scraper script

Discussion in 'PHP & Perl' started by snarf2000, Jul 18, 2009.

  1. snarf2000

    snarf2000 Newbie

    Joined:
    Aug 9, 2008
    Messages:
    48
    Likes Received:
    11
    I need some help with this scraper script. The author put in some coding errors to weed out newbs like me. I have some basic php skills but can't get it to work. Thanks to anyone who can help.

    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;
        }
    }
    ?>
     
    • Thanks Thanks x 1
  2. snarf2000

    snarf2000 Newbie

    Joined:
    Aug 9, 2008
    Messages:
    48
    Likes Received:
    11
    Bump. Any takers?