仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1054|回复: 7
打印 上一主题 下一主题

[学习教程] PHP网页设计php 图片水印类

[复制链接]
只想知道 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-16 22:15:04 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
虽说不上很好,但至少一般的数据操作,再在原有的SQL语言的基础上,用得还是可以的。
  1. <?phpclassThumbHandler{var$dst_img;//方针文件var$h_src;//图片资本句柄var$h_dst;//新图句柄var$h_mask;//水印句柄var$img_create_quality=100;//图片天生质量var$img_display_quality=80;//图片显现质量,默许为75var$img_scale=0;//图片缩放比例var$src_w=0;//原始图片宽度var$src_h=0;//原始图片高度var$dst_w=0;//新图总宽度var$dst_h=0;//新图总高度var$fill_w;//添补图形宽var$fill_h;//添补图形高var$copy_w;//拷贝图形宽var$copy_h;//拷贝图形高var$src_x=0;//原始图片绘制肇端横坐标var$src_y=0;//原始图片绘制肇端纵坐标var$start_x;//新图绘制肇端横坐标var$start_y;//新图绘制肇端纵坐标var$mask_word;//水印笔墨var$mask_img;//水印图片var$mask_pos_x=0;//水印横坐标var$mask_pos_y=0;//水印纵坐标var$mask_offset_x=5;//水印横向偏移var$mask_offset_y=5;//水印纵向偏移var$font_w;//水印字体宽var$font_h;//水印字体高var$mask_w;//水印宽var$mask_h;//水印高var$mask_font_color="#ffffff";//水印笔墨色彩var$mask_font=2;//水印字体var$font_size;//尺寸var$mask_position=0;//水印地位var$mask_img_pct=50;//图片兼并水平,值越年夜,兼并程序越低var$mask_txt_pct=50;//笔墨兼并水平,值越小,兼并程序越低var$img_border_size=0;//图片边框尺寸var$img_border_color;//图片边框色彩var$_flip_x=0;//程度翻转次数var$_flip_y=0;//垂直翻转次数var$cut_type=0;//剪切范例var$img_type;//文件范例//文件范例界说,并指出了输入图片的函数var$all_type=array("jpg"=>array("output"=>"imagejpeg"),"gif"=>array("output"=>"imagegif"),"png"=>array("output"=>"imagepng"),"wbmp"=>array("output"=>"image2wbmp"),"jpeg"=>array("output"=>"imagejpeg"));/***机关函数*/functionThumbHandler(){$this->mask_font_color="#ffffff";$this->font=2;$this->font_size=12;}/***获得图片的宽*/functiongetImgWidth($src){returnimagesx($src);}/***获得图片的高*/functiongetImgHeight($src){returnimagesy($src);}/***设置图片天生路径**@paramstring$src_img图片天生路径*/functionsetSrcImg($src_img,$img_type=null){echo$img_type;if(!file_exists($src_img)){die("图片不存在");}if(!empty($img_type)){$this->img_type=$img_type;}else{$this->img_type=$this->_getImgType($src_img);}$this->_checkValid($this->img_type);//file_get_contents函数请求php版本>4.3.0$src=;if(function_exists("file_get_contents")){$src=file_get_contents($src_img);}else{$handle=fopen($src_img,"r");while(!feof($handle)){$src.=fgets($fd,4096);}fclose($handle);}if(empty($src)){die("图片源为空");}$this->h_src=@ImageCreateFromString($src);$this->src_w=$this->getImgWidth($this->h_src);$this->src_h=$this->getImgHeight($this->h_src);}/***设置图片天生路径**@paramstring$dst_img图片天生路径*/functionsetDstImg($dst_img){$arr=explode(/,$dst_img);$last=array_pop($arr);$path=implode(/,$arr);$this->_mkdirs($path);$this->dst_img=$dst_img;}/***设置图片的显现质量**@paramstring$n质量*/functionsetImgDisplayQuality($n){$this->img_display_quality=(int)$n;}/***设置图片的天生质量**@paramstring$n质量*/functionsetImgCreateQuality($n){$this->img_create_quality=(int)$n;}/***设置笔墨水印**@paramstring$word水印笔墨*@paraminteger$font水印字体*@paramstring$color水印字体色彩*/functionsetMaskWord($word){$this->mask_word.=$word;}/***设置字体色彩**@paramstring$color字体色彩*/functionsetMaskFontColor($color="#ffffff"){$this->mask_font_color=$color;}/***设置水印字体**@paramstringinteger$font字体*/functionsetMaskFont($font=2){if(!is_numeric($font)&&!file_exists($font)){die("字体文件不存在");}$this->font=$font;}/***设置笔墨字体巨细,仅对truetype字体无效*/functionsetMaskFontSize($size="12"){$this->font_size=$size;}/***设置图片水印**@paramstring$img水印图片源*/functionsetMaskImg($img){$this->mask_img=$img;}/***设置水印横向偏移**@paraminteger$x横向偏移量*/functionsetMaskOffsetX($x){$this->mask_offset_x=(int)$x;}/***设置水印纵向偏移**@paraminteger$y纵向偏移量*/functionsetMaskOffsetY($y){$this->mask_offset_y=(int)$y;}/***指定水印地位**@paraminteger$position地位,1:左上,2:左下,3:右上,0/4:右下*/functionsetMaskPosition($position=0){$this->mask_position=(int)$position;}/***设置图片兼并水平**@paraminteger$n兼并水平*/functionsetMaskImgPct($n){$this->mask_img_pct=(int)$n;}/***设置笔墨兼并水平**@paraminteger$n兼并水平*/functionsetMaskTxtPct($n){$this->mask_txt_pct=(int)$n;}/***设置缩略图边框**@param(范例)(参数名)(形貌)*/functionsetDstImgBorder($size=1,$color="#000000"){$this->img_border_size=(int)$size;$this->img_border_color=$color;}/***程度翻转*/functionflipH(){$this->_flip_x++;}/***垂直翻转*/functionflipV(){$this->_flip_y++;}/***设置剪切范例**@param(范例)(参数名)(形貌)*/functionsetCutType($type){$this->cut_type=(int)$type;}/***设置图片剪切**@paraminteger$width矩形剪切*/functionsetRectangleCut($width,$height){$this->fill_w=(int)$width;$this->fill_h=(int)$height;}/***设置源图剪切肇端坐标点**@param(范例)(参数名)(形貌)*/functionsetSrcCutPosition($x,$y){$this->src_x=(int)$x;$this->src_y=(int)$y;}/***创立图片,主函数*@paraminteger$a当短少第二个参数时,此参数将用作百分比,*不然作为宽度值*@paraminteger$b图片缩放后的高度*/functioncreateImg($a,$b=null){$num=func_num_args();if(1==$num){$r=(int)$a;if($r<1){die("图片缩放比例不得小于1");}$this->img_scale=$r;$this->_setNewImgSize($r);}if(2==$num){$w=(int)$a;$h=(int)$b;if(0==$w){die("方针宽度不克不及为0");}if(0==$h){die("方针高度不克不及为0");}$this->_setNewImgSize($w,$h);}if($this->_flip_x%2!=0){$this->_flipH($this->h_src);}if($this->_flip_y%2!=0){$this->_flipV($this->h_src);}$this->_createMask();$this->_output();//开释if(imagedestroy($this->h_src)&&imagedestroy($this->h_dst)){Returntrue;}else{Returnfalse;}}/***天生水印,挪用了天生水印笔墨和水印图片两个办法*/function_createMask(){if($this->mask_word){//猎取字体信息$this->_setFontInfo();if($this->_isFull()){die("水印笔墨过年夜");}else{$this->h_dst=imagecreatetruecolor($this->dst_w,$this->dst_h);$white=ImageColorAllocate($this->h_dst,255,255,255);imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$white);//添补背景致$this->_drawBorder();imagecopyresampled($this->h_dst,$this->h_src,$this->start_x,$this->start_y,$this->src_x,$this->src_y,$this->fill_w,$this->fill_h,$this->copy_w,$this->copy_h);$this->_createMaskWord($this->h_dst);}}if($this->mask_img){$this->_loadMaskImg();//加载时,获得宽高if($this->_isFull()){//将水印天生在原始图片上再拷$this->_createMaskImg($this->h_src);$this->h_dst=imagecreatetruecolor($this->dst_w,$this->dst_h);$white=ImageColorAllocate($this->h_dst,255,255,255);imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$white);//添补背景致$this->_drawBorder();imagecopyresampled($this->h_dst,$this->h_src,$this->start_x,$this->start_y,$this->src_x,$this->src_y,$this->fill_w,$this->start_y,$this->copy_w,$this->copy_h);}else{//创立新图并拷贝$this->h_dst=imagecreatetruecolor($this->dst_w,$this->dst_h);$white=ImageColorAllocate($this->h_dst,255,255,255);imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$white);//添补背景致$this->_drawBorder();imagecopyresampled($this->h_dst,$this->h_src,$this->start_x,$this->start_y,$this->src_x,$this->src_y,$this->fill_w,$this->fill_h,$this->copy_w,$this->copy_h);$this->_createMaskImg($this->h_dst);}}if(empty($this->mask_word)&&empty($this->mask_img)){$this->h_dst=imagecreatetruecolor($this->dst_w,$this->dst_h);$white=ImageColorAllocate($this->h_dst,255,255,255);imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$white);//添补背景致$this->_drawBorder();imagecopyresampled($this->h_dst,$this->h_src,$this->start_x,$this->start_y,$this->src_x,$this->src_y,$this->fill_w,$this->fill_h,$this->copy_w,$this->copy_h);}}/***画边框*/function_drawBorder(){if(!empty($this->img_border_size)){$c=$this->_parseColor($this->img_border_color);$color=ImageColorAllocate($this->h_src,$c[0],$c[1],$c[2]);imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$color);//添补背景致}}/***天生水印笔墨*/function_createMaskWord($src){$this->_countMaskPos();$this->_checkMaskValid();$c=$this->_parseColor($this->mask_font_color);$color=imagecolorallocatealpha($src,$c[0],$c[1],$c[2],$this->mask_txt_pct);if(is_numeric($this->font)){imagestring($src,$this->font,$this->mask_pos_x,$this->mask_pos_y,$this->mask_word,$color);}else{imagettftext($src,$this->font_size,0,$this->mask_pos_x,$this->mask_pos_y,$color,$this->font,$this->mask_word);}}/***天生水印图*/function_createMaskImg($src){$this->_countMaskPos();$this->_checkMaskValid();imagecopymerge($src,$this->h_mask,$this->mask_pos_x,$this->mask_pos_y,0,0,$this->mask_w,$this->mask_h,$this->mask_img_pct);imagedestroy($this->h_mask);}/***加载水印图*/function_loadMaskImg(){$mask_type=$this->_getImgType($this->mask_img);$this->_checkValid($mask_type);//file_get_contents函数请求php版本>4.3.0$src=;if(function_exists("file_get_contents")){$src=file_get_contents($this->mask_img);}else{$handle=fopen($this->mask_img,"r");while(!feof($handle)){$src.=fgets($fd,4096);}fclose($handle);}if(empty($this->mask_img)){die("水印图片为空");}$this->h_mask=ImageCreateFromString($src);$this->mask_w=$this->getImgWidth($this->h_mask);$this->mask_h=$this->getImgHeight($this->h_mask);}/***图片输入*/function_output(){$img_type=$this->img_type;$func_name=$this->all_type[$img_type][output];if(function_exists($func_name)){//判别扫瞄器,如果IE就不发送头if(isset($_SERVER[HTTP_USER_AGENT])){$ua=strtoupper($_SERVER[HTTP_USER_AGENT]);if(!preg_match(/^.*MSIE.*)$/i,$ua)){header("Content-type:$img_type");}}$func_name($this->h_dst,$this->dst_img,$this->img_display_quality);}else{Returnfalse;}}/***剖析色彩**@paramstring$color十六进制色彩*/function_parseColor($color){$arr=array();for($ii=1;$ii<strlen($color);$ii++){$arr[]=hexdec(substr($color,$ii,2));$ii++;}Return$arr;}/***盘算出地位坐标*/function_countMaskPos(){if($this->_isFull()){switch($this->mask_position){case1://左上$this->mask_pos_x=$this->mask_offset_x+$this->img_border_size;$this->mask_pos_y=$this->mask_offset_y+$this->img_border_size;break;case2://左下$this->mask_pos_x=$this->mask_offset_x+$this->img_border_size;$this->mask_pos_y=$this->src_h-$this->mask_h-$this->mask_offset_y;break;case3://右上$this->mask_pos_x=$this->src_w-$this->mask_w-$this->mask_offset_x;$this->mask_pos_y=$this->mask_offset_y+$this->img_border_size;break;case4://右下$this->mask_pos_x=$this->src_w-$this->mask_w-$this->mask_offset_x;$this->mask_pos_y=$this->src_h-$this->mask_h-$this->mask_offset_y;break;default://默许将水印放到右下,偏移指定像素$this->mask_pos_x=$this->src_w-$this->mask_w-$this->mask_offset_x;$this->mask_pos_y=$this->src_h-$this->mask_h-$this->mask_offset_y;break;}}else{switch($this->mask_position){case1://左上$this->mask_pos_x=$this->mask_offset_x+$this->img_border_size;$this->mask_pos_y=$this->mask_offset_y+$this->img_border_size;break;case2://左下$this->mask_pos_x=$this->mask_offset_x+$this->img_border_size;$this->mask_pos_y=$this->dst_h-$this->mask_h-$this->mask_offset_y-$this->img_border_size;break;case3://右上$this->mask_pos_x=$this->dst_w-$this->mask_w-$this->mask_offset_x-$this->img_border_size;$this->mask_pos_y=$this->mask_offset_y+$this->img_border_size;break;case4://右下$this->mask_pos_x=$this->dst_w-$this->mask_w-$this->mask_offset_x-$this->img_border_size;$this->mask_pos_y=$this->dst_h-$this->mask_h-$this->mask_offset_y-$this->img_border_size;break;default://默许将水印放到右下,偏移指定像素$this->mask_pos_x=$this->dst_w-$this->mask_w-$this->mask_offset_x-$this->img_border_size;$this->mask_pos_y=$this->dst_h-$this->mask_h-$this->mask_offset_y-$this->img_border_size;break;}}}/***设置字体信息*/function_setFontInfo(){if(is_numeric($this->font)){$this->font_w=imagefontwidth($this->font);$this->font_h=imagefontheight($this->font);//盘算水印字体所占宽高$word_length=strlen($this->mask_word);$this->mask_w=$this->font_w*$word_length;$this->mask_h=$this->font_h;}else{$arr=imagettfbbox($this->font_size,0,$this->font,$this->mask_word);$this->mask_w=abs($arr[0]-$arr[2]);$this->mask_h=abs($arr[7]-$arr[1]);}}/***设置新图尺寸**@paraminteger$img_w方针宽度*@paraminteger$img_h方针高度*/function_setNewImgSize($img_w,$img_h=null){$num=func_num_args();if(1==$num){$this->img_scale=$img_w;//宽度作为比例$this->fill_w=round($this->src_w*$this->img_scale/100)-$this->img_border_size*2;$this->fill_h=round($this->src_h*$this->img_scale/100)-$this->img_border_size*2;//源文件肇端坐标$this->src_x=0;$this->src_y=0;$this->copy_w=$this->src_w;$this->copy_h=$this->src_h;//方针尺寸$this->dst_w=$this->fill_w+$this->img_border_size*2;$this->dst_h=$this->fill_h+$this->img_border_size*2;}if(2==$num){$fill_w=(int)$img_w-$this->img_border_size*2;$fill_h=(int)$img_h-$this->img_border_size*2;if($fill_w<0$fill_h<0){die("图片边框过年夜,已凌驾了图片的宽度");}$rate_w=$this->src_w/$fill_w;$rate_h=$this->src_h/$fill_h;switch($this->cut_type){case0://假如原始图片年夜于缩略图,发生减少,不然不减少if($rate_w<1&&$rate_h<1){$this->fill_w=(int)$this->src_w;$this->fill_h=(int)$this->src_h;}else{if($rate_w>=$rate_h){$this->fill_w=(int)$fill_w;$this->fill_h=round($this->src_h/$rate_w);}else{$this->fill_w=round($this->src_w/$rate_h);$this->fill_h=(int)$fill_h;}}$this->src_x=0;$this->src_y=0;$this->copy_w=$this->src_w;$this->copy_h=$this->src_h;//方针尺寸$this->dst_w=$this->fill_w+$this->img_border_size*2;$this->dst_h=$this->fill_h+$this->img_border_size*2;break;//主动裁切case1://假如图片是减少剪切才举行操纵if($rate_w>=1&&$rate_h>=1){if($this->src_w>$this->src_h){$src_x=round($this->src_w-$this->src_h)/2;$this->setSrcCutPosition($src_x,0);$this->setRectangleCut($fill_h,$fill_h);$this->copy_w=$this->src_h;$this->copy_h=$this->src_h;}elseif($this->src_w<$this->src_h){$src_y=round($this->src_h-$this->src_w)/2;$this->setSrcCutPosition(0,$src_y);$this->setRectangleCut($fill_w,$fill_h);$this->copy_w=$this->src_w;$this->copy_h=$this->src_w;}else{$this->setSrcCutPosition(0,0);$this->copy_w=$this->src_w;$this->copy_h=$this->src_w;$this->setRectangleCut($fill_w,$fill_h);}}else{$this->setSrcCutPosition(0,0);$this->setRectangleCut($this->src_w,$this->src_h);$this->copy_w=$this->src_w;$this->copy_h=$this->src_h;}//方针尺寸$this->dst_w=$this->fill_w+$this->img_border_size*2;$this->dst_h=$this->fill_h+$this->img_border_size*2;break;//手工裁切case2:$this->copy_w=$this->fill_w;$this->copy_h=$this->fill_h;//方针尺寸$this->dst_w=$this->fill_w+$this->img_border_size*2;$this->dst_h=$this->fill_h+$this->img_border_size*2;break;default:break;}}//方针文件肇端坐标$this->start_x=$this->img_border_size;$this->start_y=$this->img_border_size;}/***反省水印图是不是年夜于天生后的图片宽高*/function_isFull(){Return($this->mask_w+$this->mask_offset_x>$this->fill_w$this->mask_h+$this->mask_offset_y>$this->fill_h)?true:false;}/***反省水印图是不是凌驾原始图片*/function_checkMaskValid(){if($this->mask_w+$this->mask_offset_x>$this->src_w$this->mask_h+$this->mask_offset_y>$this->src_h){die("水印图片尺寸年夜于原始图片,请减少水印图");}}/***获得图片范例**@paramstring$file_path文件路径*/function_getImgType($file_path){$type_list=array("1"=>"gif","2"=>"jpg","3"=>"png","4"=>"swf","5"=>"psd","6"=>"bmp","15"=>"wbmp");if(file_exists($file_path)){$img_info=@getimagesize($file_path);if(isset($type_list[$img_info[2]])){Return$type_list[$img_info[2]];}}else{die("文件不存在,不克不及获得文件范例!");}}/***反省图片范例是不是正当,挪用了array_key_exists函数,此函数请求*php版本年夜于4.1.0**@paramstring$img_type文件范例*/function_checkValid($img_type){if(!array_key_exists($img_type,$this->all_type)){Returnfalse;}}/***按指定路径天生目次**@paramstring$path路径*/function_mkdirs($path){$adir=explode(/,$path);$dirlist=;$rootdir=array_shift($adir);if(($rootdir!=.$rootdir!=..)&&!file_exists($rootdir)){@mkdir($rootdir);}foreach($adiras$key=>$val){if($val!=.&&$val!=..){$dirlist.="/".$val;$dirpath=$rootdir.$dirlist;if(!file_exists($dirpath)){@mkdir($dirpath);@chmod($dirpath,0777);}}}}/***垂直翻转**@paramstring$src图片源*/function_flipV($src){$src_x=$this->getImgWidth($src);$src_y=$this->getImgHeight($src);$new_im=imagecreatetruecolor($src_x,$src_y);for($y=0;$y<$src_y;$y++){imagecopy($new_im,$src,0,$src_y-$y-1,0,$y,$src_x,1);}$this->h_src=$new_im;}/***程度翻转**@paramstring$src图片源*/function_flipH($src){$src_x=$this->getImgWidth($src);$src_y=$this->getImgHeight($src);$new_im=imagecreatetruecolor($src_x,$src_y);for($x=0;$x<$src_x;$x++){imagecopy($new_im,$src,$src_x-$x-1,0,$x,0,1,$src_y);}$this->h_src=$new_im;}}?>
复制代码
只要实现最基本的功能就可以了就是可以添加留言然后可以显示留言,然后加入管理功能
小妖女 该用户已被删除
沙发
发表于 2015-1-19 05:14:01 | 只看该作者
在我安装pear包的时候老是提示,缺少某某文件,才发现 那群extension 的排列是应该有一点的顺序,而我安装的版本的排序不是正常的排序。没办法我只好把那群冒号加了上去,只留下我需要使用的扩展。
变相怪杰 该用户已被删除
板凳
发表于 2015-1-24 11:20:16 | 只看该作者
我要在声明一下:我是个菜鸟!!我对php这门优秀的语言也是知之甚少。但是我要在这里说一下php在网站开发中最常用的几个功能:
只想知道 该用户已被删除
地板
 楼主| 发表于 2015-2-1 09:01:30 | 只看该作者
这些中手常用的知识,当你把我说的这些关键字都可以熟练运用的时候,你可以选择自己
蒙在股里 该用户已被删除
5#
发表于 2015-2-7 02:15:20 | 只看该作者
多看优秀程序员编写的代码,仔细理解他们解决问题的方法,对自身有很大的帮助。
再见西城 该用户已被删除
6#
发表于 2015-2-19 19:51:21 | 只看该作者
本文当是我的笔记啦,遇到的问题随时填充
7#
发表于 2015-3-6 15:35:20 | 只看该作者
这些都是最基本最常用功能,我们这些菜鸟在系统学习后,可以先对这些功能深入研究。
飘灵儿 该用户已被删除
8#
发表于 2015-3-13 03:35:56 | 只看该作者
要进行开发,搭建环境是首先需要做的事,windows下面我习惯把环境那个安装在C盘下面,因为我配的环境经常出现诡异事件,什么事都没做环境有的时候就不能用啦。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-11-14 13:06

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表