php教程 php手册 PHP加水印代码 支持文字和图片水印

PHP加水印代码 支持文字和图片水印

Jun 13, 2016 am 09:37 AM
php 암호 추가하다 그리고 그림 지원하다 문자 양수표 ~의 친절한 배경 설정 투명도

   PHP加图片水印、文字水印类代码,PHP加水印类,支持文字图片水印的透明度设置、水印图片背景透明。自己写的一个类,因为自己开发的一套CMS中要用到,网上的总感觉用着不顺手,希望大家也喜欢这个类,后附有类使用方法。

  001

  002class WaterMask{

  003 public $waterType = 1; //水印类型:0为文字水印、1为图片水印

  004 public $pos = 0; //水印位置

  005 public $transparent = 45; //水印透明度

  006 public $waterStr = 'www.codefans.net'; //水印文字

  007 public $fontSize = 16; //文字字体大小

  008 public $fontColor = array(255,0,255); //水印文字颜色(RGB)

  009 public $fontFile = 'AHGBold.ttf';//字体文件

  010 public $waterImg = 'logo.png';//水印图片

  011 private $srcImg = '';//需要添加水印的图片

  012 private $im = '';//图片句柄

  013 private $water_im = '';//水印图片句柄

  014 private $srcImg_info = '';//图片信息

  015 private $waterImg_info = '';//水印图片信息

  016 private $str_w = '';//水印文字宽度

  017 private $str_h = '';//水印文字高度

  018 private $x = '';//水印X坐标

  019 private $y = '';//水印y坐标

  020 function __construct($img) {

  021 $this->srcImg = file_exists($img) $img : die('"'.$img.'" 对不起,水印文件不存在!');

  022 }

  023 private function imginfo() {//获取水印图片信息,并加载。

  024 $this->srcImg_info = getimagesize($this->srcImg);

  025 switch ($this->srcImg_info[2]) {

  026 case 3:

  027 $this->im = imagecreatefrompng($this->srcImg);

  028 break 1;

  029 case 2:

  030 $this->im = imagecreatefromjpeg($this->srcImg);

  031 break 1;

  032 case 1:

  033 $this->im = imagecreatefromgif($this->srcImg);

  034 break 1;

  035 default:

  036 die('水印图片('.$this->srcImg.')水印图片格式不对,请选择PNG、JPEG、GIF格式的图片。');

  037 }

  038 }

  039 private function waterimginfo() {//获取水印图片并载入。

  040 $this->waterImg_info = getimagesize($this->waterImg);

  041 switch ($this->waterImg_info[2]) {

  042 case 3:

  043 $this->water_im = imagecreatefrompng($this->waterImg);

  044 break 1;

  045 case 2:

  046 $this->water_im = imagecreatefromjpeg($this->waterImg);

  047 break 1;

  048 case 1:

  049 $this->water_im = imagecreatefromgif($this->waterImg);

  050 break 1;

  051 default:

  052 die('水印图片('.$this->srcImg.')格式不对,只支持PNG、JPEG、GIF。');

  053 }

  054 }

  055 private function waterpos() {//水印位置算法

  056 switch ($this->pos) {

  057 case 0: //随机位置

  058 $this->x = rand(0,$this->srcImg_info[0]-$this->waterImg_info[0]);

  059 $this->y = rand(0,$this->srcImg_info[1]-$this->waterImg_info[1]);

  060 break 1;

  061 case 1: //上左

  062 $this->x = 0;

  063 $this->y = 0;

  064 break 1;

  065 case 2: //上中

  066 $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;

  067 $this->y = 0;

  068 break 1;

  069 case 3: //上右

  070 $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];

  071 $this->y = 0;

  072 break 1;

  073 case 4: //中左

  074 $this->x = 0;

  075 $this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2;

  076 break 1;

  077 case 5: //中中

  078 $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;

  079 $this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2;

  080 break 1;

  081 case 6: //中右

  082 $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];

  083 $this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2;

  084 break 1;

  085 case 7: //下左

  086 $this->x = 0;

  087 $this->y = $this->srcImg_info[1]-$this->waterImg_info[1];

  088 break 1;

  089 case 8: //下中

  090 $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;

  091 $this->y = $this->srcImg_info[1]-$this->waterImg_info[1];

  092 break 1;

  093 default: //下右

  094 $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];

  095 $this->y = $this->srcImg_info[1]-$this->waterImg_info[1];

  096 break 1;

  097 }

  098 }

  099 private function waterimg() {

  100 if ($this->srcImg_info[0] waterImg_info[0] || $this->srcImg_info[1] waterImg_info[1]){

  101 die('水印比原图大!');

  102 }

  103 $this->waterpos();

  104 $cut = imagecreatetruecolor($this->waterImg_info[0],$this->waterImg_info[1]);

  105 imagecopy($cut,$this->im,0,0,$this->x,$this->y,$this->waterImg_info[0],$this->waterImg_info[1]);

  106 $pct = $this->transparent;

  107 imagecopy($cut,$this->water_im,0,0,0,0,$this->waterImg_info[0],$this->waterImg_info[1]);

  108 imagecopymerge($this->im,$cut,$this->x,$this->y,0,0,$this->waterImg_info[0],$this->waterImg_info[1],$pct);

  109 }

  110 private function waterstr() {

  111 $rect = imagettfbbox($this->fontSize,0,$this->fontFile,$this->waterStr);

  112 $w = abs($rect[2]-$rect[6]);

  113 $h = abs($rect[3]-$rect[7]);

  114 $fontHeight = $this->fontSize;

  115 $this->water_im = imagecreatetruecolor($w, $h);

  116 imagealphablending($this->water_im,false);

  117 imagesavealpha($this->water_im,true);

  118 $white_alpha = imagecolorallocatealpha($this->water_im,255,255,255,127);

  119 imagefill($this->water_im,0,0,$white_alpha);

  120 $color = imagecolorallocate($this->water_im,$this->fontColor[0],$this->fontColor[1],$this->fontColor[2]);

  121 imagettftext($this->water_im,$this->fontSize,0,0,$this->fontSize,$color,$this->fontFile,$this->waterStr);

  122 $this->waterImg_info = array(0=>$w,1=>$h);

  123 $this->waterimg();

  124 }

  125 function output() {

  126 $this->imginfo();

  127 if ($this->waterType == 0) {

  128 $this->waterstr();

  129 }else {

  130 $this->waterimginfo();

  131 $this->waterimg();

  132 }

  133 switch ($this->srcImg_info[2]) {

  134 case 3:

  135 imagepng($this->im,$this->srcImg);

  136 break 1;

  137 case 2:

  138 imagejpeg($this->im,$this->srcImg);

  139 break 1;

  140 case 1:

  141 imagegif($this->im,$this->srcImg);

  142 break 1;

  143 default:

  144 die('原因未知:水印添加失败!');

  145 break;

  146 }

  147 imagedestroy($this->im);

  148 imagedestroy($this->water_im);

  149 }

  150}

  151?>

  PHP生成水印类用法:

  view sourceprint?01

  02$obj = new WaterMask($imgFileName);

  03$obj->$waterType = 1;//水印类型:0为文字水印、1为图片水印

  04$obj->$transparent = 45;//水印透明度

  05$obj->$waterStr = 'www.codefans.net';//水印文字

  06$obj->$fontSize = 16;//字体大小

  07$obj->$fontColor = array(255,0,255);//水印文字颜色(RGB值)

  08$obj->$fontFile = = 'AHGBold.ttf'; //字体名称

  09$obj->output(); //输出水印图片

  10?>

