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']}"; $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 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,$newname.'.'.$this->info['type']); } //销毁图片 public function __destruct() { imagedestroy($this->image); } } <?php require "image.class.php"; $src='timg.jpg'; $content="欢迎来到南充农商网"; $font_url="simsun.ttc"; $size=20; $color=array( 0 => 255, 1 => 255, 2 => 255, 3 => 20 ); $local=array( 'x'=>20, 'y'=>30 ); $angle=10; $image=new Image($src); $image->fontMark($content,$font_url,$size,$color,$local,$angle); $image->show(); ?>
orangewangjie3473319