외부 이미지 링크가 금지된 사이트의 이미지 가져 오기 > php

본문 바로가기
사이트 내 전체검색

php

외부 이미지 링크가 금지된 사이트의 이미지 가져 오기

페이지 정보

작성자 서방님 댓글 0건 조회 155회 작성일 13-07-24 14:16

본문

외부 이미지 링크를 막은 사이트의 경우, 대부분 접속자의 Referer를 조사하여 동일 사이트가 아닌 곳에서 접속하면 이를 차단하거나 링크금지 안내문 이미지로 돌리게 된다.
따라서, 이미지를 요청할 때 header에 Referer를 해당 사이트 주소로 넣으면 문제 없이 읽어 올 수 있다.

CURL을 사용한 예제

## get_image.php ##########
<?
 if($_GET[p]) {
  $tUrl = $_GET[p]; //가져올 이미지의 주소
  $rUrl = $_GET[r]; //Referer
  $path_parts = pathinfo($tUrl);
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $tUrl);
  curl_setopt($ch, CURLOPT_REFERER, $rUrl);
  curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 1.1.4322; .NET CLR 3.0.04506.30) '); //브라우저 종류 - Explorer 7.0
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
  curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
  curl_setopt($ch, CURLOPT_HEADER, false);
  $f = curl_exec($ch);
  curl_close($ch);
 
  $len = strlen($f);
  header("Content-type: image/jpeg"); //.jpg 만 지원.. gif, png 등은 $tUrl을 파싱하여 적절히 조치하면 됨..
  header("Content-Disposition: attachment; filename=".$path_parts['basename']); //우클릭으로 저장할 때 bmp 로 저장되는 문제 해결
  header("Content-Transfer-Encoding: binary");
   header("Content-Length: ".$len);
  echo ($f);
 }
?>


### 사용 예제 #########
<img src="get_img.php?p=http://some.where/img/some.jpg&r=http://some.where">

댓글목록

등록된 댓글이 없습니다.

회원로그인

접속자집계

오늘
229
어제
225
최대
1,347
전체
154,988
Latest Crypto Fear & Greed Index

그누보드5
Copyright © 서방님.kr All rights reserved.