Download images from Google Image search results

Download images from Google Image search results using PHP
<?php
class downloadgimag{

    function downloadto($foldername,$to,$size,$sf) {

        // create directory if not exist
        if (!is_dir($foldername)) {
            mkdir("$foldername",0755);
        }
        
        // download pattern you can modify it
        $pattern='/http[a-z.\/\/-a-z_]{2,129}[a-z0-9_-]{1,90}.(gif|png|jpg)/';

        //getting url addresses
        for ($i = 0; $i <= $to; $i += 20) {
            $gurl = "http://www.google.com/search?q=".rawurlencode($foldername)."&hl=en&gbv=2&tbm=isch&prmd=ivns&ei=GWPxTvPKNM2srAfKyPnPDw&start=".$i."&sa=N$sf$size";
            $pg = file_get_contents("$gurl");
            preg_match_all($pattern, $pg, $returnArray);
            $matches = $returnArray[0];
            $count = count($matches);
            foreach( $matches as $key){
                $randname = $foldername."/".rand(9,999999999).".jpg";
                file_put_contents("$randname", file_get_contents($key));
            }

        }
    }
}
?> 

You can download the script by click on the url bellow:
http://www.phpclasses.org/browse/file/38086.html

Related Posts by Categories



0 comments:

Post a Comment