php代码如下:
<BR><?php <BR>header("Content-type:text/html ; charset=utf-8"); <BR>if (!empty($_POST['submit'])){ <BR>$url = $_POST['url']; <BR>//为了获取相对路径的图片所做的操作 <BR>$url_fields = parse_url($url); <BR>$main_url = $url_fields['host']; <BR>$base_url = substr($url,0,strrpos($url, '/')+1); <BR>//获取网页内容 <BR>//设置代理服务器 <BR>$opts = array('http'=>array('request_fulluri'=>true)); <BR>$context = stream_context_create($opts); <BR>$content = file_get_contents($url,false,$context); <BR>//匹配img标签,将所有匹配字符串保存到数组$matches <BR>$reg = "//i"; <BR>preg_match_all($reg, $content, $matches); <BR>$count = count($matches[0]); <BR>for ($i=0; $i<$count; $i++){ <BR>/*将所有图片的url转换为小写 <BR>*$matches[1][$i] = strtolower($matches[1][$i]); <BR>*/ <BR>//如果图片为相对路径就转化为全路径 <BR>if (!strpos('a'.$matches[1][$i], 'http')){ <BR>//因为'/'是第0个位置 <BR>if (strpos('a'.$matches[1][$i], '/')){ <BR>$matches[1][$i] = 'http://'.$main_url.$matches[1][$i]; <BR>}else{ <BR>$matches[1][$i] = $base_url.$matches[1][$i]; <BR>} <BR>} <BR>} <BR>//过滤重复的图片 <BR>$img_arr = array_unique($matches[1]); <BR>//实例化图片下载类 <BR>$getImg = new DownImage(); <BR>$url_count = count($img_arr); <BR>for ($i=0; $i<$url_count; $i++){ <BR>$getImg->source = $img_arr[$i]; <BR>$getImg->save_address = './pic/'; <BR>$file = $getImg->download(); <BR>} <BR>echo "下载完成!哈哈,简单吧!"; <BR>} <BR>class DownImage{ <BR>public $source;//远程图片URL <BR>public $save_address;//保存本地地址 <BR>public $set_extension; //设置图片扩展名 <BR>public $quality; //图片的质量(0~100,100最佳,默认75左右) <BR>//下载方法(选用GD库图片下载) <BR>public function download(){ <BR>//获取远程图片信息 <BR>$info = @getimagesize($this->source); <BR>//获取图片扩展名 <BR>$mime = $info['mime']; <BR>$type = substr(strrchr($mime, '/'), 1); <BR>//不同的图片类型选择不同的图片生成和保存函数 <BR>switch($type){ <BR>case 'jpeg': <BR>$img_create_func = 'imagecreatefromjpeg'; <BR>$img_save_func = 'imagejpeg'; <BR>$new_img_ext = 'jpg'; <BR>$image_quality = isset($this->quality) ? $this->quality : 100; <BR>break; <BR>case 'png': <BR>$img_create_func = 'imagecreatefrompng'; <BR>$img_save_func = 'imagepng'; <BR>$new_img_ext = 'png'; <BR>break; <BR>case 'bmp': <BR>$img_create_func = 'imagecreatefrombmp'; <BR>$img_save_func = 'imagebmp'; <BR>$new_img_ext = 'bmp'; <BR>break; <BR>case 'gif': <BR>$img_create_func = 'imagecreatefromgif'; <BR>$img_save_func = 'imagegif'; <BR>$new_img_ext = 'gif'; <BR>break; <BR>case 'vnd.wap.wbmp': <BR>$img_create_func = 'imagecreatefromwbmp'; <BR>$img_save_func = 'imagewbmp'; <BR>$new_i<i style="color:transparent">@本文来源gaodai$ma#com搞$代*码6网</i><b>搞代gaodaima码</b>mg_ext = 'bmp'; <BR>break; <BR>case 'xbm': <BR>$img_create_func = 'imagecreatefromxbm'; <BR>$img_save_func = 'imagexbm'; <BR>$new_img_ext = 'xbm'; <BR>break; <BR>default: <BR>$img_create_func = 'imagecreatefromjpeg'; <BR>$img_save_func = 'imagejpeg'; <BR>$new_img_ext = 'jpg'; <BR>} <BR>//根据是否设置扩展名来合成本地文件名 <BR>if (isset($this->set_extension)){ <BR>$ext = strrchr($this->source,"."); <BR>$strlen = strlen($ext); <BR>$newname = basename(substr($this->source,0,-$strlen)).'.'.$new_img_ext; <BR>}else{ <BR>$newname = basename($this->source); <BR>} <br><br>//生成本地文件路径 <BR>$save_address = $this->save_address.$newname; <BR>$img = @$img_create_func($this->source); <BR>if (isset($image_quality)){ <BR>$save_img = @$img_save_func($img,$save_address,$image_quality); <BR>}else{ <BR>$save_img = @$img_save_func($img,$save_address); <BR>} <BR>return $save_img; <BR>} <BR>} <BR>?> <BR> <BR>远程url地址: <BR> <BR> <BR>
运行结果如图:
下载的图片本例中保存在当前目录的pic文件夹下!