thinkphp给图片打水印不清晰

时间:2022-09-09 04:20:26

项目中打印条形码的函数,从thinkphp自带的water函数修改而来的。

贴上代码:

/**
  * water2
  * 改写thinkphp的water函数更强健的函数,增加了写入位置参数
    去掉了alpha设定参数,以修正打出的水印不清晰的bug
  * @param string $source 原图文件名
  * @param string $water 水印图片文件名
  * @param int $waterPos 要打水印的位置
  * @param string $savename 要保存的图片名,如果留空则用source
  * @return void
  */
function water2($source,$water,$waterPos=1,$savename=null)
{
    import('ORG.Util.Image');
    //检查文件是否存在
    if(!file_exists($source)||!file_exists($water))
        return false;

    //图片信息
    $sInfo=Image::getImageInfo($source);
    $wInfo=Image::getImageInfo($water);

    //如果图片小于水印图片,不生成图片
    if($sInfo["width"]<$wInfo["width"] || $sInfo['height']<$wInfo['height'])
        return false;

    //建立图像
    $sCreateFun="imagecreatefrom".$sInfo['type'];
    $sImage=$sCreateFun($source);
    $wCreateFun="imagecreatefrom".$wInfo['type'];
    $wImage=$wCreateFun($water);

    //设定图像的混色模式
   imagealphablending($wImage, true);

   switch($waterPos){
        case 0://随机
            $posX = rand(0,($sInfo["width"] - $wInfo["width"]));
            $posY = rand(0,($sInfo["height"] - $wInfo["height"]));
            break;
        case 1://1为顶端居左
            $posX = 8;
            $posY = 8;
            break;
        case 2://2为顶端居中
            $posX = ($sInfo["width"] - $wInfo["width"])/2;
            $posY = 0;
            break;
        case 3://3为顶端居右
            $posX = $sInfo["width"] - $wInfo["width"];
            $posY = 0;
            break;
        case 4://4为中部居左
            $posX = 0;
            $posY = ($sInfo["height"] - $wInfo["height"])/2;
            break;
        case 5://5为中部居中
            $posX = ($sInfo["width"] - $wInfo["width"])/2;
            $posY = ($sInfo["height"] - $wInfo["height"])/2;
            break;
        case 6://6为中部居右
            $posX = $sInfo["width"] - $wInfo["width"];
            $posY = ($sInfo["height"]  - $wInfo["height"])/2;
            break;
        case 7://7为底端居左
            $posX = 0;
            $posY = $sInfo["height"] - $wInfo["height"];
            break;
        case 8://8为底端居中
            $posX = ($sInfo["width"] - $wInfo["width"])/2;
            $posY = $sInfo["height"]- $wInfo["height"];
            break;
        case 9://9为底端居右
            $posX = $sInfo["width"] - $wInfo["width"]-8;
            $posY = $sInfo["height"] - $wInfo["height"]-8;
            break;
        default://随机
            $posX = rand(0,($sInfo["width"] - $wInfo["width"]));
            $posY = rand(0,($sInfo["height"] - $wInfo["height"]));
            break;
     }

    //生成混合图像
     //imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo['width'],$wInfo['height'],$alpha);
     imagecopy($sImage, $wImage,$posX, $posY, 0, 0,$wInfo["width"],$wInfo["height"]);

    //输出图像
    $ImageFun='Image'.$sInfo['type'];
    //如果没有给出保存文件名,默认为原图像名
    if(!$savename){
        $savename=$source;
        @unlink($source);
    }
    //保存图像
    $ImageFun($sImage,$savename);
    imagedestroy($sImage);
}

thinkphp中Image.class.php的getImageInfo方法:

    /**
     +----------------------------------------------------------
     * 取得图像信息
     *
     +----------------------------------------------------------
     * @static
     * @access public
     +----------------------------------------------------------
     * @param string $image 图像文件名
     +----------------------------------------------------------
     * @return mixed
     +----------------------------------------------------------
     */
    static function getImageInfo($img) {
        $imageInfo = getimagesize($img);
        if( $imageInfo!== false) {
            if(function_exists(image_type_to_extension)){
                $imageType = strtolower(substr(image_type_to_extension($imageInfo[2]),1));
            }else{
                $imageType = strtolower(substr($img,strrpos($img,'.')+1));
            }
            $imageSize = filesize($img);
            $info = array(
                "width"=>$imageInfo[0],
                "height"=>$imageInfo[1],
                "type"=>$imageType,
                "size"=>$imageSize,
                "mime"=>$imageInfo['mime']
            );
            return $info;
        }else {
            return false;
        }
    }

thinkphp中的water函数:

  /**
     +----------------------------------------------------------
     * 为图片添加水印
     +----------------------------------------------------------
     * @static public
     +----------------------------------------------------------
     * @param string $source 原文件名
     * @param string $water  水印图片
     * @param string $$savename  添加水印后的图片名
     * @param string $alpha  水印的透明度
     +----------------------------------------------------------
     * @return string
     +----------------------------------------------------------
     * @throws ThinkExecption
     +----------------------------------------------------------
     */

    static public function water($source,$water,$savename=null,$alpha=80)
    {
        //检查文件是否存在
        if(!file_exists($source)||!file_exists($water))
            return false;

        //图片信息
        $sInfo=self::getImageInfo($source);
        $wInfo=self::getImageInfo($water);

        //如果图片小于水印图片,不生成图片
        if($sInfo["width"]<$wInfo["width"] || $sInfo['height']<$wInfo['height'])
            return false;

        //建立图像
        $sCreateFun="imagecreatefrom".$sInfo['type'];
        $sImage=$sCreateFun($source);
        $wCreateFun="imagecreatefrom".$wInfo['type'];
        $wImage=$wCreateFun($water);

        //设定图像的混色模式
        imagealphablending($wImage, true);

        //图像位置,默认为右下角右对齐
        $posY=$sInfo["height"]-$wInfo["height"];
        $posX=$sInfo["width"]-$wInfo["width"];

        //生成混合图像
         imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo['width'],$wInfo['height'],$alpha);

        //输出图像
        $ImageFun='Image'.$sInfo['type'];
        //如果没有给出保存文件名,默认为原图像名
        if(!$savename){
            $savename=$source;
            @unlink($source);
        }
        //保存图像
        $ImageFun($sImage,$savename);
        imagedestroy($sImage);

    }