메뉴 건너뛰기

프로그램언어

조회 수 19667 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

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

단축키

Prev이전 문서

Next다음 문서

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

/******************************************************************************

Program: waterMark.php
Description : 서비스 이미지에 워터마크 적용하기
Author      : Ryu Jee Hyoung
CreateDate  : 2007.01.18
CopyRight   : Copyright(c) 청명공자 All Right Reserved.
UpdateDate  :

Todo        :

 워터마크가 적용되도록 이미지 URL 변경
 - 사용법
 > $img_url = http://localhost/images/test.jpg;
 > $img_url = waterMark($img_url);
 > echo $img_url;

******************************************************************************/

function waterMark($image,$thumbW=null,$thumbH=null)
{
  $argTmp = $thumbW."|".$thumbH."|".$image;
  $arg = base64_encode($argTmp);
  $arg = urlencode($arg);

  $img_info = @getImageSize($image);
  if($img_info[2] == 6) {
    $chgURL = $image;
  }
  else {
    $chgURL = "http://localhost/imgView.php?image=$arg";
  }


  return $chgURL;
}


$img_url = http://localhost/images/test.jpg;
$img_url = waterMark($img_url);
echo $img_url;

?>

 

< ?

/******************************************************************************

Program: imgView.php
Description : 서비스 이미지에 워터마크 적용하기
Author      : Ryu Jee Hyoung
CreateDate  : 2007.01.18
CopyRight   : Copyright(c) 청명공자 All Right Reserved.
UpdateDate  :

Todo        :

******************************************************************************/
flush();

 

# 특정경로에 워터마크 처리할 이미지가 존재해야 함!!
# 워터마크 이미지는 필히 JPG 여야 합니다.

define("WATERMARK", "/home/httpd/html/images/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");

 

$photoTmp = base64_decode($image);
$photo = $photoTmp; # waterMarkImage($arg) 이런 형식(w,h가 지정이 안된 경우)으로 사용된 것
if (ereg("|",$photo)) {
  # waterMarkImage($arg,100,50) 이런 형식(w,h가 지정된 경우)으로 사용된 것
  list($w1,$h1,$photo) = explode("|", $photoTmp, 3);
  $thumb_w1 = $w1;
  $thumb_h1 = $h1;
}

$photo = urldecode($photo); # %C24%.... 이런식으로 encode되어 오는 파일오류를 방지


$img_info = getImageSize($photo);
$photo = ereg_replace("%","%25",$photo);
$image_url = urldecode($photo);
if (!@fopen($image_url, "r")) {
  $dst_img = imagecreatefromjpeg(WATERMARK);
  $res = imagejpeg($dst_img, "", 100);
  die();
}

$file = pathinfo($image_url);
if ($img_info[2] == "1") {
  $file["extension"] = "GIF";
}
else if ($img_info[2] == "2") {
  $file["extension"] = "JPG";
}
else if ($img_info[2] == "3") {
  $file["extension"] = "PNG";
}
switch(strtoupper($file["extension"])) {
  case "JPEG":
    $src_img = imagecreatefromjpeg($image_url);
    break;
  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;

 

# w, h값이 있으면 thumbnail로 보여주기
if(!is_numeric($w1) || empty($w1)) $w1 = $img_info[0];
if(!is_numeric($h1) || empty($h1)) $h1 = $img_info[1];
$dest_w = $w1;
$dest_h = $h1;
$dst_img = imagecreatetruecolor($dest_w, $dest_h);

 

# 배경이미지 흰색으로 채우는 부분 - 투명이미지가 이상하게 보이는 현상을 방지
$white = imagecolorallocate($dst_img,255,255,255);
imagefill($dst_img, 0, 0, $white);


imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $dest_w, $dest_h, $src_w, $src_h);

imagedestroy($src_img);

flush();

$watermark = imagecreatefromjpeg(WATERMARK);

$watermark_w = imagesx($watermark);
$watermark_h = imagesy($watermark);

$resize_w = $watermark_w;
$resize_h = $watermark_h;
if ($dest_w < 100) {
  $resize_w = $watermark_w / 2;
  $resize_h = $watermark_h / 2;
}
else if ($dest_h < 100) {
  $resize_w = $watermark_w / 2;
  $resize_h = $watermark_h / 2;
}
$overlay_img = imagecreatetruecolor($watermark_w, $watermark_h);
imagecopyresized($overlay_img, $watermark, 0, 0, 0, 0, $resize_w, $resize_h, $watermark_w, $watermark_h);
imagedestroy($watermark);

$white = imagecolorallocate($overlay_img, 0xFF, 0xFF, 0xFF);
$black = imagecolorallocate($overlay_img, 0x00, 0x00, 0x00);
imagecolortransparent($overlay_img, $black);

 

# 워터마크 중앙에 배치
$offsetX = ($dest_w - $watermark_w - 3) / 2;
$offsetY = ($dest_h - $watermark_h - 3) / 2;

# 워터마크 좌상단에 배치
# $offsetX = 3;
# $offsetY = 3;

# 워터마크 우상단에 배치
# $offsetX = $dest_w - $watermark_w - 3;
# $offsetY = $dest_h - $watermark_h - 3;

 

# w, h값이 있으면 thumbnail로 보여주되 워터마크 적용안함!!
if (empty($thumb_w1) && empty($thumb_h1)) imagecopymerge($dst_img,$overlay_img,$offsetX,$offsetY,0,0,$watermark_w,$watermark_h, 30);
imagedestroy($overlay_img);

$res = imagejpeg($dst_img, "", 100);
imagedestroy($dst_img);

flush();

exit;
?>

List of Articles
번호 제목 날짜 조회 수
300 지엠 웹에디터 v1.1 (저작권표시없음)| file 2017.03.06 17109
299 주차 , 요일, 해당주의 시작일, 해당주의 종료일 date() 2021.07.08 432
298 주간단위 시작일에서 종료일을 셀렉트박스로 만들기. file 2019.04.29 1331
297 주간날짜 뽑아오기 2014.02.27 26731
296 조건문의 함수실행 여부 if ( 0 && ... ) 2021.03.26 227
295 정규표현식 매치를 수행 (preg_match) 2016.12.23 20050
294 정규표현식 검사 도구 (ereg, eregi) 2016.12.23 18395
293 정규표현식 2021.03.26 311
292 정규식 2017.04.13 17599
291 정규 표현식 검색과 치환 (preg_replace) 2016.12.23 19012
290 정규 표현 / 전화번호 / 이메일 2019.01.16 1206
289 접속 IP 검사 2015.04.14 21871
288 전화번호에 하이픈(-) 넣기 2015.04.14 26622
287 잡다한 php 2017.03.06 18502
286 자바스크립트 이스케이프 문자열을 PHP로 디코딩 하기 2018.10.27 3259
285 자릿수만큼 앞에 0 붙이기 2017.03.07 19026
284 자동으로 다른 페이지로 넘어가는 함수 2019.01.08 1256
283 이스케이프 함수 (htmlentities) 2016.12.23 18375
» 이미지 워터마크 구현 2014.03.26 19667
281 이미지 사이즈 비율로 조정하기 2019.01.08 1629
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 17 Next
/ 17

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved