Home  >  Article  >  Backend Development  >  The gd library image download class implements the php code for downloading all images on the web page_PHP tutorial

The gd library image download class implements the php code for downloading all images on the web page_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:17:27712browse

The php code is as follows:

Copy code The code is as follows:

header("Content-type: text/html ; charset=utf-8");
if (!empty($_POST['submit'])){
$url = $_POST['url'];
//For Operations to obtain images with relative paths
$url_fields = parse_url($url);
$main_url = $url_fields['host'];
$base_url = substr($url,0,strrpos( $url, '/')+1);
//Get web content
//Set proxy server
$opts = array('http'=>array('request_fulluri'=>true ));
$context = stream_context_create($opts);
$content = file_get_contents($url,false,$context);
//Match the img tag and save all matching strings to the array$ matches
$reg = "//i";
preg_match_all($reg, $content, $matches);
$count = count($matches[0]);
for ($i=0; $i<$count; $i++){
/*Convert all image urls to lowercase
* $matches[1][$i] = strtolower($matches[1][$i]);
*/
//If the image is a relative path, convert it to a full path
if (!strpos ('a'.$matches[1][$i], 'http')){
//Because '/' is the 0th position
if (strpos('a'.$matches[1 ][$i], '/')){
$matches[1][$i] = 'http://'.$main_url.$matches[1][$i];
}else {
$matches[1][$i] = $base_url.$matches[1][$i];
}
}
}
//Filter duplicate images
$img_arr = array_unique($matches[1]);
//Instantiate the image download class
$getImg = new DownImage();
$url_count = count($img_arr);
for ($i=0; $i<$url_count; $i++){
$getImg->source = $img_arr[$i];
$getImg->save_address = './pic/';
$file = $getImg->download();
}
echo "Download completed! Haha, keep it simple! ";
}
class DownImage{
public $source;//Remote image URL
public $save_address;//Save local address
public $set_extension; //Set image extension
public $quality; //The quality of the picture (0~100, 100 is the best, the default is about 75)
//Download method (select GD library image download)
public function download(){
// Get remote image information
$info = @getimagesize($this->source);
//Get the image extension
$mime = $info['mime'];
$type = substr(strrchr($mime, '/'), 1);
//Select different image generation and saving functions for different image types
switch($type){
case 'jpeg':
$img_create_func = 'imagecreatefromjpeg';
$img_save_func = 'imagejpeg';
$new_img_ext = 'jpg';
$image_quality = isset($this->quality) ? $this-> quality : 100;
break;
case 'png':
$img_create_func = 'imagecreatefrompng';
$img_save_func = 'imagepng';
$new_img_ext = 'png';
break;
case 'bmp':
$img_create_func = 'imagecreatefrombmp';
$img_save_func = 'imagebmp';
$new_img_ext = 'bmp';
break;
case ' gif':
$img_create_func = 'imagecreatefromgif';
$img_save_func = 'imagegif';
$new_img_ext = 'gif';
break;
case 'vnd.wap.wbmp':
$img_create_func = 'imagecreatefromwbmp';
$img_save_func = 'imagewbmp';
$new_img_ext = 'bmp';
break;
case 'xbm':
$img_create_func = '
$img_save_func = 'imagexbm';
$new_img_ext = 'xbm';
break;
default:
$img_create_func = 'imagecreatefromjpeg';
$img_save_func = ' imagejpeg';
$new_img_ext = 'jpg';
}
//Synthesize the local file name according to whether the extension is set
if (isset($this->set_extension)){
$ext = strrchr($this->source,".");
$strlen = strlen($ext);
$newname = basename(substr($this->source,0,- $strlen)).'.'.$new_img_ext;
}else{
$newname = basename($this->source);
}

//Generate local file path
$save_address = $this->save_address.$newname;
$img = @$img_create_func($this->source);
if (isset($image_quality)){
$ save_img = @$img_save_func($img,$save_address,$image_quality);
}else{
$save_img = @$img_save_func($img,$save_address);
}
return $save_img;
}
}
?>

Remote url address:



The running results are as shown below:

gd库图片下载类实现下载网页所有图片The downloaded pictures are saved in the pic folder of the current directory in this example!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325724.htmlTechArticlephp code is as follows: Copy the code as follows: ?php header("Content-type:text/html; charset= utf-8"); if (!empty($_POST['submit'])){ $url = $_POST['url']; //To get the relative path image...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn