찾다
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으로 문의하세요.
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

See all articles

핫 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를 무료로 생성하십시오.

뜨거운 도구

맨티스BT

맨티스BT

Mantis는 제품 결함 추적을 돕기 위해 설계된 배포하기 쉬운 웹 기반 결함 추적 도구입니다. PHP, MySQL 및 웹 서버가 필요합니다. 데모 및 호스팅 서비스를 확인해 보세요.

DVWA

DVWA

DVWA(Damn Vulnerable Web App)는 매우 취약한 PHP/MySQL 웹 애플리케이션입니다. 주요 목표는 보안 전문가가 법적 환경에서 자신의 기술과 도구를 테스트하고, 웹 개발자가 웹 응용 프로그램 보안 프로세스를 더 잘 이해할 수 있도록 돕고, 교사/학생이 교실 환경 웹 응용 프로그램에서 가르치고 배울 수 있도록 돕는 것입니다. 보안. DVWA의 목표는 다양한 난이도의 간단하고 간단한 인터페이스를 통해 가장 일반적인 웹 취약점 중 일부를 연습하는 것입니다. 이 소프트웨어는

에디트플러스 중국어 크랙 버전

에디트플러스 중국어 크랙 버전

작은 크기, 구문 강조, 코드 프롬프트 기능을 지원하지 않음

안전한 시험 브라우저

안전한 시험 브라우저

안전한 시험 브라우저는 온라인 시험을 안전하게 치르기 위한 보안 브라우저 환경입니다. 이 소프트웨어는 모든 컴퓨터를 안전한 워크스테이션으로 바꿔줍니다. 이는 모든 유틸리티에 대한 액세스를 제어하고 학생들이 승인되지 않은 리소스를 사용하는 것을 방지합니다.

Atom Editor Mac 버전 다운로드

Atom Editor Mac 버전 다운로드

가장 인기 있는 오픈 소스 편집기