免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 42366 | 回复: 0
打印 上一主题 下一主题

对图片进行水印添加以及生成缩率图 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-08-12 13:50 |只看该作者 |倒序浏览
2个方法
1 给图片进行水印添加
2生成一个新的缩率图

代码
  1. <?php
  2.     class Image{
  3.         //水印配置项
  4.         private $waterOn;
  5.         private $waterImg;
  6.         private $waterPos;
  7.         private $waterPct;
  8.         private $waterText;
  9.         private $waterFont;
  10.         private $waterTextSize;
  11.         private $waterTextColor;
  12.         private $qua;
  13.         //缩略图配置项
  14.         private $thumbWidth;
  15.         private $thumbHeight;
  16.         private $thumbType;
  17.         private $thumbEndfix;
  18.         //构造函数
  19.         public function __construct(){
  20.             $this->waterOn=C("WATER_ON");
  21.             $this->waterImg=C("WATER_IMG");
  22.             $this->waterPos=C("WATER_POS");
  23.             $this->waterPct=C("WATER_PCT");
  24.             $this->waterText=C("WATER_TEXT");
  25.             $this->waterFont=C("WATER_FONT");
  26.             $this->waterTextSize=C("WATER_TEXT_SIZE");
  27.             $this->waterTextColor=C("WATER_TEXT_COLOR");
  28.             $this->qua=C("WATER_QUA");
  29.             //缩率图
  30.             $this->thumbWidth=C("THUMB_WIDTH");
  31.             $this->thumbHeight=C("THUMB_HEIGHT");
  32.             $this->thumbType=C("THUMB_TYPE");
  33.             $this->thumbEndFix=C("THUMB_ENDFIX");
  34.             }
  35.         /*
  36.         *验证图片是否合法
  37.         */
  38.         private function check($img){
  39.             return is_file($img)&&getimagesize($img)&&extension_loaded("gd");
  40.             }
  41.         /*
  42.         *缩率图
  43.         *@param string  $img         原图
  44.         *@param string  $outFile     缩率之后存储的图片
  45.         *@param int     $thumbWidth  缩率图宽度
  46.         *@param int     $thumbHeight 缩率图高度
  47.         *@param int     $thumbType   那种方式进行缩略处理
  48.         */
  49.         public function thumb($img,$outFile="",$thumbWidth="",$thumbHeight="",$thumbType=""){
  50.             if(!$this->check($img)){
  51.                 return false;
  52.                 }
  53.             //缩率图处理方式   
  54.             $thumbType=$thumbType?$thumbType:$this->thumbType;
  55.             //缩率图宽度
  56.             $thumbWidth=$thumbWidth?$thumbWidth:$this->thumbWidth;   
  57.             //缩率图高度
  58.             $thumbHeight=$thumbHeight?$thumbHeight:$this->thumbHeight;
  59.             //获取原图信息
  60.             $imgInfo=getimagesize($img);
  61.             //原图宽度
  62.             $imgWidth=$imgInfo[0];
  63.             //原图高度
  64.             $imgHeight=$imgInfo[1];
  65.             //获得原图类型
  66.             $imgtype=image_type_to_extension($imgInfo[2]);
  67.             //根据不同的缩略处理方式,获得尺寸(原图和缩略图相应的尺寸)
  68.             $thumb_size=$this->thumbsize($imgWidth,$imgHeight,$thumbWidth,$thumbHeight,$thumbType);
  69.             //创建原图
  70.             $func="imagecreatefrom".substr($imgtype,1);//变量函数
  71.             $resImg=$func($img);
  72.             //创建缩率图画布
  73.             if($imgtype==".gif"){
  74.                 $res_thumb=imagecreate($thumb_size[2],$thumb_size[3]);
  75.                 }else{
  76.                     $res_thumb=imagecreatetruecolor($thumb_size[2],$thumb_size[3]);
  77.                     }
  78.             imagecopyresized($res_thumb,$resImg,0,0,0,0,$thumb_size[2],$thumb_size[3],$thumb_size[0],$thumb_size[1]);
  79.             $fileInfo=pathinfo($img);//文件信息
  80.             $outFile=$outFile?$outFile:$fileInfo['filename'].$this->thumbEndFix.$fileInfo['extension'];//文件名称
  81.             $outFile=$fileInfo["dirname"]."/".$outFile;//加上目录
  82.             $func="image".substr($imgtype,1);
  83.             $func($res_thumb,$outFile);
  84.             return $outFile;
  85.             }   
  86.         private function thumbSize($imgWidth,$imgHeight,$thumbWidth,$thumbHeight,$thumbType){
  87.             //缩率图尺寸
  88.             $w=$thumbWidth;
  89.             $h=$thumbHeight;
  90.             //原图尺寸
  91.             $img_w=$imgWidth;
  92.             $img_h=$imgHeight;
  93.             switch($thumbType){
  94.                 case 1:
  95.                     //宽度固定,高度自增
  96.                     $h=$w/$imgWidth*$imgHeight;
  97.                     break;
  98.                 case 2://高度固定,宽度自   
  99.                     $w=$h/$imgHeight*$imgWidth;
  100.                     break;
  101.                 case 3:
  102.                     if($imgHeight/$thumbHeight>$imgWidth/$thumbWidth){
  103.                         $img_h=$imgWidth/$thumbWidth*$thumbHeight;
  104.                         }else{
  105.                             $img_w=$imgHeight/$thumbHeight*$thumbWidth;
  106.                             }
  107.                 }
  108.                 return array($img_w,$img_h,$w,$h);
  109.             }
  110.         /*
  111.         *@param string  $img     原图
  112.         *@param string  $outImg  加完水印后生成的图
  113.         *@param int     $pos     水印位置
  114.         *@param int     $pct     透明度
  115.         *@param text    $text    水印文字
  116.         *@param string  $waterImg水印图片      
  117.         */
  118.         public function water($img,$outImg=null,$pos="",$pct="",$text="",$waterImg="",$textColor=""){
  119.             if(!$this->check($img)){
  120.                 return false;
  121.                 }
  122.             //加完水印后生成的图
  123.             $outImg=$outImg?$outImg:$img;
  124.             //水印位置
  125.             $pos=$pos?$pos:$this->waterPos;
  126.             //透明度
  127.             $pct=$pct?$pct:$this->waterPct;
  128.             //水印文字
  129.             $text=$text?$text:$this->waterText;
  130.             //水印图片
  131.             $waterImg=$waterImg?$waterImg:$this->waterImg;
  132.             //验证水印图片
  133.             $waterImgOn=$this->check($waterImg);
  134.             //水印文字颜色
  135.             $textColor=$textColor?$textColor:$this->waterTextColor;
  136.             //原图信息
  137.             $imgInfo=getimagesize($img);
  138.             //原图宽度
  139.             $imgWidth=$imgInfo[0];
  140.             //原图高度
  141.             $imgHeight=$imgInfo[1];
  142.             switch($imgInfo[2]){
  143.                 case 1:
  144.                     $resImg=imagecreatefromgif($img);
  145.                     break;
  146.                 case 2:
  147.                     $resImg=imagecreatefromjpeg($img);
  148.                     break;
  149.                 case 3:
  150.                     $resImg=imagecreatefrompng($img);
  151.                     break;      
  152.                 }
  153.             if($waterImgOn){//水印图片有效
  154.                 //水印信息
  155.                 $waterInfo=getimagesize($waterImg);
  156.                 //水印宽度
  157.                 $waterWidth=$waterInfo[0];
  158.                 //水印高度
  159.                 $waterHeight=$waterInfo[1];
  160.                 //根据不同的情况创建不同的类型 gif jpeg png
  161.                 $w_img=null;
  162.                 switch($waterInfo[2]){
  163.                     case 1:
  164.                         $w_img=imagecreatefromgif($waterImg);
  165.                         break;
  166.                     case 2:
  167.                         $w_img=imagecreatefromjpeg($waterImg);
  168.                         break;
  169.                     case 3:
  170.                         $w_img=imagecreatefrompng($waterImg);      
  171.                     }
  172.                 }else{//水印图片失效,使用文字水印
  173.                     if(empty($text)||strlen($textColor)!==7){
  174.                         return false;
  175.                         }
  176.                     //获得文字水印盒子信息
  177.                     $textInfo=imagettfbbox($this->waterTextSize,0,$this->waterFont,$text);
  178.                     //文字信息宽度
  179.                     $textWidth=$textInfo[2]-$textInfo[6];
  180.                     //文字信息高度   
  181.                     $textHeight=$textInfo[3]-$textInfo[7];
  182.                     }
  183.                 //水印位置
  184.                 $x=$y=20;
  185.                 switch($pos){
  186.                     case 1:
  187.                         break;
  188.                     case 2:
  189.                         $x=($imgWidth-$waterWidth)/2;
  190.                         break;
  191.                     case 3:
  192.                         $y=$imgWidth-$waterWidth-10;
  193.                         break;
  194.                     case 4:
  195.                         $x=($imgHeight-$waterHeight)/2;
  196.                         break;
  197.                     case 5:
  198.                         $x=($imgWidth-$waterWidth)/2;
  199.                         $y=($imgHeight-$waterHeight)/2;
  200.                         break;
  201.                     case 6:
  202.                         $x=$imgWidth-$waterWidth-10;
  203.                         $y=($imgHeight-$waterHeight)/2;
  204.                         break;
  205.                     case 7:
  206.                         $x=$imgHeight-$waterHeight-10;
  207.                         break;
  208.                     case 8:
  209.                         $x=($imgWidth-$waterWidth)/2;
  210.                         $y=$imgHeight-$waterHeight-10;
  211.                         break;
  212.                     case 9:
  213.                         $x=$imgWidth-$waterWidth-10;
  214.                         $y=$imgHeight-$waterHeight-10;      
  215.                         break;
  216.                     default:
  217.                         $x=mt_rand(20,$imgWidth-$waterWidth);
  218.                         $y=mt_rand(20,$imgHeight-$waterHeight);         
  219.                     }   
  220.                 if($waterImgOn){//当水印图片有效时,以图片形式加水印
  221.                     if($waterInfo[2]==3){
  222.                         imagecopy($resImg,$w_img,$x,$y,0,0,$waterWidth,$waterHeight);
  223.                         }else{
  224.                             imagecopymerge($resImg,$w_img,$x,$y,0,0,$waterInfo,$waterHeight,$pct);
  225.                             }
  226.                     }else{//水印图片失效,以文字水印加
  227.                         $red=hexdec(substr($this->waterTextColor,1,2));
  228.                         $greem=hexdec(substr($this->waterTextColor,3,2));
  229.                         $blue=hexdec(substr($this->waterTextColor,5,2));
  230.                         $color=imagecolorallocate($resImg,$red,$greem,$blue);
  231.                         imagettftext($resImg,$this->waterTextSize,0,$x,$y,$color,$this->waterFont,$text);
  232.                         }
  233.                 //输出图片      
  234.                 switch($imgInfo[2]){
  235.                         case 1:
  236.                             imagegif($resImg,$outImg);
  237.                             break;
  238.                         case 2:
  239.                             imagejpeg($resImg,$outImg);
  240.                             break;
  241.                         case 3:
  242.                             imagepng($resImg,$outImg);
  243.                             break;      
  244.                     }   
  245.                 //垃圾回收
  246.                 if(isset($resImg)){
  247.                     imagedestroy($resImg);
  248.                     }
  249.                 if(isset($w_img)){
  250.                     imagedestroy($w_img);
  251.                     }   
  252.                 return true;               
  253.             }   
  254.         }
  255. ?>
