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

高洛峰
Release: 2023-03-02 16:50:02
Original
1122 people have browsed it

The php code is as follows:
Copy the code The code is as follows:
header("Content-type:text/html ; charset=utf-8");
if (!empty($_POST['submit']) ){
$url = $_POST['url'];
//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 the urls of all images to lowercase
*$matches[1][$i] = strtolower( $matches[1][$i]);
*/
//If the image is a relative path, convert it to the 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 the local address
public $set_extension; //Set the image extension
public $quality; //The quality of the image ( 0~100, 100 is the best, the default is around 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 images for different image types Generate and save functions
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';
$im g_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 = 'imagecreatefromxbm';
$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:

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!