php gd库水印类7年后重构了 支持php7
<?php /** * 缩略图水印生成类 文字水印 字符编码为 utf-8 * 中文需要处理还字体的问题 * @name MakeMiniature * @see * @version 2.1.0 (2016-1-22) * @author sanshi0815 */ class MakeMiniature { //字体带目录 private $font; //水印内容 可以是 文字,可以是图片路径,也可以为为空 private $watermark; //源文件 private $srcFile; //目标文件 private $dstFile; //水印类别 0 无水印,1图片水印,2文字水印 private $watetType; //支持的图片类别当前支持2种 private $imgType=array("jpg","jpeg","png"); //图片打开资源句柄 private $im; //原始图片类型 private $fileType; private $errorMsg=""; public function __construct() { } /** * 获取失败信息 * @author sanshi0815 * @return string 失败信息 */ public function getErrorMsg() { return $this->errorMsg; } /** * 参数设置 * @author sanshi0815 * @param string $font 字体文件带目录文字水印使用 * @param string $watermark 水印文字或者是图片地址 * @param string $srcFile 原始图片地址 * @param string $dstFile 生成新图片地址 * @return null 无返回值 */ public function set($font,$watermark,$srcFile,$dstFile) { $this->font = $font; $this->watermark = $watermark; $this->srcFile = $srcFile; $this->dstFile = $dstFile; if(empty($this->watermark )) { //无水印 $this->watetType = 0; }elseif(is_file($this->watermark)){ //图片水印 $this->watetType = 1; }else{ //文字水印 $this->watetType = 2; } } /** * 图片资源获取 成功返回数组,失败返回false * @author sanshi0815 * @param string $fileName 图片地址 * @return array r{句柄},t{后缀},w{宽度},h{高度} */ private function getResource($fileName) { if(!is_file($fileName)) { $this->errorMsg = "{$fileName} 不存在 line:".__LINE__; } $temp = explode('.',$fileName); $fileType = strtolower(end($temp)); //判断后缀是否是否符合要求 if(!in_array($fileType,$this->imgType)) { //文件类型不支持 $this->errorMsg = "{$fileName}图片后缀类型不支持 line:".__LINE__; return false; } if($fileType=="jpg" || $fileType=="jpeg") { $im=imageCreateFromjpeg($fileName); }else{ $im=imagecreatefrompng($fileName); } if(!$im) { //图片初始化失败 $this->errorMsg = "{$fileName}图片初始化资源失败 line:".__LINE__; return false; } //源图片宽 $width=imagesx($im); //源图片高 $height=imagesy($im); if(empty($width) || empty($height)) { //图片高度宽度获取失败 $this->errorMsg = "{$fileName}图片高度或者宽度获取失败 line:".__LINE__; return false; } return array("r"=>$im,"t"=>$fileType,"w"=>$width,"h"=>$height); } /** * 原始图片全局变量设置 成功返回数组,失败返回false * @author sanshi0815 * @return array w{宽度},h{高度} */ private function initSrcImgWH() { $temp = $this->getResource($this->srcFile); if(empty($temp)) { $this->errorMsg = "图像资源不存在 line:".__LINE__; return false; } $this->fileType = $temp['t']; $this->im=$temp['r']; return array("w"=>$temp['w'],"h"=>$temp['h']); } /** * 固定宽度,高度 进行图片缩放 * @author sanshi0815 * @param int $width 生成图片宽度 * @param int $height 生成图片高度 * @return bool 成功为 true,失败为false */ public function resetImgWH($width,$height) { $temp = $this->initSrcImgWH(); if(empty($temp)) { $this->errorMsg = "图像资源不存在 line:".__LINE__; return false; } $srcW = $temp['w']; $srcH = $temp['h']; $detW = intval($width); $detH = intval($height); //生成新的图像资源 $om = $this->getNewImg($srcW,$srcH,$detW,$detH); $temp = empty($om) ? false : $this->createImg($om,$detW,$detH); return $temp; } /** * 根据最大高度 进行图片等比缩放 * @author sanshi0815 * @param int $maxHeight 生成图片高度 * @return bool 成功为 true,失败为false */ public function resetImgMaxH($maxHeight) { $maxHeight = intval($maxHeight); $temp = $this->initSrcImgWH(); if(empty($temp)) { $this->errorMsg = "图像资源不存在 line:".__LINE__; return false; } $srcW = $temp['w']; $srcH = $temp['h']; //计算缩放比 $scale = round($maxHeight/$srcH,4); $detW = round($srcW*$scale); $detH = round($srcH*$scale); //生成新的图像资源 $om = $this->getNewImg($srcW,$srcH,$detW,$detH); $temp = empty($om) ? false : $this->createImg($om,$detW,$detH); return $temp; } /** * 根据最大宽度 进行图片等比缩放 * @author sanshi0815 * @param int $maxWidth 生成图片宽度 * @return bool 成功为 true,失败为false */ public function resetImgMaxW($maxWidth) { $temp = $this->initSrcImgWH(); if(empty($temp)) { $this->errorMsg = "图像资源不存在 line:".__LINE__; return false; } $srcW = $temp['w']; $srcH = $temp['h']; //计算缩放比 $scale = round($maxWidth/$srcW,4); $detW = round($srcW*$scale); $detH = round($srcH*$scale); //生成新的图像资源 $om = $this->getNewImg($srcW,$srcH,$detW,$detH); //$om = $this->im; $temp = empty($om) ? false : $this->createImg($om,$detW,$detH); return $temp; } /** * 获得缩放后的图片资源句柄 * @author sanshi0815 * @param int $srcW 原始图片宽度 * @param int $srcH 原始图片高度 * @param int $detW 原始图片宽度 * @param int $detH 原始图片高度 * @return bool 成功为 true,失败为false */ private function getNewImg($srcW,$srcH,$detW,$detH) { $om=imagecreatetruecolor($detW,$detH);//真色彩对gb库有要求 if(empty($om)) { $this->errorMsg = "imagecreatetruecolor 函数失败 line:".__LINE__; return false; } //ImageCopyResized($om,$im,0,0,0,0,$detW,$detH,$srcW,$srcH); $temp = imagecopyresampled($om,$this->im,0,0,0,0,$detW,$detH,$srcW,$srcH); if(empty($temp)) { $this->errorMsg = "imagecopyresampled 函数失败 line:".__LINE__; return false; } return $om; } /** * 获得图片加文字水印后资源 * @author sanshi0815 * @param resource $im 原始资源 * @param int $detW 原始图片宽度 * @param int $detH 原始图片高度 * @return resource 成功为 水印后的图片资源,失败为false */ private function getWatermarkText($im,$detW,$detH) { if(!$is_file($this->font)) { $this->errorMsg = "{$this->font} 字体不存在 line:".__LINE__; return false; } //旋转角度 $angle = 20; $width = $detW/10; $size = $detW/8; $height = $detH; //echo $height; $black = imagecolorallocate($im, 0, 0, 0); $grey = imagecolorallocate($im, 180, 180, 180); //生成水印次数 $formax = 3; for($i=$formax;$i>=1;$i--) { $height =$height-$detH/($formax+2); //echo $height."<br>"; $temp = imagettftext($im, $size, $angle,$width,$height, $grey, $this->font,$this->watermark); if(empty($temp)) { $this->errorMsg = "imagettftext 函数失败 line:".__LINE__; return false; } } return $im; } /** * 获得图片加图片水印后资源 * @author sanshi0815 * @param resource $im 原始资源 * @param int $detW 原始图片宽度 * @param int $detH 原始图片高度 * @return resource 成功为 水印后的图片资源,失败为false */ private function getWatermarkPic($im,$detW,$detH) { $temp = $this->getResource($this->watermark); if(empty($temp)) return false; $wm = $temp['r']; $src_x = 0; $src_y = 0; $src_w = $temp['w']; $src_h = $temp['h']; $dst_x = $detW; $dst_y = $detH; $height = $dst_y > $src_h ? ($dst_y - $src_h)/2 :0; $width = $dst_x > $src_w ? ($dst_x - $src_w)/2 : 0; $temp = imagealphablending($im,true); if(empty($temp)) { $this->errorMsg = "imagealphablending 函数失败 line:".__LINE__; return false; } $temp = imagecopymerge($im,$wm,$width,$height,0,0,$src_w,$src_h,70); if(empty($temp)) { $this->errorMsg = "imagecopymerge 函数失败 line:".__LINE__; return false; } return $im; } /** * 新图片生成 * @author sanshi0815 * @param resource $im 原始资源 * @param int $detW 原始图片宽度 * @param int $detH 原始图片高度 * @return bool 成功为true,失败为false */ private function createImg($im,$detW,$detH) { //处理水印 if($this->watetType==2) { $om = $this->getWatermarkText($im,$detW,$detH); }elseif ($this->watetType==1) { $om = $this->getWatermarkPic($im,$detW,$detH); }else{ $om = $im; } if(empty($om)) { $this->errorMsg = "图片资源不存在 line:".__LINE__; return false; } $fileType = $this->fileType; if($fileType=="jpg" || $fileType=="jpeg") { $temp=imagejpeg($om,$this->dstFile); }else{ $temp=imagepng($om,$this->dstFile); } return $temp; } } $file=new MakeMiniature(); $file->set("./simhei.ttf","张磊专用","1_1453362028.png","s1_1453362028.png"); $file->resetImgMaxW(800); ?>
很早以前的水印类 写个东西,看到了曾经写水印类,时间太久了,使用起来有些东西不太顺手了,而且场景发生了变化,比如缩略图,更多的应该是根据最大宽度或者高度去生成而不是固定比例,因为这样出来的图片才是最佳的观赏效果,而且不会变形。所以重新改进了这个类,对于php7的支持,实际上老的类也是支持的。
以前的类 http://blog.csdn.net/sanshi0815/article/details/1604905
以上就介绍了php gd库水印类7年后重构了 支持php7,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

许多用户在选择智能手表的时候都会选择的华为的品牌,其中华为GT3pro和GT4都是非常热门的选择,不少用户都很好奇华为GT3pro和GT4有什么区别,下面就就给大家介绍一下二者。华为GT3pro和GT4有什么区别一、外观GT4:46mm和41mm,材质是玻璃表镜+不锈钢机身+高分纤维后壳。GT3pro:46.6mm和42.9mm,材质是蓝宝石玻璃表镜+钛金属机身/陶瓷机身+陶瓷后壳二、健康GT4:采用最新的华为Truseen5.5+算法,结果会更加的精准。GT3pro:多了ECG心电图和血管及安

C语言return的用法有:1、对于返回值类型为void的函数,可以使用return语句来提前结束函数的执行;2、对于返回值类型不为void的函数,return语句的作用是将函数的执行结果返回给调用者;3、提前结束函数的执行,在函数内部,我们可以使用return语句来提前结束函数的执行,即使函数并没有返回值。

temp文件夹是我们的临时文件存储位置,系统会将临时文件保存到这个文件夹中,如果临时文件过多,尤其是当temp文件夹在系统盘时,很有可能会影响系统运行速度。我们可以通过更改temp位置的方式来解决问题,下面一起来看一下吧。win7temp位置更改教程1、首先右键选择“计算机”,打开“属性”2、点击左边的“高级系统设置”3、点击下方的“环境变量”4、选中“temp”点击“编辑”5、然后将“变量值”改为需要更改的路径即可。

源码:publicclassReturnFinallyDemo{publicstaticvoidmain(String[]args){System.out.println(case1());}publicstaticintcase1(){intx;try{x=1;returnx;}finally{x=3;}}}#输出上述代码的输出可以简单地得出结论:return在finally之前执行,我们来看下字节码层面上发生了什么事情。下面截取case1方法的部分字节码,并且对照源码,将每个指令的含义注释在

为什么截图工具在Windows11上不起作用了解问题的根本原因有助于找到正确的解决方案。以下是截图工具可能无法正常工作的主要原因:对焦助手已打开:这可以防止截图工具打开。应用程序损坏:如果截图工具在启动时崩溃,则可能已损坏。过时的图形驱动程序:不兼容的驱动程序可能会干扰截图工具。来自其他应用程序的干扰:其他正在运行的应用程序可能与截图工具冲突。证书已过期:升级过程中的错误可能会导致此issu简单的解决方案这些适合大多数用户,不需要任何特殊的技术知识。1.更新窗口和Microsoft应用商店应用程
![如何修复 Windows 11 / 10 中的处理器热跳闸错误 [修复]](https://img.php.cn/upload/article/000/000/164/168169038621890.png?x-oss-process=image/resize,m_fill,h_207,w_330)
大多数设备(例如笔记本电脑和台式机)长期被年轻游戏玩家和编码人员频繁使用。由于应用程序过载,系统有时会挂起。这使用户被迫关闭他们的系统。这主要发生在安装和玩重度游戏的玩家身上。当系统在强制关闭后尝试启动时,它会在黑屏上抛出一个错误,如下所示:以下是在此引导期间检测到的警告。这些可以在事件日志页面的设置中查看。警告:处理器热跳闸。按任意键继续。..当台式机或笔记本电脑的处理器温度超过其阈值温度时,总是会抛出这些类型的警告消息。下面列出了在Windows系统上发生这种情况的原因。许多繁重的应用程序在
![内部错误:无法创建临时目录 [已解决]](https://img.php.cn/upload/article/000/000/164/168171504798267.png?x-oss-process=image/resize,m_fill,h_207,w_330)
Windows系统允许用户使用可执行/设置文件在您的系统上安装各种类型的应用程序。最近,许多Windows用户开始抱怨他们收到一个名为INTERNALERROR:cannotcreatetemporarydirectory在他们的系统上尝试使用可执行文件安装任何应用程序的错误。问题不仅限于此,而且还阻止用户启动任何现有的应用程序,这些应用程序也安装在Windows系统上。下面列出了一些可能的原因。运行可执行文件进行安装时不授予管理员权限。为TMP变量提供了无效或不同的路径。损坏的系

temp是临时文件夹,在路径“C:\Documents and Settings\Administrator\Local Settings\”内,很多临时文件放在这里,收藏夹,浏览网页的临时文件,编辑文件等。
