/*
* 成效:PHP图片水印 (水印支持图片或文字)
* 参数:
* $groundImage
背景图片,即必要加水印的图纸,暂只帮助GIF,JPG,PNG格式;
* $waterPos 水印地点,有10种情状,0为随飞机地方置;
* 1为顶上部分居左,2为顶部居中,3为最上部居右;
* 4为宗旨居左,5为主题居中,6为中心居右;
* 7为底端居左,8为底端居中,9为底端居右;
* $waterImage 图片水印,即作为水印的图形,暂只扶助GIF,JPG,PNG格式;
* $waterText 文字水印,即把文字作为为水印,匡助ASCII码,不帮衬中文;
* $textFont 文字大小,值为1、2、3、4或5,默以为5;
* $textColor 文字颜色,值为十六进制颜色值,默以为#FF0000(红色);
*
* 注意:Support GD 2.0,Support FreeType、GIF Read、GIF Create、JPG
、PNG
* $waterImage 和 $waterText 最佳不用同一时间采纳,选中间之一就可以,优先利用
$waterImage。
*
当$waterImage有效时,参数$waterString、$stringFont、$stringColor均不奏效。
* 加水印后的图样的文书名和 $groundImage 同样。
* 作者:longware @ 2004-11-3 14:15:13
*/
function
imageWaterMark($groundImage,$waterPos=0,$waterImage=””,$waterText=””,$textFont=5,$textColor=”#FF0000″)
{
$isWaterImage = FALSE;
$formatMsg =
“暂不支持该文件格式,请用图片管理软件将图纸转变为GIF、JPG、PNG格式。”;

php 图片管理类,缩略,水印

public function water($filename,$water,$pos=0,$pct=80){
                // 加载水印图片
                $info = $this->getImageInfo($water);
                if(!empty($info[0])){
                        $water_w = $info[0];
                        $water_h = $info[1];
                        $type = $info[‘type’];
                        $fun  = ‘imagecreatefrom’.$type;
                        $waterimg = $fun($water);
                } else{
                        return false;
                }
                // 加载背景图片
                $info = $this->getImageInfo($this->img);
                if(!empty($info[0])){
                        $old_w = $info[0];
                        $old_h = $info[1];
                        $type  = $info[‘type’];
                        $fun   = ‘imagecreatefrom’.$type;
                        $oldimg = $fun($this->img);
                } else{
                        return false;
                }
                // 剪切水印
                $water_w >$old_w && $water_w = $old_w;
                $water_h >$old_h && $water_h = $old_h;

//读取水印文件
if(!emptyempty($waterImage) && file_exists($waterImage))
{
$isWaterImage = TRUE;
$water_info = getimagesize($waterImage);
$water_w = $water_info[0];//获得水印图片的宽
$water_h = $water_info[1];//获得水印图片的高

class Image {

                // 水印地方
                switch($pos){
                        case 0://随机
                    $posX = rand(0,($old_w – $water_w));
                    $posY = rand(0,($old_h – $water_h));
                    break;
                case 1://1为最上部居左
                    $posX = 0;
                    $posY = 0;
                    break;
                case 2://2为最上端居中
                    $posX = ($old_w – $water_w) / 2;
                    $posY = 0;
                    break;
                case 3://3为顶上部分居右
                    $posX = $old_w – $water_w;
                    $posY = 0;
                    break;
                case 4://4为当道居左
                    $posX = 0;
                    $posY = ($old_h – $water_h) / 2;
                    break;
                case 5://5为中心居中
                    $posX = ($old_w – $water_w) / 2;
                    $posY = ($old_h – $water_h) / 2;
                    break;
                case 6://6为宗旨居右
                    $posX = $old_w – $water_w;
                    $posY = ($old_h – $water_h) / 2;
                    break;
                case 7://7为底端居左
                    $posX = 0;
                    $posY = $old_h – $water_h;
                    break;
                case 8://8为底端居中
                    $posX = ($old_w – $water_w) / 2;
                    $posY = $old_h – $water_h;
                    break;
                case 9://9为底端居右
                    $posX = $old_w – $water_w;
                    $posY = $old_h – $water_h;
                    break;
                default: //随机
                    $posX = rand(0,($old_w – $water_w));
                    $posY = rand(0,($old_h – $water_h));
                    break;
                }
            // 设定图像的混色格局
                imagealphablending($oldimg, true);
                // 增多水印
                imagecopymerge($oldimg, $waterimg, $posX, $posY, 0, 0,
$water_w,$water_h,$pct);
                $fun = ‘image’.$type;
                !@$fun($oldimg, $filename,100) &&
die(‘保存失败!检查目录是或不是存在并且可写?’);
                  imagedestroy($oldimg);
                  imagedestroy($waterimg);
                  return $filename;
        }

switch($water_info[2])//取得水印图片的格式
{
case 1:$water_im = imagecreatefromgif($waterImage);break;
case 2:$water_im = imagecreatefromjpeg($waterImage);break;
case 3:$water_im = imagecreatefrompng($waterImage);break;
default:die($formatMsg);
}
}

 /**
  * @var string $fileName 文件名
  * @access private
  */
 private $fileName = ”;
 
 /**
  * @var gd resource $imageResource 原图像
  * @access private
  */
 private $imageResource = NULL;
 
 /**
  * @var int $imageWidth 原图像宽
  * @access private
  */
 private $imageWidth = NULL;
 
 /**
  * @var int $imageHeight 原图像高
  * @access private
  */
 private $imageHeight = NULL;
 
 /**
  * @var int $imageType 原图像类型
  * @access private
  */
 private $imageType = NULL;
 
 /**
  * @var int $newResource 新图像
  * @access private
  */
 private $newResource = NULL;
 
 /**
  * @var int $newResType 新图像类型
  * @access private
  */
 private $newResType = NULL;
 
 /**
  * 构造函数
  * @param string $fileName 文件名
     */
 public function __construct($fileName = NULL) {
  $this->fileName = $fileName;
  if ($this->fileName) {
   $this->getSrcImageInfo();
  }
 }
 
 /**
  * 取源图像新闻
  * @access private
  * @return void
  */
 private function getSrcImageInfo() {
  $info = $this->getImageInfo();
  $this->imageWidth = $info[0];
  $this->imageHeight = $info[1];
  $this->imageType = $info[2];
 }

//读取背景图片
if(!emptyempty($groundImage) && file_exists($groundImage))
{
$ground_info = getimagesize($groundImage);
$ground_w = $ground_info[0];//获得背景图片的宽
$ground_h = $ground_info[1];//获得背景图片的高

 /**
  * 取图像消息
  * @param string $fileName 文件名
  * @access private
  * @return array
  */
 private function getImageInfo($fileName = NULL) {
  if ($fileName==NULL) {
   $fileName = $this->fileName;
  }
  $info = getimagesize($fileName);
  return $info;
 }

switch($ground_info[2])//取得背景图片的格式
{
case 1:$ground_im = imagecreatefromgif($groundImage);break;
case 2:$ground_im = imagecreatefromjpeg($groundImage);break;
case 3:$ground_im = imagecreatefrompng($groundImage);break;
default:die($formatMsg);
}
}
else
{
die(”须求加水印的图形海市蜃楼!”);
}

 /**
  * 创设源图像GD 能源
  * @access private
  * @return void
  */
 private function createSrcImage () {
  $this->imageResource = $this->createImageFromFile();
 }

//水印地方
if($isWaterImage)//图片水印
{
$w = $water_w;
$h = $water_h;
$label = “图片的”;
}
else//文字水印
{
$temp =
imagettfbbox(ceil($textFont*5),0,”./cour.ttf”,$waterText);//取得使用
TrueType 字体的公文的限制
$w = $temp[2] – $temp[6];
$h = $temp[3] – $temp[7];
unset($temp);
$label = “文字区域”;
}
if( ($ground_w<$w) || ($ground_h<$h) )
{
echo
“必要加水印的图形的尺寸或宽度比水印”.$label.”还小,无法转移水印!”;
return;
}
switch($waterPos)
{
case 0://随机
$posX = rand(0,($ground_w – $w));
$posY = rand(0,($ground_h – $h));
break;
case 1://1为最上端居左
$posX = 0;
$posY = 0;
break;
case 2://2为顶部居中
$posX = ($ground_w – $w) / 2;
$posY = 0;
break;
case 3://3为顶部居右
$posX = $ground_w – $w;
$posY = 0;
break;
case 4://4为当道居左
$posX = 0;
$posY = ($ground_h – $h) / 2;
break;
case 5://5为中心居中
$posX = ($ground_w – $w) / 2;
$posY = ($ground_h – $h) / 2;
break;
case 6://6为宗旨居右
$posX = $ground_w – $w;
$posY = ($ground_h – $h) / 2;
break;
case 7://7为底端居左
$posX = 0;
$posY = $ground_h – $h;
break;
case 8://8为底端居中
$posX = ($ground_w – $w) / 2;
$posY = $ground_h – $h;
break;
case 9://9为底端居右
$posX = $ground_w – $w;
$posY = $ground_h – $h;
break;
default://随机
$posX = rand(0,($ground_w – $w));
$posY = rand(0,($ground_h – $h));
break;
}

 /**
  * 跟据文件创造图像GD 资源
  * @param string $fileName 文件名
  * @return gd resource
  */
    public function createImageFromFile($fileName = NULL)
    {
  if (!$fileName) {
   $fileName = $this->fileName;
   $imgType = $this->imageType;
  }
        if (!is_readable($fileName) || !file_exists($fileName)) {
            throw new Exception(‘Unable to open file “‘ . $fileName .
‘”‘);
        }

//设定图像的混色格局
imagealphablending($ground_im, true);

  if (!$imgType) {
   $imageInfo = $this->getImageInfo($fileName);
   $imgType = $imageInfo[2];
  }
  
        switch ($imgType) {
  case IMAGETYPE_GIF:
   $tempResource = imagecreatefromgif($fileName);
   break;
  case IMAGETYPE_JPEG:
   $tempResource = imagecreatefromjpeg($fileName);
   break;
  case IMAGETYPE_PNG:
   $tempResource = imagecreatefrompng($fileName);
   break;
  case IMAGETYPE_WBMP:
   $tempResource = imagecreatefromwbmp($fileName);
   break;
  case IMAGETYPE_XBM:
   $tempResource = imagecreatefromxbm($fileName);
   break;
  default:
   throw new Exception(‘错误的图片格式,或图表有问

if($isWaterImage)//图片水印
{
imagecopy($ground_im, $water_im, $posX, $posY, 0, 0,
$water_w,$water_h);//拷贝水印到指标文件
}
else//文字水印
{
if( !emptyempty($textColor) && (strlen($textColor)==7) )
{
$R = hexdec(substr($textColor,1,2));
$G = hexdec(substr($textColor,3,2));
$B = hexdec(substr($textColor,5));
}
else
{
die(”水印文字颜色格式不准确!”);
}
imagestring ( $ground_im, $textFont, $posX, $posY, $waterText,
imagecolorallocate($ground_im, $R, $G, $B));
}

题!’);
        }
  return $tempResource;
    }
 /**
  * 退换图像大小
  * @param int $width 宽
  * @param int $height 高
  * @param string $flag 按怎样点子改换 0=长度宽度调换来参数内定的 1=按比

//生成水印后的图形
@unlink($groundImage);
switch($ground_info[2])//猎取背景图片的格式
{
case 1:imagegif($ground_im,$groundImage);break;
case 2:imagejpeg($ground_im,$groundImage);break;
case 3:imagepng($ground_im,$groundImage);break;
default:die($errorMsg);
}

例缩放,长度宽度约束在参数钦点内,2=以宽为约束缩放,3=以高为束缚缩放
  * @return string
  */
 public function resizeImage($width, $height, $flag=1) {
  global $cfg;
  $widthRatio = $width/$this->imageWidth;
  $heightRatio = $height/$this->imageHeight;
  switch ($flag) {
  case 1:
   if ($this->imageHeight < $height && $this-

//释放内存
if(isset($water_info)) unset($water_info);
if(isset($water_im)) imagedestroy($water_im);
unset($ground_info);
imagedestroy($ground_im);
}
//—————————————————————————————
$id=$_REQUEST[‘id’];
$num = count($_FILES[‘userfile’][‘name’]);
print_r($_FILES[‘userfile’]);
print_r($_FILES[‘userfile’][‘name’]);

>imageWidth < $width) {
    $endWidth = $this->imageWidth;
    $endHeight = $this->imageHeight;
    //return;
   } elseif (($this->imageHeight * $widthRatio)

echo $num;
echo “<bR>”;
if(isset($id)){
for($i=0;$i<$id;$i++){

>$height) {
    $endWidth = ceil($this->imageWidth *

if(isset($_FILES) && !emptyempty($_FILES[‘userfile’]) &&
$_FILES[‘userfile’][‘size’]>0)
{
$uploadfile = “./”.time().”_”.$_FILES[‘userfile’][name][$i];
echo “<br>”;
echo $uploadfile;
if (copy($_FILES[‘userfile’][‘tmp_name’][$i], $uploadfile))
{
echo “OK<br>”;

$heightRatio);
    $endHeight = $height;
   } else {
    $endWidth = $width;
    $endHeight = ceil($this->imageHeight *

//文字水印
//imageWaterMark($uploadfile,5,””,”HTTP://www.lvye.info”,5,”#cccccc“);

$widthRatio);
   }
   break;
  case 2:
   $endWidth = $width;
   $endHeight = ceil($this->imageHeight * $widthRatio);
   break;
  case 3:
   $endWidth = ceil($this->imageWidth * $heightRatio);
   $endHeight = $height;
   break;
  case 4:
   $endWidth2 = $width;
   $endHeight2 = $height;
   if ($this->imageHeight < $height && $this-

//图片水印
$waterImage=”logo_ok1.gif”;//水印图片路线
imageWaterMark($uploadfile,9,$waterImage);

>imageWidth < $width) {
    $endWidth = $this->imageWidth;
    $endHeight = $this->imageHeight;
    //return;
   } elseif (($this->imageHeight * $widthRatio)

echo “<img src=””.$uploadfile.”” border=”0”>”;
}
else
{
echo “Fail<br>”;
}
}
}
}

<$height) {
    $endWidth = ceil($this->imageWidth *

?>
<form enctype=”multipart/form-data” method=”POST”>
<?php
for($a=0;$a<$id;$a++){
echo “文件: <input name=”userfile[]” type=”file”><br>”;

$heightRatio);
    $endHeight = $height;
   } else {
    $endWidth = $width;
    $endHeight = ceil($this->imageHeight *

}
?>
<input type=”submit” value=”上传”>
</form>
?>

$widthRatio);
   }
   break;
  case 5:
   $endWidth2 = $width;
   $endHeight2 = $height;
   if ($this->imageHeight > $height && $this-

//下边这段代码是一段图片上传况且给上传的图片加水印

>imageWidth > $width) {
    //都大
    $ratio = max($this->imageHeight/

参数表达:
$max_file_size  : 上传文件大小限制, 单位BYTE
$destination_folder : 上传文件路线
$watermark   : 是不是附加水印(1为加水印,其他为不加水印);
动用验证:
1.
将PHP.INI文件之中的”extension=php_gd2.dll”一行前边的;号去掉,因为我们要用到GD库;

$height,$this->imageWidth/$width);
   }elseif ($this->imageHeight > $height){
    $ratio = $this->imageHeight/$height;
   }elseif ( $this->imageWidth > $width){
    $ratio =$this->imageWidth/$width;
   }else{
    $ratio =1;
   }
   
   $endWidth = $this->imageWidth / $ratio;
   $endHeight = $this->imageHeight / $ratio;
   
   break;
  default:
   $endWidth = $width;
   $endHeight = $height;
   break;
  }
  if ($this->imageResource==NULL) {
   $this->createSrcImage();
  }
  if($flag == 5){
   //直接缩略
   $this->newResource = imagecreatefromjpeg($cfg

  1. 将extension_dir =改为你的php_gd2.dll所在目录;
    3.
    http://www.111cn.net/php.asp;
    ****************************************************/
    //上传文件类型列表
    $uptypes=array(
        ‘image/jpg’, 
        ‘image/jpeg’,
        ‘image/png’,
        ‘image/pjpeg’,
        ‘image/gif’,
        ‘image/bmp’,
        ‘image/x-png’
    );
    $max_file_size=三千000;     //上传文件大小限制, 单位BYTE
    $destination_folder=”uploadimg/”; //上传文件路线
    $watermark=1;      //是或不是附加水印(1为加水印,其余为不加水印);
    $watertype=1;      //水印类型(1为文字,2为图片)
    $waterposition=1;    
    //水印地方(1为左下角,2为右下角,3为左上角,4为右上角,5为居中);
    $waterstring=””;  //水印字符串
    $waterimg=”xplore.gif”;    //水印图片
    $imgpreview=1;      //是或不是变动预览图(1为浮动,其余为不转换);
    $imgpreviewsize=一半;    //缩略图比例
    ?>
    <html>
    <head>
    <title>ZwelL图片上传程序</title>
    <style
    type=”text/css教程”>
    <!–
    body
    {
         font-size: 9pt;
    }
    input
    {
         background-color: #66CCFF;
         border: 1px inset #CCCCCC;
    }
    –>
    </style>
    </head>
    <body>
    <form enctype=”multipart/form-data” method=”post” name=”upform”>
      上传文件:
      <input name=”upfile” type=”file”>
      <input type=”submit” value=”上传”><br>
      允许上传的文件类型为:<?=implode(‘, ‘,$uptypes)?>
    </form>
    <?php
    if ($_SERVER[‘REQUEST_METHOD’] == ‘POST’)
    {
        if (!is_uploaded_file($_FILES[“upfile”][tmp_name]))
        //是还是不是留存文件
        {
             echo “图片不设有www.111cn.net!”;
             exit;
        }
        $file = $_FILES[“upfile”];
        if($max_file_size < $file[“size”])
        //检查文件大小
        {
            echo “文件太大!”;
            exit;
        }
        if(!in_array($file[“type”], $uptypes))
        //检查文件类型
        {
            echo “文件类型不符!”.$file[“type”];
            exit;
        }
        if(!file_exists($destination_folder))
        {
            mkdir($destination_folder);
        }
        $filename=$file[“tmp_name”];
        $image_size = getimagesize($filename);
        $pinfo=pathinfo($file[“name”]);
        $ftype=$pinfo[‘extension’];
        $destination = $destination_folder.time().”.”.$ftype;
        if (file_exists($destination) && $overwrite != true)
        {
            echo “同名文件已经存在了”;
            exit;
        }
        if(!move_uploaded_file ($filename, $destination))
        {
            echo “移动文件出错”;
            exit;
        }
        $pinfo=pathinfo($destination);
        $fname=$pinfo[basename];
        echo ” <font
    color=red>已经成功上传</font><br>文件名:  <font
    color=blue>”.$destination_folder.$fname.”</font><br>”;
        echo ” 宽度:”.$image_size[0];
        echo ” 长度:”.$image_size[1];
        echo “<br> 大小:”.$file[“size”].” bytes”;
        if($watermark==1)
        {
            $iinfo=getimagesize($destination,$iinfo);
           
    $nimage=imagecreatetruecolor($image_size[0],$image_size[1]);
            $white=imagecolorallocate($nimage,255,255,255);
            $black=imagecolorallocate($nimage,0,0,0);
            $red=imagecolorallocate($nimage,255,0,0);
            imagefill($nimage,0,0,$white);
            switch ($iinfo[2])
            {
                case 1:
                $simage =imagecreatefromgif($destination);
                break;
                case 2:
                $simage =imagecreatefromjpeg($destination);
                break;
                case 3:
                $simage =imagecreatefrompng($destination);
                break;
                case 6:
                $simage =imagecreatefromwbmp($destination);
                break;
                default:
                die(“不帮助的文件类型”);
                exit;
            }
           
    imagecopy($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]);
           
    imagefilledrectangle($nimage,1,$image_size[1]-15,80,$image_size[1],$white);
            switch($watertype)
            {
                case 1:   //加水印字符串
               
    imagestring($nimage,2,3,$image_size[1]-15,$waterstring,$black);
                break;
                case 2:   //加水印图片
                $simage1 =imagecreatefromgif(“xplore.gif”);
                imagecopy($nimage,$simage1,0,0,0,0,85,15);
                imagedestroy($simage1);
                break;
            }
            switch ($iinfo[2])
            {
                case 1:
                //imagegif($nimage, $destination);
                imagejpeg($nimage, $destination);
                break;
                case 2:
                imagejpeg($nimage, $destination);
                break;
                case 3:
                imagepng($nimage, $destination);
                break;
                case 6:
                imagewbmp($nimage, $destination);
                //imagejpeg($nimage, $destination);
                break;
            }
            //覆盖原上传文件
            imagedestroy($nimage);
            imagedestroy($simage);
        }
        if($imgpreview==1)
        {
        echo “<br>图片预览:<br>”;
        echo “<img src=””.$destination.””
    width=”.($image_size[0]*$imgpreviewsize).”
    height=”.($image_size[1]*$imgpreviewsize);
        echo ” alt=”图片预览: 文件名:”.$destination.”
    www.111cn.net上传时间:”>”;
        }
    }
    ?>

[‘path’][‘data’].’blank_thumb.jpg’);
  }elseif ($flag==4) {
   $this->newResource = imagecreatetruecolor

($endWidth2,$endHeight2);
  } else {
   $this->newResource = imagecreatetruecolor

($endWidth,$endHeight);
  }
  $this->newResType = $this->imageType;
  if($flag == 5){
   $dest_x = ($width-$endWidth)/2;
   $dest_y = ($height-$endHeight)/2;
   imagecopyresampled($this->newResource, $this-

>imageResource, $dest_x, $dest_y, 0, 0, $endWidth,
$endHeight,$this-

>imageWidth,$this->imageHeight);
  }else{
   imagecopyresampled($this->newResource, $this-

>imageResource, 0, 0, 0, 0, $endWidth,
$endHeight,$this->imageWidth,$this-

>imageHeight);
  }
 }

 /**
  * 给图像加水印
  * @param string $waterContent 水印内容能够是图像文件名,也可以是文


  * @param int $pos 地点0-9得以是数组
  * @param int $textFont 字体大字,当水印内容是文字时有效
  * @param string $textColor 文字颜色,当水印内容是文字时有效
  * @return string
  */
 public function waterMark($waterContent, $pos = 0, $textFont=5,

$textColor=”#ffffff”) {
  $isWaterImage = file_exists($waterContent);
  if ($isWaterImage) {
   $waterImgRes = $this->createImageFromFile

($waterContent);
   $waterImgInfo = $this->getImageInfo($waterContent);
   $waterWidth = $waterImgInfo[0];
   $waterHeight = $waterImgInfo[1];
  } else {
   $waterText = $waterContent;
   //$temp = @imagettfbbox(ceil

($textFont*2.5),0,”./cour.ttf”,$waterContent);
   if ($temp) {
    $waterWidth = $temp[2]-$temp[6];
    $waterHeight = $temp[3]-$temp[7];
   } else {
    $waterWidth = 100;
    $waterHeight = 12;
   }
  }
  if ($this->imageResource==NULL) {
   $this->createSrcImage();
  }
  switch($pos)
  {
  case 0://随机
   $posX = rand(0,($this->imageWidth – $waterWidth));
   $posY = rand(0,($this->imageHeight – $waterHeight));
   break;
  case 1://1为顶上部分居左
   $posX = 0;
   $posY = 0;
   break;
  case 2://2为顶上部分居中
   $posX = ($this->imageWidth – $waterWidth) / 2;
   $posY = 0;
   break;
  case 3://3为顶部居右
   $posX = $this->imageWidth – $waterWidth;
   $posY = 0;
   break;
  case 4://4为主旨居左
   $posX = 0;
   $posY = ($this->imageHeight – $waterHeight) / 2;
   break;
  case 5://5为主题居中
   $posX = ($this->imageWidth – $waterWidth) / 2;
   $posY = ($this->imageHeight – $waterHeight) / 2;
   break;
  case 6://6为当道居右
   $posX = $this->imageWidth – $waterWidth;
   $posY = ($this->imageHeight – $waterHeight) / 2;
   break;
  case 7://7为底端居左
   $posX = 0;
   $posY = $this->imageHeight – $waterHeight;
   break;
  case 8://8为底端居中
   $posX = ($this->imageWidth – $waterWidth) / 2;
   $posY = $this->imageHeight – $waterHeight;
   break;
  case 9://9为底端居右
   $posX = $this->imageWidth – $waterWidth-20;
   $posY = $this->imageHeight – $waterHeight-10;
   break;
  default://随机
   $posX = rand(0,($this->imageWidth – $waterWidth));
   $posY = rand(0,($this->imageHeight – $waterHeight));
   break;    
  }
  imagealphablending($this->imageResource, true); 
  if($isWaterImage) {
   imagecopy($this->imageResource, $waterImgRes, $posX,

$posY, 0, 0, $waterWidth,$waterHeight);   
  } else {
   $R = hexdec(substr($textColor,1,2));
   $G = hexdec(substr($textColor,3,2));
   $B = hexdec(substr($textColor,5));
   
   $textColor = imagecolorallocate($this-

>imageResource, $R, $G, $B);
   imagestring ($this->imageResource, $textFont, $posX,

$posY, $waterText, $textColor);        
  }
  $this->newResource =  $this->imageResource;
  $this->newResType = $this->imageType;
 }
 
 /**
  * 生成验证码图片
  * @param int $width 宽
  * @param string $height 高
  * @param int $length 长度
  * @param int $validType 0=数字,1=字母,2=数字加字母
  * @param string $textColor 文字颜色
  * @param string $backgroundColor 背景颜色
  * @return void
  */
 public function imageValidate($width, $height, $length = 4,

$validType = 1, $textColor = ‘#000000’, $backgroundColor = ‘#ffffff’)
{
  if ($validType==1) {
   //$validString =

‘abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ’;
   //$validLength = 52;
   //no i no l
   $validString =

‘abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNOPQRSTUVWXYZ’;
   $validLength = 48;
  } elseif ($validType==2) {
   //$validString =

‘0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ’;
   //$validLength = 62;
   //no i no l no 1
   $validString =

‘023456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNOPQRSTUVWXYZ’;
   $validLength = 57;
  } else {
   $validString = ‘0123456789’;
   $validLength = 10;
  }
  
  srand((int)time());
  $valid = ”;
  for ($i=0; $i<$length; $i++) {
   $valid .= $validString{rand(0, $validLength-1)};
  }
  $this->newResource = imagecreate($width,$height);
  $bgR = hexdec(substr($backgroundColor,1,2));
  $bgG = hexdec(substr($backgroundColor,3,2));
  $bgB = hexdec(substr($backgroundColor,5,2));
  $backgroundColor = imagecolorallocate($this->newResource,

$bgR, $bgG, $bgB);
  $tR = hexdec(substr($textColor,1,2));
  $tG = hexdec(substr($textColor,3,2));
  $tB = hexdec(substr($textColor,5,2));
  $textColor = imagecolorallocate($this->newResource, $tR,

$tG, $tB);
  for ($i=0;$i<strlen($valid);$i++){
   imagestring($this->newResource,5,$i*$width/

$length+3,2, $valid[$i],$textColor);
  }
  $this->newResType = IMAGETYPE_JPEG;
  return $valid;

 }
 
 /**
  * 展现输出图像
  * @return void
  */
 public function display($fileName=”, $quality=60) {
 
  $imgType = $this->newResType;
  $imageSrc = $this->newResource;
        switch ($imgType) {
  case IMAGETYPE_GIF:
   if ($fileName==”) {
    header(‘Content-type: image/gif’);
   }
   imagegif($imageSrc, $fileName, $quality);
   break;
  case IMAGETYPE_JPEG:
   if ($fileName==”) {
    header(‘Content-type: image/jpeg’);
   }
   imagejpeg($imageSrc, $fileName, $quality);
   break;
  case IMAGETYPE_PNG:
   if ($fileName==”) {
    header(‘Content-type: image/png’);
    imagepng($imageSrc);
   } else {
    imagepng($imageSrc, $fileName);
   }
   break;
  case IMAGETYPE_WBMP:
   if ($fileName==”) {
    header(‘Content-type: image/wbmp’);
   }
   imagewbmp($imageSrc, $fileName, $quality);
   break;
  case IMAGETYPE_XBM:
   if ($fileName==”) {
    header(‘Content-type: image/xbm’);
   }
   imagexbm($imageSrc, $fileName, $quality);
   break;
  default:
   throw new Exception(‘Unsupport image type’);
        }
  imagedestroy($imageSrc);
 }
 
 /**
  * 保存图像
  * @param int $fileNameType 文件名类型 0使用原来的文章件名,1使用内定的文

件名,2在原作件名加上后缀,3发生随机文件名
  * @param string $folder 文件夹路线 为空为与原版的书文件一样
  * @param string $param 参数$fileNameType为2时为文件名加后缀
  * @return void
  */
 public function save($fileNameType = 0, $folder = NULL, $param =

‘_miniature’) {
  if ($folder==NULL) {
   $folder = dirname($this-

>fileName).DIRECTORY_SEPARATOR;
   
  }
  $fileExtName = FileSystem::fileExt($this->fileName, true);
  $fileBesicName = FileSystem::getBasicName($this->fileName,

false);
  switch ($fileNameType) {
   case 1:
    //$newFileName = $folder.$param;
    $newFileName = $folder.basename($this-

>fileName);
    //var_dump($newFileName);
    break;
   case 2:
    $newFileName =

$folder.$fileBesicName.$param.$fileExtName;
    break;
   case 3:
    $tmp = date(‘YmdHis’);
    $fileBesicName = $tmp;
    $i = 0;
    while (file_exists

($folder.$fileBesicName.$fileExtName)) {
     $fileBesicName = $tmp.$i;
     $i++;
    }
    $newFileName =

$folder.$fileBesicName.$fileExtName;
    break;
   default:
    $newFileName = $this->fileName;
    break;
  }
  $this->display($newFileName);
  return $newFileName;
 }
 /**
  * 剪切出选定区域
  *
  * @param string $srcimgurl  原图
  * @param string $endimgurl 管理过的图
  * @param int $x 坐标原点X
  * @param int $y 坐标原点Y
  * @param int $endimg_w 最后图宽
  * @param int $endimg_h 最后图高
  * @param int $border_w 末坐标X
  * @param int $border_h 末坐标Y
  * @param int $scale 原图缩放情形百分比
  * @param int $fix 是或不是自动取值
  */
 public function cutimg

($srcimgurl,$endimgurl,$x,$y,$endimg_w,$endimg_h,$border_w,$border_h,$scale=

100,$fix=0){
  $path = dirname ($endimgurl);
  if (!is_dir($path)) {
   if(!@mkdir ($path, 0777)){
    die (“{$path} 此目录无法创造,文件创立失败”);
   }
  }
  $ground_info = getimagesize($srcimgurl);
  switch($ground_info[2]){
   case 1:$im = imagecreatefromgif($srcimgurl);break;
   case 2:$im = imagecreatefromjpeg($srcimgurl);break;
   case 3:$im = imagecreatefrompng($srcimgurl);break;
   default:die(“图片格式不容许$srcimgurl”);
     }
  if($fix){//方便截取头像的一片段
   if($ground_info[0]<$ground_info[1]){
    $border_w=$ground_info[0];
    $border_h=$endimg_h*$ground_info[0]/

$endimg_w;
   }elseif($ground_info[0]>$ground_info[1]){
    $border_h=$ground_info[1];
    $border_w=$endimg_w*$ground_info[1]/

$endimg_h;
   }else{
    $border_w=$ground_info[0];
    $border_h=$ground_info[1];
   }
  }
  $newim = imagecreatetruecolor($endimg_w, $endimg_h);
  $x=($x*100)/$scale;
  $y=($y*100)/$scale;
  $border_width=($border_w*100)/$scale;
  $border_height=($border_h*100)/$scale;
  imagecopyresampled($newim, $im, 0,0, $x,$y, $endimg_w,

$endimg_h, $border_width, $border_height );
  if(function_exists(“imagegif”)){
   switch($ground_info[2]){
    case 1:imagegif($newim,$endimgurl);break;
    case 2:imagejpeg($newim,$endimgurl);break;
    case 3:imagepng($newim,$endimgurl);break;
    default:die(“errorMsg”);
   }
  }elseif(function_exists(“imagejpeg”)){
   imagejpeg($newim,$endimgurl);
  }else{
   imagepng($newim,$endimgurl);
  }
  imagedestroy ($newim);
  imagedestroy ($im);
 }
}

Author

发表评论

电子邮件地址不会被公开。 必填项已用*标注