search
HomeBackend DevelopmentPHP TutorialPHP给图片增多水印得类

PHP给图片增加水印得类

<?php/*+--------------------------------------| 生成加水印的图片类 (支持水印为图片或者文字)| ============================| by JackMing (感谢Dash和其他不知道姓名的朋友支持,本类在这些朋友作品的基础上创建)+--------------------------------------*/Class Gimage{var $src_image_name = ""; //输入图片的文件名(必须包含路径名)var $jpeg_quality = 90; //jpeg图片质量var $save_image_file = ''; //输出文件名var $wm_image_name = ""; //水印图片的文件名(必须包含路径名)var $wm_image_pos = 1; //水印图片放置的位置// 0 = middle// 1 = top left// 2 = top right// 3 = bottom right// 4 = bottom left// 5 = top middle// 6 = middle right// 7 = bottom middle// 8 = middle left//other = 3var $wm_image_transition = 20; //水印图片与原图片的融合度 (1=100)var $wm_text = ""; //水印文字(支持中英文以及带有\r\n的跨行文字)var $wm_text_size = 20; //水印文字大小var $wm_text_angle = 4; //水印文字角度,这个值尽量不要更改var $wm_text_pos = 3; //水印文字放置位置var $wm_text_font = ""; //水印文字的字体var $wm_text_color = "#cccccc"; //水印字体的颜色值function create($filename=""){if ($filename) $this->src_image_name = strtolower(trim($filename));$src_image_type = $this->get_type($this->src_image_name);$src_image = $this->createImage($src_image_type,$this->src_image_name);if (!$src_image) return;$src_image_w=ImageSX($src_image);$src_image_h=ImageSY($src_image);if ($this->wm_image_name){$this->wm_image_name = strtolower(trim($this->wm_image_name));$wm_image_type = $this->get_type($this->wm_image_name);$wm_image = $this->createImage($wm_image_type,$this->wm_image_name);$wm_image_w=ImageSX($wm_image);$wm_image_h=ImageSY($wm_image);$temp_wm_image = $this->getPos($src_image_w,$src_image_h,$this->wm_image_pos,$wm_image);$wm_image_x = $temp_wm_image["dest_x"];$wm_image_y = $temp_wm_image["dest_y"];imageCopyMerge($src_image, $wm_image,$wm_image_x,$wm_image_y,0,0,$wm_image_w,$wm_image_h,$this->wm_image_transition);}if ($this->wm_text){$this->wm_text = $this->gb2utf8($this->wm_text);$temp_wm_text = $this->getPos($src_image_w,$src_image_h,$this->wm_text_pos);$wm_text_x = $temp_wm_text["dest_x"];$wm_text_y = $temp_wm_text["dest_y"];if(preg_match("/([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])/i", $this->wm_text_color, $color)){$red = hexdec($color[1]);$green = hexdec($color[2]);$blue = hexdec($color[3]);$wm_text_color = imagecolorallocate($src_image, $red,$green,$blue);}else{$wm_text_color = imagecolorallocate($src_image, 255,255,255);}imagettftext($src_image, $this->wm_text_size, $this->wm_angle, $wm_text_x, $wm_text_y, $wm_text_color,$this->wm_text_font,$this->wm_text);}if ($this->save_file){switch ($this->output_type){case 'gif':$src_img=ImagePNG($src_image, $this->save_file); break;case 'jpeg':$src_img=ImageJPEG($src_image, $this->save_file, $this->jpeg_quality); break;case 'png':$src_img=ImagePNG($src_image, $this->save_file); break;default:$src_img=ImageJPEG($src_image, $this->save_file, $this->jpeg_quality); break;}}else{if ($src_image_type = "jpg") $src_image_type="jpeg";header("Content-type: image/{$src_image_type}");switch ($src_image_type){case 'gif':$src_img=ImagePNG($src_image); break;case 'jpg':$src_img=ImageJPEG($src_image, "", $this->jpeg_quality);break;case 'png':$src_img=ImagePNG($src_image);break;default:$src_img=ImageJPEG($src_image, "", $this->jpeg_quality);break;}}imagedestroy($src_image);}/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++/*createImage 根据文件名和类型创建图片内部函数$type: 图片的类型,包括gif,jpg,png$img_name:图片文件名,包括路径名,例如 " ./mouse.jpg"*/function createImage($type,$img_name){if (!$type){$type = $this->get_type($img_name);}switch ($type){case 'gif':if (function_exists('imagecreatefromgif'))[email&#160;protected]($img_name);break;case 'jpg':$tmp_img=ImageCreateFromJPEG($img_name);break;case 'png':$tmp_img=ImageCreateFromPNG($img_name);break;default:$tmp_img=ImageCreateFromString($img_name);break;}return $tmp_img;}/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++getPos 根据源图像的长、宽,位置代码,水印图片id来生成把水印放置到源图像中的位置内部函数$sourcefile_width: 源图像的宽$sourcefile_height: 原图像的高$pos: 位置代码// 0 = middle// 1 = top left// 2 = top right// 3 = bottom right// 4 = bottom left// 5 = top middle// 6 = middle right// 7 = bottom middle// 8 = middle left$wm_image: 水印图片ID*/function getPos($sourcefile_width,$sourcefile_height,$pos,$wm_image=""){if($wm_image){$insertfile_width = ImageSx($wm_image);$insertfile_height = ImageSy($wm_image);}else {$lineCount = explode("\r\n",$this->wm_text);$fontSize = imagettfbbox($this->wm_text_size,$this->wm_text_angle,$this->wm_text_font,$this->wm_text);$insertfile_width = $fontSize[2] - $fontSize[0];$insertfile_height = count($lineCount)*($fontSize[1] - $fontSize[3]);}switch ($pos){case 0:$dest_x = ( $sourcefile_width / 2 ) - ( $insertfile_width / 2 );$dest_y = ( $sourcefile_height / 2 ) - ( $insertfile_height / 2 );break;case 1:$dest_x = 0;if ($this->wm_text){$dest_y = $insertfile_height;}else{$dest_y = 0;}break;case 2:$dest_x = $sourcefile_width - $insertfile_width;if ($this->wm_text){$dest_y = $insertfile_height;}else{$dest_y = 0;}break;case 3:$dest_x = $sourcefile_width - $insertfile_width;$dest_y = $sourcefile_height - $insertfile_height;break;case 4:$dest_x = 0;$dest_y = $sourcefile_height - $insertfile_height;break;case 5:$dest_x = ( ( $sourcefile_width - $insertfile_width ) / 2 );if ($this->wm_text){$dest_y = $insertfile_height;}else{$dest_y = 0;}break;case 6:$dest_x = $sourcefile_width - $insertfile_width;$dest_y = ( $sourcefile_height / 2 ) - ( $insertfile_height / 2 );break;case 7:$dest_x = ( ( $sourcefile_width - $insertfile_width ) / 2 );$dest_y = $sourcefile_height - $insertfile_height;break;case 8:$dest_x = 0;$dest_y = ( $sourcefile_height / 2 ) - ( $insertfile_height / 2 );break;default:$dest_x = $sourcefile_width - $insertfile_width;$dest_y = $sourcefile_height - $insertfile_height;break;}return array("dest_x"=>$dest_x,"dest_y"=>$dest_y);}/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++gb2utf8 指定的文字转换为UTF-8格式,包括中英文混合内部函数*/function gb2utf8($gb){if(!trim($gb))return $gb;$filename="./gb2312.txt";$tmp=file($filename);$codetable=array();while(list($key,$value)=each($tmp))$codetable[hexdec(substr($value,0,6))]=substr($value,7,6);$utf8="";while($gb){if (ord(substr($gb,0,1))>127){$tthis=substr($gb,0,2);$gb=substr($gb,2,strlen($gb)-2);$utf8.=$this->u2utf8(hexdec($codetable[hexdec(bin2hex($tthis))-0x8080]));}else{$tthis=substr($gb,0,1);$gb=substr($gb,1,strlen($gb)-1);$utf8.=$this->u2utf8($tthis);}}return $utf8;}function u2utf8($c){$str="";if ($c < 0x80){$str.=$c;}else if ($c < 0x800){$str.=chr(0xC0 | $c>>6);$str.=chr(0x80 | $c & 0x3F);}else if ($c < 0x10000){$str.=chr(0xE0 | $c>>12);$str.=chr(0x80 | $c>>6 & 0x3F);$str.=chr(0x80 | $c & 0x3F);}else if ($c < 0x200000){$str.=chr(0xF0 | $c>>18);$str.=chr(0x80 | $c>>12 & 0x3F);$str.=chr(0x80 | $c>>6 & 0x3F);$str.=chr(0x80 | $c & 0x3F);}return $str;}/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++get_type 获得图片的格式,包括jpg,png,gif内部函数$img_name: 图片文件名,可以包括路径名*/function get_type($img_name)//获取图像文件类型{$name_array = explode(".",$img_name);if (preg_match("/\.(jpg|jpeg|gif|png)$/", $img_name, $matches)){$type = strtolower($matches[1]);}else{$type = "string";}return $type;}}?>


使用方法:
$img = new Gimage();
$img->wm_text = "www.discuz.com";
$img->wm_text_font = "./STXINWEI.TTF";
$img->create("./mouse.jpg");

就可以了,其中
mouse.jpg是你要在其上添加水印的图片名称,注意包含路径名
STXINWEI.TTF是字体文件的路径名+文件名
这就是一个简单的测试。如果要调整更复杂的显示效果,只要修改一下类中的属性就可以了,例如把字体放大就可以
$img->wm_text_size = 20;
增加水印图片就可以
$img->wm_image_name="文件名";

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
如何免费使用Bing Image Creator如何免费使用Bing Image CreatorFeb 27, 2024 am 11:04 AM

本文将介绍七种利用免费的BingImageCreator获得高质量输出的方法。BingImageCreator(现称为MicrosoftDesigner的ImageCreator)是一个出色的在线人工智能艺术生成器之一。它能根据用户的提示生成高度逼真的视觉效果。提示越具体、清晰和创意,生成的效果也会更出色。BingImageCreator在创建高质量图像方面取得了重大进展。它现在使用Dall-E3培训模式,显示出更高水平的细节和现实主义。然而,它能否始终如一地生成高清结果取决于几个因素,包括快速

小米手机image怎么删除小米手机image怎么删除Mar 02, 2024 pm 05:34 PM

小米手机image怎么删除?在小米手机中是可以删除image,但是多数的用户不知道image如何的删除,接下来就是小编为用户带来的小米手机image删除方法教程,感兴趣的用户快来一起看看吧!小米手机image怎么删除1、首先打开小米手机中的【相册】功能;2、然后勾选不需要的图片,点击右下角的【删除】按钮;3、之后点击最顶部的【相册】进入到专区,选择【回收站】;4、接着直接点击下图所示的【清空回收站】;5、最后直接点击【永久删除】即可完成。

Imagemagic安装Centos及Image安装教程Imagemagic安装Centos及Image安装教程Feb 12, 2024 pm 05:27 PM

LINUX是一种开源的操作系统,它的灵活性和可定制性使得它成为了许多开发者和系统管理员的首选,在LINUX系统中,图像处理是一个非常重要的任务,而Imagemagick和Image是两个非常流行的图像处理工具,本文将为您介绍如何在Centos系统中安装Imagemagick和Image,并提供详细的安装教程。Imagemagic安装Centos教程Imagemagick是一个功能强大的图像处理工具集,它可以在命令行下执行各种图像操作,以下是在Centos系统上安装Imagemagick的步骤:1

一篇搞懂this指向,赶超70%的前端人一篇搞懂this指向,赶超70%的前端人Sep 06, 2022 pm 05:03 PM

同事因为this指向的问题卡住的bug,vue2的this指向问题,使用了箭头函数,导致拿不到对应的props。当我给他介绍的时候他竟然不知道,随后也刻意的看了一下前端交流群,至今最起码还有70%以上的前端程序员搞不明白,今天给大家分享一下this指向,如果啥都没学会,请给我一个大嘴巴子。

Java中this方法怎么使用Java中this方法怎么使用Apr 18, 2023 pm 01:58 PM

一、this关键字1.this的类型:哪个对象调用就是哪个对象的引用类型二、用法总结1.this.data;//访问属性2.this.func();//访问方法3.this();//调用本类中其他构造方法三、解释用法1.this.data这种是在成员方法中使用让我们来看看不加this会出现什么样的状况classMyDate{publicintyear;publicintmonth;publicintday;publicvoidsetDate(intyear,intmonth,intday){ye

聊聊Vue2为什么能通过this访问各种选项中属性聊聊Vue2为什么能通过this访问各种选项中属性Dec 08, 2022 pm 08:22 PM

本篇文章带大家解读vue源码,来介绍一下Vue2中为什么可以使用 this 访问各种选项中的属性,希望对大家有所帮助!

什么是this?深入解析JavaScript中的this什么是this?深入解析JavaScript中的thisAug 04, 2022 pm 05:02 PM

什么是this?下面本篇文章给大家介绍一下JavaScript中的this,并聊聊this在函数不同调用方式下的区别,希望对大家有所帮助!

JavaScript箭头函数中的this详解JavaScript箭头函数中的this详解Jan 25, 2024 pm 01:41 PM

JavaScript中箭头函数是一种比较新的语法,没有自己的this关键字,相反箭头函数的this指向包含它的作用域对象,影响方面有:1、箭头函数中的this是静态的;2、箭头函数不能作为构造函数使用;3、箭头函数不能用作方法。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function