类实例_一个水印图片生成类c_imageCreateShuiyin

<?php
/**********************************************************************************************************************************
*功能说明:实现文字和log图片水印并自动生成需要的大图片和小图片
*建立人员:肖红阳
*建立日期:2010-07-22
*修改日期:2010-07-23
*设计思想:1.实现文字水印2.实现图片水印3.水印后将文件生成略缩图并保存,大小通过$lst_w和$lst_h设定
*使用说明:  
     <?php
       include('../../class/c_gd/c_imageCreateShuiyin.php');
       $pic=new c_imageCreateShuiyin
       ('01.jpg','02.jpg','hello!','simkai.ttf','30',0,50,50,'255','255','0','400','400','300','300','550','550','200','200','小图','大图');
     ?>
*************************************************************************************************************************************/

class c_imageCreateShuiyin{
function __construct($path1,$path2,$text,$font,$f_size,$f_angle,$f_x,$f_y,$c_r,$c_g,$c_b,$dt_x,$dt_y,$sy_x,$sy_y,$sy_w,$sy_h,$lst_w,$lst_h,$s_name,$b_name){
   $this->path1=$path1;         //1底图片文件路径
   $this->path2=$path2;         //2水印图片路径
   $this->text=$text;          //3水印文字内容
   $this->font=$font;          //4水印文字字体路径
   $this->f_size=$f_size;         //5水印文字字体大小
   $this->f_angle=$f_angle;        //6水印文字旋转角度
   $this->f_x=$f_x;          //7水印文字起点x坐标
   $this->f_y=$f_y;          //8水印文字起点y坐标
   $this->c_r=$c_r;          //9水印文字字体red颜色值
   $this->c_g=$c_g;          //10水印文字字体green颜色值
   $this->c_b=$c_b;          //11水印文字字体blue颜色值
   $this->dt_x=$dt_x;          //12水印图片左上角在底图上的x坐标
   $this->dt_y=$dt_y;          //13水印图片左上角在底图上的y坐标
   $this->sy_x=$sy_x;          //14水印图片截取起点X坐标
   $this->sy_y=$sy_y;          //15水印图片截取起点y坐标
   $this->sy_w=$sy_w;          //16水印图片截取宽度
   $this->sy_h=$sy_h;          //17水印图片截取高度
   $this->lst_w=$lst_w;         //18生成的略缩图宽度
   $this->lst_h=$lst_h;         //生成的略缩图高度
   $this->s_name=$s_name;         //小图文件名
   $this->b_name=$b_name;         //大图文件名
   $this->cre_s_pass="../../pic_s/";       //小图生成路径
   $this->cre_b_pass="../../pic_b/";       //大图生成路径
  
   $this->echoImage();
}
/******************************从文件或路径新建一php图像,并返回该图像句柄************************/
function imageCreateFrom($path){    
   $image=$path;
   $img_mess=GetImageSize($image);     //获取图片信息 
   switch($img_mess[2])       //判断图片格式后新建PHP的图像
   {
    case 1:
   
$img=@ImageCreateFromGIF($image);   //从GIF文件或 URL 新建一图像
    return $img;
    break;
    case 2:
   
$img=@ImageCreateFromJPEG($image);   //从GIF文件或 URL 新建一图像
    return $img;
    break;
    case 3:
   
$img=@ImageCreateFromPNG($image);   //从GIF文件或 URL 新建一图像
    return $img;
    break;
   }
   
}
/******************************生成文字图片水印图片及略缩图************************/
function setImage(){
    if(!file_exists($this->font)){ echo "<script>alert('字体".$this->font."不存在');history.back();</script>";}//判断字体是否存在
   if(file_exists($this->path1)){                   //判断原始图片是否存在
    $im=$this->imageCreateFrom($this->path1);
    if(file_exists($this->path2)){
     $gi=$this->imageCreateFrom($this->path2); 
     imagecopy($im,$gi,$this->dt_x,$this->dt_y,$this->sy_x,$this->sy_y,$this->sy_w,$this->sy_h);   //实现图片水印
    }
    if(!file_exists($this->font)){ echo "<script>alert('字体".$this->font."不存在');history.back();</script>";}//判断字体是否存在
    $te = imagecolorallocate($im,$this->c_r,$this->c_g,$this->c_b);          //设置文字颜色
    $str = iconv("gbk","utf-8",$this->text);               //设置要绘制的中文文字
    $img=$this->imageCreateFrom($this->path1);
    imagettftext($im,$this->f_size,$this->f_angle,$this->f_x,$this->f_y,$te,$this->font,$str);   //文字输入图片  
    $smallpic=imagecreatetruecolor( $this->lst_w,$this->lst_h );          //建立新层
   
   
    $img=$this->imageCreateFrom($this->path1);              
    imageCopyResized($smallpic,$im,0,0,0,0,$this->lst_w,$this->lst_h,imagesx($img),imagesy($img)); //制作新的图片
    $img_arr=array($im,$smallpic);                  //将大图和小图句柄存入数组      
    return $img_arr;
   }
  
}

function echoImage(){
   $img_arr=$this->setImage(); 
   if(!is_dir($this->cre_s_pass)){ echo "<script>alert('目录".$this->cre_s_pass."不存在');history.back();</script>";}
   else if(!is_dir($this->cre_b_pass)){ echo "<script>alert('目录".$this->cre_b_pass."不存在');history.back();</script>";} 
    else{
      imagejpeg($img_arr[0],"$this->cre_b_pass $this->b_name" );           //生成大图../../smpic/bg_$image**/
      imagejpeg($img_arr[1],"$this->cre_s_pass $this->s_name");           //生成小图../../bgpic/sm_$image**/
     }
}
}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值