复制代码
代码
  1. <?php
  2. return array(
  3.     //水印处理
  4.     "WATER_ON"=>1,//水印开关
  5.     "WATER_IMG"=>"./data/logo.png",//水印图片
  6.     "WATER_POS"=>9,//水印位置
  7.     "WATER_PCT"=>80,//水印透明度
  8.     "WATER_TEXT"=>"http://www.caoxiaobin.cn",
  9.     "WATER_FONT"=>"./data/simsunb.ttf",//水印字体
  10.     "WATER_TEXT_COLOR"=>"#333333",//文字颜色 16进制表示
  11.     "WATER_TEXT_SIZE"=>16,//文字大小
  12.     "WATER_QUA"=>80,//图片压缩比
  13.     //缩略图
  14.     "THUMB_WIDTH"=>150,//缩率图宽度
  15.     "THUMB_HEIGHT"=>150,//缩略图高度
  16.     "THUMB_TYPE"=>1,//缩略图处理  1宽度固定,高度自增 2高度固定,宽度自增 //缩略图尺寸不变,对原图进行裁切
  17.     "THUMB_ENDFIX"=>"_thmub"//缩略图后缀
  18.      
  19. );
  20. ?>
复制代码
代码
  1. /*
  2. * 不区分大小写的数据键检测
  3. */
  4. function array_key_exists_d($key,$arr){
  5.     $_key=strtolower($key);
  6.     foreach ($arr as $k=>$v){
  7.         if($_key==strtolower($k)){
  8.             return true;
  9.         }
  10.     }
  11. }
  12. /*
  13. * 递归更改数组的KEY(键名)
  14. * @param array;
  15. * @stat int 0小写 1大写
  16. */
  17. function array_change_key_case_d($arr,$stat=0){
  18.     $func=$stat?"strtoupper":"strtolower";
  19.     $_newArr=array();
  20.     if(!is_array($arr)||empty($arr)){
  21.         return $_newArr;
  22.     }
  23.     foreach($arr as $k=>$v){
  24.         $_k=$func($k);//通过变量函数转换KEY大小写
  25.         $_newArr[$_k]= is_array($v)?array_change_key_case_d($v):$v;
  26.     }
  27.     return $_newArr;
  28. }
  29. /*
  30. * 读取与设置配置项
  31. * @param $name void 配置项名称,如果不填写返回所有配置项
  32. * @param $value void 配置项的值
  33. * @param $value 值 false null 只取$name值
  34. */
  35. function C($name=null,$value=null){
  36.     static $config=array();//静态变量$config存储所有配置项
  37.     if(is_null($name)){
  38.         return $config;
  39.     }
  40.     //如果$name为数组
  41.     if(is_array($name)){
  42.         return $config=array_merge($config,array_change_key_case_d($name,1));
  43.     }
  44.     //$name为字符串 2种情况 $value无值表示获得配置项的值,有值表示更改配置项
  45.     if(is_string($name)){
  46.         $name=  strtoupper($name);
  47.         //获得配置项的值
  48.         if(is_null($value)){
  49.           return  array_key_exists_d($name,$config)?$config[$name]:null;
  50.         }else{
  51.             //设置值
  52.             $config[$name]=$value;
  53.             return true;
  54.         }
  55.     }
  56. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP