温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

PHP如何实现文字水印,缩略图,图片水印功能

发布时间:2021-07-08 09:54:29 来源:亿速云 阅读:156 作者:小新 栏目:开发技术

这篇文章主要介绍PHP如何实现文字水印,缩略图,图片水印功能,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

具体如下:

1.实现类ImageToTest.class.php参考代码

class ImageToTest {   /**    * 图片的基本信息    */   private $info;   private $image;   public function __construct($src){     $info = getimagesize($src);     $this->info = array(       'width'=> $info[0],       'height'=> $info[1],       'type'=> image_type_to_extension($info[2],false),       'mime'=>$info['mime']     );     $fun = "imagecreatefrom{$this->info['type']}";     $this->image = $fun($src);   }   /**    * 操作图片 (压缩)    */   public function thumb($width,$height){     $image_thumb = imagecreatetruecolor($width,$height);     imagecopyresampled($image_thumb,$this->image,0,0,0,0,$width,$height,       $this->info['width'],$this->info['height']);     imagedestroy($this->image);     $this->image = $image_thumb;   }   /**    * 操作图片(添加文字水印)    */   public function fontMark($content,$font_url,$size,$color,$local,$angle){     $col = imagecolorallocatealpha($this->image,$color[0],$color[1],$color[2],$color[3]);     imagettftext($this->image,$size,$angle,$local['x'],$local['y'],$col,$font_url,$content);   }   /**    * 操作图片(添加水印图片)    */   public function imageMark($source,$local,$alpha){     //1.获取水印图片的基本信息     $info2 = getimagesize($source);     //2.通过水印的图片编号来获取水印的图片类型     $type2 = image_type_to_extension($info2[2],false);     //3.在内存中创建一个和我们的水印图像一致的图像类型     $func2 = "imagecreatefrom{$type2}";     //4.把水印图片复制到内存中     $water = $func2($source);     //5.合并图片     imagecopymerge($this->image,$water,$local['x'],$local['y'],0,0,$info2[0],$info2[1],$alpha);     //6.销毁水印图片     imagedestroy($water);   }   /**    * 在浏览器中输出图片    */   public function show(){     header("Content-type:".$this->info['mime']);     $funs = "image{$this->info['type']}";     $funs($this->image);   }   /**    * 把图片保存到硬盘里    */   public function save($newName){     $funs = "image{$this->info['type']}";     $funs($this->image,'./outPut/'.$newName.'.'.$this->info['type']);   }   /**    * 销毁图片 使用析构函数    */   public function __destruct()   {     imagedestroy($this->image);   } }

2.测试参考代码

require_once('ImageToTest.class.php'); /*$src = './image/wbg.jpg'; $image = new ImageToTest($src); $image->thumb(700,550); $image->show();*/ /*$src2 = './image/wbg.jpg'; $content = 'SGC'; $font_url = './image/YGYcuhei.ttf'; $size = 33; $color = array(   0=>2,   1=>222,   2=>222,   3=>60 ); $local = array(   'x'=>20,   'y'=>100 ); $angle = 10; $image2 = new ImageToTest($src2); $image2->fontMark($content,$font_url,$size,$color,$local,$angle); $image2->show(); $image2->save('hahahah');*/ $src3 = './image/wbg.jpg'; $source = './image/water.jpg'; $local = array(   'x'=>20,   'y'=>100 ); $font_url = './image/YGYcuhei.ttf'; $size = 38; $color = array(   0=>2,   1=>222,   2=>222,   3=>60 ); $alpha = 60; $angle = 50; $image3 = new ImageToTest($src3); $image3->imageMark($source,$local,$alpha); $image3->thumb(700,550); $image3->fontMark('Hello',$font_url,$size,$color,$local,$angle); $image3->show(); $image3->save('WAWAWAWAWA');

以上是“PHP如何实现文字水印,缩略图,图片水印功能”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php
AI