项目中打印条形码的函数,从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);

    }

最新文章

  1. 【406错误】 The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request &quot;accept&quot; headers.
  2. Cocos2d-x lua 游戏中的文字和标签
  3. MyBaits一对一的查询方法
  4. MongoDB学习笔记一:入门
  5. 状态模式 java &amp;&amp; php
  6. 为Python添加默认模块搜索路径
  7. mouseenter和mouseout中间的时间控制
  8. 深入理解javacript之prototype
  9. coco2d学习day01 精灵分析
  10. Login failed for user &#39;NT AUTHORITY\ANONYMOUS LOGON
  11. powershell 生成随机用户信息
  12. redis的主从复制与哨兵
  13. DWR3.0框架入门(3) —— ScriptSession的维护及优化
  14. HTTP服务器的本质:tinyhttpd源码分析及拓展
  15. java排序算法(五):快速排序
  16. python算法之插入排序
  17. POJ 2245 Addition Chains(算竞进阶习题)
  18. weak_ptr_c++11
  19. canvas绘图基础
  20. C# DataTable.Compute()用法

热门文章

  1. PagerSlidingTabStrip
  2. CentOS开发环境LAMP搭建
  3. 各种会义PPT
  4. Memcached启动、关闭参数(摘录)
  5. Android自定义View之ProgressBar出场记
  6. arcgis10 安装1721错误
  7. Android_常用控件及适配器
  8. AutoInvoice in Oracle Apps R12
  9. 20151225jquery学习笔记---编辑器插件
  10. javaweb常用工具类及配置文件备份