>  기사  >  백엔드 개발  >  gd 라이브러리 이미지 다운로드 클래스는 웹 페이지의 모든 이미지를 다운로드하기 위한 PHP 코드를 구현합니다.

gd 라이브러리 이미지 다운로드 클래스는 웹 페이지의 모든 이미지를 다운로드하기 위한 PHP 코드를 구현합니다.

高洛峰
高洛峰원래의
2016-12-01 10:34:531087검색

php 코드는 다음과 같습니다.
코드를 복사합니다. 코드는 다음과 같습니다.
header("Content-type:text/html ; charset=utf-8") ;
if (!empty($_POST['submit'])){
$url = $_POST['url']
//상대 경로를 사용하여 이미지를 얻는 작업
$url_fields = pars_url($ url);
$main_url = $url_fields['host'];
$base_url = substr($url,0,strrpos($url, '/')+1); //웹페이지 콘텐츠 가져오기
//프록시 서버 설정
$opts = array('http'=>array('request_fulluri'=>true))
$context = stream_context_create($ opts);
$content = file_get_contents($url,false,$context)
//img 태그를 일치시키고 일치하는 모든 문자열을 $matches 배열에 저장합니다.
$reg = "//i";
preg_match_all($reg, $content, $matches);
$count = count($matches[0]) ;
for ( $i=0; $i<$count; $i++){
/*모든 이미지의 URL을 소문자로 변환
*$matches[1][$i] = strtolower( $matches[1][ $i]);
*/
//이미지가 상대 경로인 경우 전체 경로로 변환합니다.
if (!strpos('a'.$matches[ 1][$i], 'http' )){
//'/'가 0번째 위치이므로
if (strpos('a'.$matches[1][$i], '/' )){
$matches[ 1][$i] = 'http://'.$main_url.$matches[1][$i]
}else{
$matches[1] [$i] = $base_url.$ match[1][$i];
}
}
}
//중복 이미지 필터링
$img_arr = array_unique($matches[1 ]);// 이미지 다운로드 클래스 인스턴스화
$getImg = new DownImage()
$url_count = count($img_arr)
for ($i=0; $i<$url_count; ; $i++){
$getImg->source = $img_arr[$i]
$getImg->save_address = './pic/'; download();
}
echo "다운로드가 완료되었습니다! 하하, 간단하게 하세요! ";
}
class DownImage{
public $source;//원격 이미지 URL
public $save_address;//로컬 주소 저장
public $set_extension; //이미지 확장자 설정
public $quality; //이미지 품질(0~100, 100이 가장 좋음, 기본값은 75 정도)
//다운로드 방법(GD 라이브러리 이미지 다운로드 선택)
public 함수 download() {
// 원격 이미지 정보 가져오기
$info = @getimagesize($this->source)
//이미지 확장자 가져오기
$mime = $info['mime']; 🎜>$type = substr(strrchr($mime, '/'), 1)
//이미지 유형별로 다른 이미지 생성 및 저장 기능 선택
switch($type){
case ' jpeg':
$img_create_func = 'imagecreatefromjpeg';
$img_save_func = 'imagejpeg';
$new_img_ext = 'jpg'; this-> 품질:
break
case 'png':
$img_create_func = 'imagecreatefrompng'
$img_save_func = 'png' ;
break;
case 'bmp':
$img_create_func = 'imagecreatefrombmp';
$img_save_func = 'imagebmp';
break; 🎜>case 'gif':
$img_create_func = 'imagecreatefromgif';
$img_save_func = 'imagegif';
$new_img_ext = 'gif'
case 'vnd.wap; .wbmp':
$img_create_func = 'imagecreatefromwbmp';
$img_save_func = 'imagewbmp';
$new_img_ext = 'bmp'
case 'xbm':
$img_create_func = 'imagecreatefromxbm';
$new_img_ext = 'xbm';
기본값:
$img_createfromjpeg'; $img_save_func = ' imagejpeg';
$new_img_ext = 'jpg'
}
//확장자 설정 여부에 따라 로컬 파일 이름을 합성합니다.
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)
}

//로컬 파일 경로 생성
$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)
}
$save_img 반환
}
}
?>


원격 URL 주소: ;input type="submit" name="submit" value="이 페이지의 모든 이미지 다운로드" />

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.