사이트 내 전체검색
PHP
PHP를 이용하여 워터마크를 만들어보자 - php 이미지 폰트 변경 php 워터마크 소스
로빈아빠
https://cmd.kr/php/561 URL이 복사되었습니다.

본문

여기서 소개하는 PHP워터마크 처리 소스는 이미지에 텍스트를 추가하는 방식으로 워터마크를 구현합니다. 이미지를 사용한 워터마크보다 수준이 떨어지는 기술이지만 텍스트를 추가하는 것만으로도 워터마크 기능을 충실히 할 수 있다고 저는 생각합니다.

먼저 PHP 소스를 보도록 하겠습니다.

  1. class Image {   
  2.   function impressWaterMark($type,$image,$string = null) {   
  3.     $type = strtolower($type);   
  4.     switch($type) {   
  5.       case 'gif':   
  6.         $output = imagecreatefromgif($image);   
  7.         $image_org = imagecreatefromgif($image);   
  8.       break;   
  9.       case 'jpg':   
  10.       case 'jpeg':   
  11.         $output = imagecreatefromjpeg($image);   
  12.         $image_org = imagecreatefromjpeg($image);   
  13.       break;   
  14.       case 'png':   
  15.         $output = imagecreatefrompng($image);   
  16.         $image_org = imagecreatefrompng($image);   
  17.       break;   
  18.     }   
  19.     if($string === null) {   
  20.       switch($type) {   
  21.         case 'gif':   
  22.           imagegif($output);   
  23.         break;   
  24.         case 'jpg':   
  25.         case 'jpeg':   
  26.           imagejpeg($output);   
  27.         break;   
  28.         case 'png':   
  29.           imagepng($output);   
  30.         break;   
  31.       }   
  32.       imagedestroy($output);   
  33.       imagedestroy($image_org);   
  34.       return;   
  35.     }   
  36.     $font_size = 15;   
  37.     $opacity = 30;   
  38.     // FONTS 상수는 사용자가 직접 정의해주세요.   
  39.     $font_path = FONTS.'watermark.ttf';   
  40.     $_font_size = imagettfbbox($font_size,0,$font_path,$string);   
  41.     $dx = abs($_font_size[2]-$_font_size[0]);   
  42.     $dy = abs($_font_size[5]-$_font_size[3]);   
  43.     $xpad = 20;   
  44.     $ypad = 20;   
  45.     $w = imagesx($output);   
  46.     $h = imagesy($output);   
  47.     $white = imagecolorallocate($output,255,255,255);   
  48.     $black = imagecolorallocate($output,0,0,0);   
  49.     imagettftext($output,$font_size,0,(int)($xpad/2)+1,$dy+(int)($ypad/2),$black,$font_path,$string);   
  50.     imagettftext($output,$font_size,0,(int)($xpad/2),$dy+(int)($ypad/2)-1,$white,$font_path,$string);   
  51.     imagecopymerge($output,$image_org,0,0,0,0,$w,$h,$opacity);   
  52.     switch($type) {   
  53.       case 'gif':   
  54.         imagegif($output);   
  55.       break;   
  56.       case 'jpg':   
  57.       case 'jpeg':   
  58.         imagejpeg($output);   
  59.       break;   
  60.       case 'png':   
  61.         imagepng($output);   
  62.       break;   
  63.     }   
  64.     imagedestroy($output);   
  65.     imagedestroy($image_org);   
  66.   }   
  67. }   
class Image {  function impressWaterMark($type,$image,$string = null) {    $type = strtolower($type);    switch($type) {      case 'gif':        $output = imagecreatefromgif($image);        $image_org = imagecreatefromgif($image);      break;      case 'jpg':      case 'jpeg':        $output = imagecreatefromjpeg($image);        $image_org = imagecreatefromjpeg($image);      break;      case 'png':        $output = imagecreatefrompng($image);        $image_org = imagecreatefrompng($image);      break;    }    if($string === null) {      switch($type) {        case 'gif':          imagegif($output);        break;        case 'jpg':        case 'jpeg':          imagejpeg($output);        break;        case 'png':          imagepng($output);        break;      }      imagedestroy($output);      imagedestroy($image_org);      return;    }    $font_size = 15;    $opacity = 30;    // FONTS 상수는 사용자가 직접 정의해주세요.    $font_path = FONTS.'watermark.ttf';    $_font_size = imagettfbbox($font_size,0,$font_path,$string);    $dx = abs($_font_size[2]-$_font_size[0]);    $dy = abs($_font_size[5]-$_font_size[3]);    $xpad = 20;    $ypad = 20;    $w = imagesx($output);    $h = imagesy($output);    $white = imagecolorallocate($output,255,255,255);    $black = imagecolorallocate($output,0,0,0);    imagettftext($output,$font_size,0,(int)($xpad/2)+1,$dy+(int)($ypad/2),$black,$font_path,$string);    imagettftext($output,$font_size,0,(int)($xpad/2),$dy+(int)($ypad/2)-1,$white,$font_path,$string);    imagecopymerge($output,$image_org,0,0,0,0,$w,$h,$opacity);    switch($type) {      case 'gif':        imagegif($output);      break;      case 'jpg':      case 'jpeg':        imagejpeg($output);      break;      case 'png':        imagepng($output);      break;    }    imagedestroy($output);    imagedestroy($image_org);  }}
위 워터마크 함수는 태터툴즈(텍스트큐브 이전 이름)를 참고하여 제작하였습니다.
위 함수는 Image 클래스에 포함되어 다른 함수와의 중복을 처음부터 방지하며 관리를 쉽도록 하였습니다.

사용법은 Image::impressWaterMark([이미지 타입],[파일 경로],[표시 문자열]);입니다.

위의 함수를 사용하면 따로 이미지를 출력할 필요가 없이 바로 워터마크 처리된 이미지를 출력해 줍니다.
그렇기 때문에 이미지 출력 페이지를 따로 만들거나 다운로드 페이지에서 이 함수를 호출하면 바로 이미지를 출력할 수 있습니다.

사용 예) Image::impressWaterMark('jpeg','/files/0/testImage','Beneglo');

※ 위 소스에서 FONTS는 폰트 파일 경로를 지정하는 상수입니다. FONTS는 제가 지정한 상수이므로 사용할때는 수정해주세요.

댓글목록

등록된 댓글이 없습니다.

PHP
871 (8/18P)

Search

Copyright © Cmd 명령어 3.19.234.150