GD库处理图片文字水印/图片水印/图片压缩
2016-07-30 / PHP / 1989 次围观 / 0 次吐槽 /<?php require 'image.GD.class.php'; $img_url = 'image/1.jpg'; $font_url = 'font/msyh.ttc'; $source = "image/net.jpg"; $content = '水印内容'; $size = 20; $color = array(0=>255,1=>255,2=>255,3=>20); $local = array('x'=>20,'y'=>50); $angle = 10; $alpha = 30; $image = new Image($img_url); $image->fontMark($content, $font_url, $size, $color, $local, $angle);//添加文字水印 $image->imageMark($source, $local, $alpha);//添加图片水印 $image->thumb(500,300);//压缩图片 $image->show();//显示图片 $image->save('nowimage');//保存图片 ?> image.GD.class.php: <?php class Image{ private $info; private $image; public function __construct($img) { //读取图片 $info = getimagesize($img); $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($img); } 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){ //添加图片水印 $info2 = getimagesize($source); $type2 = image_type_to_extension($info2[2],false); $fun2 = "imagecreatefrom{$type2}"; $water = $fun2($source); imagecopymerge($this->image, $water,$local['x'],$local['y'],0,0,$info2[0],$info2[1],$alpha); imagedestroy($water); } public function show(){ //浏览器输出图片 header("content-type:".$this->info['mime']); $func = "image{$this->info['type']}"; $func($this->image); } public function save($newname){ //保存图片 $func = "image{$this->info['type']}"; $func($this->image,$newname.'.'.$this->info['type']); } public function __destruct(){ //销毁内存 imagedestroy($this->image); } }
Powered By Cheug's Blog
Copyright Cheug Rights Reserved.