본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

인기 기사

R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
2 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
2 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
3 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드 Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드 Dec 24, 2024 pm 04:42 PM

PHP 8.4는 상당한 양의 기능 중단 및 제거를 통해 몇 가지 새로운 기능, 보안 개선 및 성능 개선을 제공합니다. 이 가이드에서는 Ubuntu, Debian 또는 해당 파생 제품에서 PHP 8.4를 설치하거나 PHP 8.4로 업그레이드하는 방법을 설명합니다.

CakePHP 날짜 및 시간 CakePHP 날짜 및 시간 Sep 10, 2024 pm 05:27 PM

cakephp4에서 날짜와 시간을 다루기 위해 사용 가능한 FrozenTime 클래스를 활용하겠습니다.

CakePHP 파일 업로드 CakePHP 파일 업로드 Sep 10, 2024 pm 05:27 PM

파일 업로드 작업을 위해 양식 도우미를 사용할 것입니다. 다음은 파일 업로드의 예입니다.

CakePHP 토론 CakePHP 토론 Sep 10, 2024 pm 05:28 PM

CakePHP는 PHP용 오픈 소스 프레임워크입니다. 이는 애플리케이션을 훨씬 쉽게 개발, 배포 및 유지 관리할 수 있도록 하기 위한 것입니다. CakePHP는 강력하고 이해하기 쉬운 MVC와 유사한 아키텍처를 기반으로 합니다. 모델, 뷰 및 컨트롤러 gu

CakePHP 유효성 검사기 만들기 CakePHP 유효성 검사기 만들기 Sep 10, 2024 pm 05:26 PM

컨트롤러에 다음 두 줄을 추가하면 유효성 검사기를 만들 수 있습니다.

CakePHP 로깅 CakePHP 로깅 Sep 10, 2024 pm 05:26 PM

CakePHP에 로그인하는 것은 매우 쉬운 작업입니다. 한 가지 기능만 사용하면 됩니다. cronjob과 같은 백그라운드 프로세스에 대해 오류, 예외, 사용자 활동, 사용자가 취한 조치를 기록할 수 있습니다. CakePHP에 데이터를 기록하는 것은 쉽습니다. log() 함수는 다음과 같습니다.

PHP 개발을 위해 Visual Studio Code(VS Code)를 설정하는 방법 PHP 개발을 위해 Visual Studio Code(VS Code)를 설정하는 방법 Dec 20, 2024 am 11:31 AM

VS Code라고도 알려진 Visual Studio Code는 모든 주요 운영 체제에서 사용할 수 있는 무료 소스 코드 편집기 또는 통합 개발 환경(IDE)입니다. 다양한 프로그래밍 언어에 대한 대규모 확장 모음을 통해 VS Code는

CakePHP 빠른 가이드 CakePHP 빠른 가이드 Sep 10, 2024 pm 05:27 PM

CakePHP는 오픈 소스 MVC 프레임워크입니다. 이를 통해 애플리케이션 개발, 배포 및 유지 관리가 훨씬 쉬워집니다. CakePHP에는 가장 일반적인 작업의 과부하를 줄이기 위한 여러 라이브러리가 있습니다.

See all articles