워터마크 질문

워터마크 질문

QA

워터마크 질문

본문

https://tonhnegod.tistory.com/entry/그누보드-게시판-이미지-업로드시-워터마크-삽입하기

 

위 게시물을 보고 워터마크 적용을 하려고 하는데요.

 




 

<?php
function add_watermark_text($image_path, $text, $font) {
    
    // $image_path, $font는 반드시 절대경로로 지정해야함 (url이 아닌 path)
    
    $array_img_chk = array("jpg", "jpeg", "png", "gif", "bmp");
    
    // 이미지 확장자
    $img_ext = explode(".", strrev($image_path));
    $img_ext = strrev($img_ext[0]);
    $img_ext = strtolower($img_ext);
    
    // 이미지 파일인 경우에만 진행
    if(in_array($img_ext, $array_img_chk)) {
 
        if($img_ext == 'jpg' || $img_ext == 'jpeg')
            $create_img = imagecreatefromjpeg($image_path);
 
        if($img_ext == 'png')
            $create_img = imagecreatefrompng($image_path);
 
        if($img_ext == 'gif')
            $create_img = imagecreatefromgif($image_path);
 
        if($img_ext == 'bmp')
            $create_img = imagecreatefromwbmp($image_path);
 
        if($create_img) {
 
            imagealphablending($create_img, true);
            
            // 워터마크 폰트 색상 (RGB 값)
            $color = imagecolorallocate($create_img, 0, 0, 0);
            
            imagettftext($create_img, 20, 0, 2, 20, $color, $font, $text);
 
            /* imagettftext 설명 */
            // 원본 이미지 리소스 : $create_img
            // 폰트 크기 : 20
            // 폰트 각도 : 0
            // 폰트 위치 x : 2
            // 폰트 위치 y : 20
            // 텍스트 색상 : $watermark_color (rgb값)
            // 텍스트 폰트 : $font
            // 텍스트 내용 : $text
 
            //header("Content-type: image/jpeg");
            imagejpeg($create_img, $image_path);
 
            imagedestroy($create_img);
        }
    }
}
 
function add_watermark_image($image_path, $watermark_path) {
    
    // $image_path, $watermark_path 는 반드시 절대경로로 지정해야함 (url이 아닌 path)
    
    $array_img_chk = array("jpg", "jpeg", "png", "gif", "bmp");
    
    // 이미지 확장자
    $img_ext = explode(".", strrev($image_path));
    $img_ext = strrev($img_ext[0]);
    $img_ext = strtolower($img_ext);
    
    // 이미지 파일인 경우에만 진행
    if(in_array($img_ext, $array_img_chk)) {
 
        if($img_ext == 'jpg' || $img_ext == 'jpeg')
            $create_img = imagecreatefromjpeg($image_path);
 
        if($img_ext == 'png')
            $create_img = imagecreatefrompng($image_path);
 
        if($img_ext == 'gif')
            $create_img = imagecreatefromgif($image_path);
 
        if($img_ext == 'bmp')
            $create_img = imagecreatefromwbmp($image_path);
 
        if($create_img) {
            
            // 워터마크 이미지 확장자
            $watermark_img_ext = explode(".", strrev($watermark_path));
            $watermark_img_ext = strrev($watermark_img_ext[0]);
            $watermark_img_ext = strtolower($watermark_img_ext);
 
            if($watermark_img_ext == 'jpg' || $watermark_img_ext == 'jpeg')
                $create_watermark_img = imagecreatefromjpeg($watermark_path);
 
            if($watermark_img_ext == 'png')
                $create_watermark_img = imagecreatefrompng($watermark_path);
 
            if($watermark_img_ext == 'gif')
                $create_watermark_img = imagecreatefromgif($watermark_path);
 
            if($watermark_img_ext == 'bmp')
                $create_watermark_img = imagecreatefromwbmp($watermark_path);
 
            if($create_watermark_img) {
 
                list($img_w, $img_h) = getimagesize($image_path);
                list($watermark_img_w, $watermark_img_h) = getimagesize($watermark_path);
 
                imagealphablending($create_img, true);
                
                // 워터마크 위치 지정
                //$pos_x = 50;
                //$pos_y = 50;
 
                // (예시) 워터마크를 정중앙으로
                $pos_x = ceil(($img_w - $watermark_img_w) / 2);
                $pos_y = ceil(($img_h - $watermark_img_h) / 2);
                
                // 워터마크 삽입
                imagecopy($create_img, $create_watermark_img, $pos_x, $pos_y, 0, 0, $watermark_img_w, $watermark_img_h);
 
                /* imagecopy 설명 */
                // 원본 이미지 리소스 : $create_img
                // 워터마크 이미지 리소스 : $create_watermark_img
                // 워터마크 이미지 x 좌표 : $pos_x
                // 워터마크 이미지 y 좌표 : $pos_y
                // 원본 이미지의 x 좌표 : 0
                // 원본 이미지의 y 좌표 : 0
                // 워터마크 이미지 가로크기 : $watermark_img_w
                // 워터마크 이미지 세로크기 : $watermark_img_h
                
                //header("Content-type: image/jpeg");
                imagejpeg($create_img, $image_path);
 
                imagedestroy($create_img);
                imagedestroy($create_watermark_img);
            }
        }
    }
}
?>
 

위에서 언급하는 ↓↓↓↓↓

$image_path(원본 이미지), $font(폰트TTF파일), $watermark_path(워터마크 이미지)는 
반드시 절대 경로로 지정하셔야 합니다.

↑↑↑↑↑↑↑↑


이 부분을 어떻게 해야하는지 이해가 안가서그러는데 워터마크 이미지는 /img/watermark.png 를 적용하려면 어떻게 설정을 해야할지 좀 알려주시면 감사하겠습니다ㅠ

이 질문에 댓글 쓰기 :

답변 2

다음과 같은 디렉토리 구조에서


.
├── bin
├── dev
├── etc
├── home
│   └── user1
│       └── public_html (domain.com DOCUMENT_ROOT)
│           ├── data
│           │   └── dbconfig.php
│           ├── img
│           │   └── watermark.png
│           └── index.php
├── media
├── mnt
├── usr
└── var


domain.com/img/watermark.png 파일을 
domain.com/index.php 에서 기술할때

 

상대경로는 다음과 같이 표현하고
img/watermark.png
./img/watermark.png

 

절대경로는 다음과 같이 표현합니다.
/home/user1/public_html/img/watermark.png

 

따라서 함수를 호출할때 다음과 같은 형태가 되어야 합니다.


$image_path = '/home/user1/public_html/img/image.png';
$font = '/absolute/path/from/font.ttf';
$text = 'text';
$watermark_path = '/home/user1/public_html/img/watermark.png';
 
add_watermark_text($image_path, $text, $font);
add_watermark_image($image_path, $watermark_path);
답변을 작성하시기 전에 로그인 해주세요.
전체 222
QA 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT