Skip to content
조회 수 34918 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

.htaccess와 워터마킹을 이용한 이미지 링크 방지


개인 계정에서 블로그등의 사이트를 운영하시는 분들 중 가끔 외부에서의 컨텐츠 직링크로 인해 트래픽 초과에 걸리시는 분들이 많습니다. 
트래픽 문제가 아니더라도, 무단 링크로 인해 저작권을 침해받는 경우도 있겠죠. 이를 해결하기 위해 레퍼러비교 같은 방법도 쓰곤 하는데, 아마 .htaccess 파일을 이용한 것도 많이들 아실 겁니다. 간단히, .htaccess와 워터마킹 기술을 이용해 이미지 링크 제어를 하는 법을 소개합니다. 일단, 이미지들이 저장되는 디렉토리에 .htaccess파일을 만듭니다. (이미 존재한다면 아래의 소스를 마지막에 추가하시면 됩니다.) RewriteEngine On RewriteCond %{REQUEST_FILENAME} .*jpg$|.*gif$|.*png$ [NC] RewriteCond %{HTTP_REFERER} !^$ RewriteCond %{HTTP_REFERER} !explug\.com [NC] RewriteCond %{HTTP_REFERER} !eouia0\. [NC] RewriteCond %{HTTP_REFERER} !google\. [NC] RewriteCond %{HTTP_REFERER} !search\?q=cache [NC] RewriteRule (.*) image.php?image=$1 ........... 잠깐 설명을 하자면, RewriteCond %{HTTP_REFERER} !explug\.com [NC] RewriteCond %{HTTP_REFERER} !eouia0\. [NC] RewriteCond %{HTTP_REFERER} !google\. [NC] RewriteCond %{HTTP_REFERER} !search\?q=cache [NC] 이 부분에 링크를 허용할 URL들을 기재합니다. 정규식 패턴이므로 잘 알아서.. :) 위의 예의 경우 explug.com, eouia0가 포함되는 사이트, google 등에 링크를 허용한 경우입니다. 이 경우에는 원본 이미지를 마음대로 가져다 쓸 수 있습니다. 그외의 URL에서 링크가 걸릴 경우에는 RewriteRule (.*) image.php?image=$1 에 따라, image.php?image=파일이름 으로 리다이렉트됩니다. 만약 그냥 링크 자체를 끊고 싶으시다면 여기에 아무거나 써주셔도 되지요. 이제 워터마킹 처리를 위해 image.php를 작성합니다. GD라이브러리를 사용했으므로 GD가 사용가능한 계정이어야 합니다. <? define ("WATERMARK", "watermark.jpg"); header("Content-type: image/jpeg"); header("Cache-Control: no-store, no-cache, must-revalidate"); header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); $pic = strip_tags( $_GET['image'] ); $image_url = urldecode($pic); if (!@fopen($image_url, "r")) { $dst_img = imagecreatefromjpeg(WATERMARK); $res = imagejpeg($dst_img, "", 100); die(); } $file = pathinfo($image_url); switch(strtoupper($file["extension"])) { case "JPG": $src_img = imagecreatefromjpeg($image_url); break; case "GIF": $src_img = imagecreatefromgif($image_url); break; case "PNG": $src_img = imagecreatefrompng($image_url); break; } $src_w = imagesx($src_img); $src_h = imagesy($src_img); $portion = $src_h / $src_w; $dest_w = 300; $dest_h = round($dest_w * $portion); $dst_img = imagecreatetruecolor($dest_w, $dest_h); imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $dest_w, $dest_h, $src_w, $src_h); $watermark = imagecreatefromjpeg(WATERMARK); $watermark_w = imagesx($watermark); $watermark_h = imagesy($watermark); $overlay_img = imagecreatetruecolor($watermark_w, $watermark_h); imagecopy($overlay_img, $watermark, 0,0,0,0, $watermark_w, $watermark_h); imagedestroy($watermark); $white = imagecolorallocate($overlay_img, 0xFF, 0xFF, 0xFF); imagecolortransparent($overlay_img, $white); $offsetX = $dest_w - $watermark_w - 3; $offsetY = $dest_h - $watermark_h - 3; imagecopymerge($dst_img,$overlay_img,$offsetX,$offsetY,0,0,$watermark_w,$watermark_h, 100); imagedestroy($overlay_img); $res = imagejpeg($dst_img, "", 100); imagedestroy($dst_img); ?> 실행예는 링크를 보시면 이해하실 수 있을 겁니다. 
링크를 허용한 사이트에서는 원본이, 링크를 허용하지 않은 사이트에서는 image.php에 의해 워터마크처리된 이미지로 링크가 걸리게 됩니다. 
여기에서는 예를 위해 워터마킹 처리된 이미지의 사이즈를 가로 300으로 고정시켰지만 이 부분은 적당히 소스를 고치시면 원하는 대로 적용시키실 수 있으실 겁니다.

