Location: PHPKode > scripts > Download Images from Google > download-images-from-google/ClassToDownloadImages.inc.php
<?php
/**
 * To download images of mentioned celebrity/person from google images
 *
 *
 * @requirnments: cURL enabled.
 *
 * @author Rochak Chauhan
 */
class ClassToDownloadImages {
	var $sizeLimit = "200";
	var $imagesOf = "Lindsay";
	var $numberOfImages = "20";
	var $dir = "";
	
	function returnImageUrl($data) {
		$returnArray = Array();
			
		preg_match_all("/(imgurl=)(.*?)(&)/i", $data, $links);
		
		foreach($links[2] as $link) {
			$returnArray[] = rawurldecode($link);			
		}
		return $returnArray;
	}

	/**
	 * Function the create a list of google image urls
	 *
	 * @return array
	 */
	function createUrl() {
		$imagesOf = $this->imagesOf;
		$numberOfImages = $this->numberOfImages+20;

		$numberOfPages = ($numberOfImages/20)+1;
		$j=0;

		for($i=0; $i<$numberOfPages; $i++ ) {
			$returnArray[] = "http://images.google.com/images?q=".rawurlencode($imagesOf)."&hl=en&lr=&start=".$j."&sa=N";
			$j += 20;
		}
		return $returnArray;
	}
	
	/**
	 * Function to download/copy the images in the provided array
	 *
	 * @param array $imgUrlArray
	 * 
	 * @return void
	 */
	function downloadImages($imgUrlArray) {
		$serverSoftware = $_SERVER['SERVER_SOFTWARE'];

		if(stripos($serverSoftware, "Win32") > 0) {
			//For Windows based System
			$dir = $_SERVER['DOCUMENT_ROOT']."/myclasses/ImageDownloader/".$this->imagesOf;
			@mkdir($dir, 0777);
		}
		else {
			//For UNIX based System
			// Note: you should havewrite permissions
			$dir = $_SERVER['DOCUMENT_ROOT']."/".$this->imagesOf;
			@mkdir($dir, 0777);
			chmod($dir, 0777);
		}
		$this->dir = $dir;
		
		$imgUrlArray = $imgUrlArray[0];
		
		for($i=0; $i<count($imgUrlArray); $i++) {
			$imgArray = explode("/", $imgUrlArray[$i]);
			$imageName = end($imgArray);
			$info = @getimagesize($imgUrlArray[$i]);
			if(trim($this->sizeLimit) != "" && $this->sizeLimit > 0) {
				if (count($info) > 0 && $info[0] >= $this->sizeLimit) {
					if(trim($imageName) != '' ) {
						copy($imgUrlArray[$i], $dir."/[$i]".$imageName);
					}
				}
			}
			else {
				if(trim($imageName) != '' ) {
					copy($imgUrlArray[$i], $dir."/[$i]".$imageName);
				}
			}
		}
	}
	/**
	 * Function to extract array of images via cURL
	 *
	 * @param  array $arrayOfUrls
	 * 
	 * @return void
	 */
	function getImagesByCurl($arrayOfUrls) {
		
		for($i=0; $i<count($arrayOfUrls); $i++) {
			$ch = curl_init($arrayOfUrls[$i]);
			$fp = fopen(TEMP_FILE, "w");
			curl_setopt($ch, CURLOPT_FILE, $fp);
			curl_setopt($ch, CURLOPT_HEADER, 0);
			curl_exec($ch);
			curl_close($ch);
			fclose($fp);

			$fileContents = file_get_contents(TEMP_FILE);
			$tmpStartPos = strpos($fileContents, '<td align=center valign=middle width=23%>');
			$startPos = $tmpStartPos+41;
			$endPos = strrpos($fileContents, '</font></font></font>');
			$charsToRead = $endPos - $startPos;
			$string = substr($fileContents, $startPos, $charsToRead);
			
			$imgUrl = $this->returnImageUrl($string);
			$downloadImagesArray[] = $imgUrl;
		}		
		$this->downloadImages($downloadImagesArray);
	}
	/**
	 * Function to download images from images.google
	 *
	 * @param string $imagesOf
	 * @param int $numberOfImages
	 * @param int $sizeLimit [OPTIONAL]
	 */
	function downloadImagesFromGoogle($imagesOf, $numberOfImages, $sizeLimit = 0) {
		$this->sizeLimit = $sizeLimit;
		$this->imagesOf = $imagesOf;
		$this->numberOfImages = $numberOfImages;
		$urlArray = $this->createUrl();
		$this->getImagesByCurl($urlArray);
	}
}
Return current item: Download Images from Google