php封装成类(GD库实现文字、图片水印、压缩图)
来源:互联网 发布:php下载zip文件损坏 编辑:程序博客网 时间:2024/05/31 19:34
<?php require('image_class.php');$src = 'tmp.jpg';$font_url = 'msyh.ttc';$logo_url = 'logo.png';$content = 'Hello world';$local = array( 'x'=> 20, 'y'=> 30 );$color = array( 0 => 255, 1 => 255, 2 => 255, 3 => 20 );$fontsize = 20;$angle = -30;$alpha = 30;$image = new Image($src);$image->fontmark($font_url,$content,$local,$color,$angle,$fontsize);$image->imagemark($logo_url,$local,$alpha);$image->thumb(150,100);$image->show();$image->move('imagemark');
?>
image_class.php<?php class Image{ //图片属性 private $image; private $info; //构造图片方法 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']}"; //imagecreatefromjpeg //把图片复制到我们的内存 $this->image = $fun($src); //imagecreatefromjpeg($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; //销毁图片 // imagedestroy($image_thumb); //地址一致,不能销毁 } //文字水印方法 public function fontmark($font_url,$content,$local,$color,$angle,$fontsize){ //设置字体颜色和透明度 参数1 内存中的图片 RGB参数 $col = imagecolorallocatealpha($this->image, $color[0], $color[1], $color[2], $color[3]); //写入文字 imagettftext($this->image, $fontsize, $angle, $local['x'], $local['y'], $col, $font_url, $content); } //图片水印方法 public function imagemark($logo_url,$local,$alpha){ //获取图片信息 $info2 = getimagesize($logo_url); //通过图片的编号取得图片的类型 $type_logo = image_type_to_extension($info2[2], false); //在内存中创建一个同类型的图像 $funs = "imagecreatefrom{$type_logo}"; //imagecreatefromjpeg //把水印复制到我们的内存 $water = $funs($logo_url); //imagecreatefromjpeg($src); //合并图像 //imagecopymerge($image, $water, 20, 30, 0, 0, $info2[0], $info2[1], 25); $this-> imagecopymerge_alpha($this->image, $water, $local['x'], $local['y'], 0, 0, $info2[0], $info2[1],$alpha); //销毁水印 imagedestroy($water); } //解决imagecopy、imagecopymerge的合并方案(兼容方法) public function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){ $cut = imagecreatetruecolor($src_w, $src_h); imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h); imagecopy($cut, $src_im,0, 0, $src_x, $src_y, $src_w, $src_h); imagecopymerge($dst_im, $cut, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct); } /*输出图片到浏览器*/ public function show(){ header("content-type:".$this->info['mime']); $func = "image{$this->info['type']}"; $func($this->image); } /*输出图片到磁盘*/ public function move($newname){ header("content-type:".$this->info['mime']); $func = "image{$this->info['type']}"; $func($this->image,$newname.'.'.$this->info['type']); } /*销毁图片*/ public function __destruct(){ imagedestroy($this->image); } }?>
阅读全文
1 0
- php封装成类(GD库实现文字、图片水印、压缩图)
- php GD库的简单封装,图片压缩、文字水印、图片水印等方法
- 将文字水印、图片水印、压缩图片封装成类。
- php gd库 文字与图片组合(类似水印)
- PHP图片水印类(GD库)
- GD库 生成缩略图、文字水印、图片水印的函数封装
- PHP---GD库实现图片水印与缩略图
- PHP的GD函数库图形类函数(创建缩略图、给图片加水印、给图片加文字、画布划线)
- PHP 文字水印,缩略图,图片水印实现类
- GD库实现图片水印及缩略图
- PHP调用GD库添加文字水印01
- php使用gd库将文字转换成图片
- php文字水印和php图片水印实现代码
- php文字水印和php图片水印实现代码
- php 图片水印 文字水印
- PHP实现给图片加文字水印
- PHP实现文本以图片格式保存,GD库操作,图片打水印
- PHP面向对象——GD库实现图片水印和缩略图
- 使用 MySQLdb 从数据库拉回数据,并将数据存入 python的DataFrame中
- Unity3d www Http 请求 Headers 验证
- 对比线程安全和可重入函数
- Django 根据数据模型models创建数据表
- E
- php封装成类(GD库实现文字、图片水印、压缩图)
- spring cache缓存
- spark参数调优
- (4)多线程服务器与连接池
- java Exception
- python 参数
- 第一天
- GYM 2015 ACM Syrian Collegiate Programming Contest
- OK ,Let's go...