로그인 후 댓글쓰기가 가능합니다.

?

List of Articles
번호 분류 제목 날짜 조회 수
897 AutoHotKey ahk) Send an email 메일발송하기 2018.01.17 9390
896 AutoHotKey AHK) 보안프로그램 등으로 화면복사(Printscreen) 안될때 사용방법 1 12 file 2012.11.21 47165
895 AutoHotKey ahk) 열려진 엑셀창의 값 불러오기 1 2013.10.30 32529
894 AutoHotKey ahk) 오토핫키 콤보박스 제어하기 file 2013.10.30 38180
893 AutoHotKey Ahk) 웹페이지 감시결과에 따라 마이피플로 글 전송하기 12 file 2013.01.06 44023
892 AutoHotKey ahk_l 웹페이지 앞, 뒤페이지 제어 예제소스 및 설명첨부 2011.02.22 17535
891 AutoHotKey ahk_l 과 com 의 이해 2011.02.22 17485
890 AutoHotKey AHK_L 예제소스 1 2011.02.10 17015
889 AutoHotKey ahk_l 웹페이지 파일로 저장한 뒤 불러와 필요한 부분 추출하여 출력하기 2011.02.22 16992
888 컴퓨터잡담 AHK_L) SysListView321 컨트롤 내용 추출하기 2011.10.07 9893
887 AutoHotKey ahk로 만든 파일을 exe로 컴파일 한 후 실행시킬때 변수를 임의 1 1 2011.02.24 15333
886 AutoHotKey AHK에서 가능한 COM 인터넷 익스플로러 및 GUI 브라우저 1 2011.02.11 19502
885 AutoHotKey ahk와 ahk_l 의 웹페이지 로딩완료 체크 비교 3 2011.02.11 20324
884 컴퓨터잡담 ajax의 XMLHttpRequest 객체 1 2010.03.27 7463
883 WindowsTip An error [-5001 : 0x80070002] ha occurred while running the setup 오류 해결방법 file 2013.06.14 21935
882 Server Apache & Mysql 셋팅방법(httpd-mpm.conf / my.ini) 2013.05.13 13707
881 Server APACHE PHP 에러 잡기 1 2016.03.23 9960
880 Server Apache에 대한 mod_proxy 지원 구성 2016.09.06 1801
879 컴퓨터잡담 API Hooking 유저 레벨 루트킷 1 2010.01.21 11623
878 Server APMSETUP7 PHP 업그레이드 2015.06.02 6373
Board Pagination Prev 1 2 3 4 5 ... 46 Next
/ 46

http://urin79.com

우린친구블로그

sketchbook5, 스케치북5

sketchbook5, 스케치북5

나눔글꼴 설치 안내


이 PC에는 나눔글꼴이 설치되어 있지 않습니다.

이 사이트를 나눔글꼴로 보기 위해서는
나눔글꼴을 설치해야 합니다.

설치 취소