heroicq 发表于 2008-9-28 11:32:22

求助: PHP水印

帮忙看看怎么老是爆错,imagettfbbox() : Invalid font filename

Message: Undefined offset: 1

Filename: controllers/waterImage.php

Line Number: 194

都没有offset,怎么回事?

[ 本帖最后由 heroicq 于 2008-9-28 11:36 编辑 ]

heroicq 发表于 2008-9-28 11:37:20

controller文件:
<?php

class waterImage extends Controller {

      function waterImage()
      {
                parent::Controller();
                $this->load->helper ( 'url' );
      }


               function imageWaterMark($groundImage,$waterPos=0,$waterImage="",$waterText="",$textFont=5,$textColor="#FF0000")
      {
                $isWaterImage = FALSE;
                $formatMsg = "暂不支持该文件格式,请用图片处理软件将图片转换为GIF、JPG、PNG格式。";
                //读取水印文件
                if(!empty($waterImage) && file_exists($waterImage))
                {
                        $isWaterImage = TRUE;
                        $water_info = getimagesize($waterImage);
                        $water_w = $water_info;//取得水印图片的宽
                        $water_h = $water_info;//取得水印图片的高
                        switch($water_info)//取得水印图片的格式
                        {
                              case 1: $water_im = imagecreatefromgif($waterImage);break;
                              case 2: $water_im = imagecreatefromjpeg($waterImage);break;
                              case 3: $water_im = imagecreatefrompng($waterImage);break;
                              default:die($formatMsg);
                        }
                }
                //读取背景图片
                if(!empty($groundImage) && file_exists($groundImage))
                {
                        $ground_info = getimagesize($groundImage);
                        $ground_w = $ground_info;//取得背景图片的宽
                        $ground_h = $ground_info;//取得背景图片的高
                        switch($ground_info)//取得背景图片的格式
                        {
                              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("需要加水印的图片不存在!");
                }
                //水印位置
                if($isWaterImage)//图片水印
                {
                        $w = $water_w;
                        $h = $water_h;
                        $label = "图片的";
                }
                else//文字水印
                {
                        $temp = imagettfbbox(ceil($textFont*5),0,"./arial.ttf",$waterText);//取得使用 TrueType 字体的文本的范围
                        $w = $temp - $temp;
                        $h = $temp - $temp;
                        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;
                }
                //设定图像的混色模式
                imagealphablending($ground_im, true);
                if($isWaterImage)//图片水印
                {
                        imagecopy($ground_im, $water_im, $posX, $posY, 0, 0, $water_w,$water_h);//拷贝水印到目标文件
                }
                else//文字水印
                {
                        if( !empty($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));
                }
                //生成水印后的图片
                @unlink($groundImage);
                switch($ground_info)//取得背景图片的格式
                {
                        case 1:imagegif($ground_im,$groundImage);break;
                        case 2:imagejpeg($ground_im,$groundImage);break;
                        case 3:imagepng($ground_im,$groundImage);break;
                        default:die($errorMsg);
                }
                //释放内存
                if(isset($water_info)) unset($water_info);
                if(isset($water_im)) imagedestroy($water_im);
                unset($ground_info);
                imagedestroy($ground_im);
      }

      /**
         * 上传图片
         *
         */
      function water(){
                $this->load->view("addwater");
      }

      /**
         * 添加水印(test)
         *
         */
      function testWater(){
                $id=count($_FILES['userfile']);
                $num = count($_FILES['userfile']['name']);
                print_r($_FILES['userfile']);
                print_r($_FILES['userfile']['name']);
                echo $num;
                echo "<br>";
                if(isset($id)){
                        for($i=0;$i<$id;$i++){
                              if(isset($_FILES) && !empty($_FILES['userfile']) && $_FILES['userfile']['size']>0)
                              {
                                        $uploadfile ="images/".time()."_".$_FILES['userfile']['name'][$i];
                                        echo "<br>";
                                        echo $_FILES['userfile']['name'][$i].'<br>';
                                        echo $uploadfile.'<br>';
                                        echo $_FILES['userfile']['tmp_name'][$i];
                                        //fopen($uploadfile,"wb");
                                        if (move_uploaded_file($_FILES['userfile']['tmp_name'][$i], $uploadfile))
                                        {
                                                echo "OK<br>";
                                                //文字水印
                                                $this->imageWaterMark($uploadfile,5,"","HTTP://www.lvye.info",5,"#cccccc");
                                                //图片水印
                                                //$waterImage=base_url()."images/logo.gif";//水印图片路径
                                                //$this->imageWaterMark($uploadfile,9,$waterImage,"",5,"#cccccc");
                                                echo "<img src=\"".$uploadfile."\" border=\"0\">";
                                        }
                                        else
                                        {
                                                echo "Fail<br>";
                                        }
                              }
                        }
                }
      }

}
?>

heroicq 发表于 2008-9-28 11:37:37

view文件:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> PHP 水印 </title>
</head>
<body>
<form enctype="multipart/form-data" method="POST" action="testWater">
      <?php
      $id = 1;
      for($a=0; $a<$id; $a++){
                echo "文件: <input name=\"userfile[]\" type=\"file\"><br>";
      }
      ?>
      <input type="submit" value="上传">
</form>
</body>
</html>

Jack 发表于 2008-9-28 12:59:41

原帖由 heroicq 于 2008-9-28 11:32 发表 http://codeigniter.org.cn/forums/images/common/back.gif
帮忙看看怎么老是爆错,imagettfbbox() : Invalid font filename

Message: Undefined offset: 1

Filename: controllers/waterImage.php

Line Number: 194

都没有offset,怎么回事? ...

看到这个 Invalid font filename 没有,是说你的字体文件无效,或者路径不对,你自己检查一下先.

heroicq 发表于 2008-9-28 13:43:18

我把字体放在system/fonts下,应该没错,有默认CI自带的,有从windows下复制过来的,都是 Invalid font filename ,这样写 "./arial.ttf",   ".arial.ttf","arial.ttf",都不行。
offset也不知道哪出错了

Hex 发表于 2008-9-28 15:16:55

waterImage.php 194 是什么内容?

heroicq 发表于 2008-9-28 16:24:44

就是在本来页面中的194行啊,反正找不到offset就是了,不知道哪错了

Hex 发表于 2008-9-28 17:07:26

你还是得检查 Invalid font filename
这肯定是你的服务器的问题,重装 PHP

dogwin 发表于 2009-9-13 07:30:41

:Q找不到有用的呢。。郁闷ING

visvoy 发表于 2009-9-14 02:15:14

路径错误,./在CI里面是指index.php所在目录
要用./system/fonts/xxx.ttf
页: [1] 2
查看完整版本: 求助: PHP水印