1 给图片进行水印添加
2生成一个新的缩率图
php代码
<?php class image{ //水印配置项 private $wateron; private $waterimg; private $waterpos; private $waterpct; private $watertext; private $waterfont; private $watertextsize; private $watertextcolor; private $qua; //缩略图配置项 private $thumbwidth; private $thumbheight; private $thumbtype; private $thumbendfix; //构造函数 public function __construct(){ $this->wateron=c("water_on"); $this->waterimg=c("water_img"); $this->waterpos=c("water_pos"); $this->waterpct=c("water_pct"); $this->watertext=c("water_text"); $this->waterfont=c("water_font"); $this->watertextsize=c("water_text_size"); $this->watertextcolor=c("water_text_color"); $this->qua=c("water_qua"); //缩率图 $this->thumbwidth=c("thumb_width"); $this->thumbheight=c("thumb_height"); $this->thumbtype=c("thumb_type"); $this->thumbendfix=c("thumb_endfix"); } /* *验证图片是否合法 */ private function check($img){ return is_file($img)&&getimagesize($img)&&extension_loaded("gd"); } /* *缩率图 *@param string $img 原图 *@param string $outfile 缩率之后存储的图片 *@param int $thumbwidth 缩率图宽度 *@param int $thumbheight 缩率图高度 *@param int $thumbtype 那种方式进行缩略处理 */ public function thumb($img,$outfile="",$thumbwidth="",$thumbheight="",$thumbtype=""){ if(!$this->check($img)){ return false; } //缩率图处理方式 $thumbtype=$thumbtype?$thumbtype:$this->thumbtype; //缩率图宽度 $thumbwidth=$thumbwidth?$thumbwidth:$this->thumbwidth; //缩率图高度 $thumbheight=$thumbheight?$thumbheight:$this->thumbheight; //获取原图信息 $imginfo=getimagesize($img); //原图宽度 $imgwidth=$imginfo[0]; //原图高度 $imgheight=$imginfo[1]; //获得原图类型 $imgtype=image_type_to_extension($imginfo[2]); //根据不同的缩略处理方式,获得尺寸(原图和缩略图相应的尺寸) $thumb_size=$this->thumbsize($imgwidth,$imgheight,$thumbwidth,$thumbheight,$thumbtype); //创建原图 $func="imagecreatefrom".substr($imgtype,1);//变量函数 $resimg=$func($img); //创建缩率图画布 if($imgtype==".gif"){ $res_thumb=imagecreate($thumb_size[2],$thumb_size[3]); }else{ $res_thumb=imagecreatetruecolor($thumb_size[2],$thumb_size[3]); } imagecopyresized($res_thumb,$resimg,0,0,0,0,$thumb_size[2],$thumb_size[3],$thumb_size[0],$thumb_size[1]); $fileinfo=pathinfo($img);//文件信息 $outfile=$outfile?$outfile:$fileinfo['filename'].$this->thumbendfix.$fileinfo['extension'];//文件名称 $outfile=$fileinfo["dirname"]."/".$outfile;//加上目录 $func="image".substr($imgtype,1); $func($res_thumb,$outfile); return $outfile; } private function thumbsize($imgwidth,$imgheight,$thumbwidth,$thumbheight,$thumbtype){ //缩率图尺寸 $w=$thumbwidth; $h=$thumbheight; //原图尺寸 $img_w=$imgwidth; $img_h=$imgheight; switch($thumbtype){ case 1: //宽度固定,高度自增 $h=$w/$imgwidth*$imgheight; break; case 2://高度固定,宽度自 $w=$h/$imgheight*$imgwidth; break; case 3: if($imgheight/$thumbheight>$imgwidth/$thumbwidth){ $img_h=$imgwidth/$thumbwidth*$thumbheight; }else{ $img_w=$imgheight/$thumbheight*$thumbwidth; } } return array($img_w,$img_h,$w,$h); } /* *@param string $img 原图 *@param string $outimg 加完水印后生成的图 *@param int $pos 水印位置 *@param int $pct 透明度 *@param text $text 水印文字 *@param string $waterimg水印图片 */ public function water($img,$outimg=null,$pos="",$pct="",$text="",$waterimg="",$textcolor=""){ if(!$this->check($img)){ return false; } //加完水印后生成的图 $outimg=$outimg?$outimg:$img; //水印位置 $pos=$pos?$pos:$this->waterpos; //透明度 $pct=$pct?$pct:$this->waterpct; //水印文字 $text=$text?$text:$this->watertext; //水印图片 $waterimg=$waterimg?$waterimg:$this->waterimg; //验证水印图片 $waterimgon=$this->check($waterimg); //水印文字颜色 $textcolor=$textcolor?$textcolor:$this->watertextcolor; //原图信息 $imginfo=getimagesize($img); //原图宽度 $imgwidth=$imginfo[0]; //原图高度 $imgheight=$imginfo[1]; switch($imginfo[2]){ case 1: $resimg=imagecreatefromgif($img); break; case 2: $resimg=imagecreatefromjpeg($img); break; case 3: $resimg=imagecreatefrompng($img); break; } if($waterimgon){//水印图片有效 //水印信息 $waterinfo=getimagesize($waterimg); //水印宽度 $waterwidth=$waterinfo[0]; //水印高度 $waterheight=$waterinfo[1]; //根据不同的情况创建不同的类型 gif jpeg png $w_img=null; switch($waterinfo[2]){ case 1: $w_img=imagecreatefromgif($waterimg); break; case 2: $w_img=imagecreatefromjpeg($waterimg); break; case 3: $w_img=imagecreatefrompng($waterimg); } }else{//水印图片失效,使用文字水印 if(empty($text)||strlen($textcolor)!==7){ return false; } //获得文字水印盒子信息 $textinfo=imagettfbbox($this->watertextsize,0,$this->waterfont,$text); //文字信息宽度 $textwidth=$textinfo[2]-$textinfo[6]; //文字信息高度 $textheight=$textinfo[3]-$textinfo[7]; } //水印位置 $x=$y=20; switch($pos){ case 1: break; case 2: $x=($imgwidth-$waterwidth)/2; break; case 3: $y=$imgwidth-$waterwidth-10; break; case 4: $x=($imgheight-$waterheight)/2; break; case 5: $x=($imgwidth-$waterwidth)/2; $y=($imgheight-$waterheight)/2; break; case 6: $x=$imgwidth-$waterwidth-10; $y=($imgheight-$waterheight)/2; break; case 7: $x=$imgheight-$waterheight-10; break; case 8: $x=($imgwidth-$waterwidth)/2; $y=$imgheight-$waterheight-10; break; case 9: $x=$imgwidth-$waterwidth-10; $y=$imgheight-$waterheight-10; break; default: $x=mt_rand(20,$imgwidth-$waterwidth); $y=mt_rand(20,$imgheight-$waterheight); } if($waterimgon){//当水印图片有效时,以图片形式加水印 if($waterinfo[2]==3){ imagecopy($resimg,$w_img,$x,$y,0,0,$waterwidth,$waterheight); }else{ imagecopymerge($resimg,$w_img,$x,$y,0,0,$waterinfo,$waterheight,$pct); } }else{//水印图片失效,以文字水印加 $red=hexdec(substr($this->watertextcolor,1,2)); $greem=hexdec(substr($this->watertextcolor,3,2)); $blue=hexdec(substr($this->watertextcolor,5,2)); $color=imagecolorallocate($resimg,$red,$greem,$blue); imagettftext($resimg,$this->watertextsize,0,$x,$y,$color,$this->waterfont,$text); } //输出图片 switch($imginfo[2]){ case 1: imagegif($resimg,$outimg); break; case 2: imagejpeg($resimg,$outimg); break; case 3: imagepng($resimg,$outimg); break; } //垃圾回收 if(isset($resimg)){ imagedestroy($resimg); } if(isset($w_img)){ imagedestroy($w_img); } return true; } } ?>
<?php return array( //水印处理 "water_on"=>1,//水印开关 "water_img"=>"./data/logo.png",//水印图片 "water_pos"=>9,//水印位置 "water_pct"=>80,//水印透明度 "water_text"=>"http://www.caoxiaobin.cn", "water_font"=>"./data/simsunb.ttf",//水印字体 "water_text_color"=>"#333333",//文字颜色 16进制表示 "water_text_size"=>16,//文字大小 "water_qua"=>80,//图片压缩比 //缩略图 "thumb_width"=>150,//缩率图宽度 "thumb_height"=>150,//缩略图高度 "thumb_type"=>1,//缩略图处理 1宽度固定,高度自增 2高度固定,宽度自增 //缩略图尺寸不变,对原图进行裁切 "thumb_endfix"=>"_thmub"//缩略图后缀 ); ?>
/* * 不区分大小写的数据键检测 */ function array_key_exists_d($key,$arr){ $_key=strtolower($key); foreach ($arr as $k=>$v){ if($_key==strtolower($k)){ return true; } } } /* * 递归更改数组的key(键名) * @param array; * @stat int 0小写 1大写 */ function array_change_key_case_d($arr,$stat=0){ $func=$stat?"strtoupper":"strtolower"; $_newarr=array(); if(!is_array($arr)||empty($arr)){ return $_newarr; } foreach($arr as $k=>$v){ $_k=$func($k);//通过变量函数转换key大小写 $_newarr[$_k]= is_array($v)?array_change_key_case_d($v):$v; } return $_newarr; } /* * 读取与设置配置项 * @param $name void 配置项名称,如果不填写返回所有配置项 * @param $value void 配置项的值 * @param $value 值 false null 只取$name值 */ function c($name=null,$value=null){ static $config=array();//静态变量$config存储所有配置项 if(is_null($name)){ return $config; } //如果$name为数组 if(is_array($name)){ return $config=array_merge($config,array_change_key_case_d($name,1)); } //$name为字符串 2种情况 $value无值表示获得配置项的值,有值表示更改配置项 if(is_string($name)){ $name= strtoupper($name); //获得配置项的值 if(is_null($value)){ return array_key_exists_d($name,$config)?$config[$name]:null; }else{ //设置值 $config[$name]=$value; return true; } } }
