+ -
当前位置:首页 → 问答吧 → php显示时图片加水印(转)

php显示时图片加水印(转)

时间:2008-10-30

来源:互联网

[php]
<?php

    /*
     * @(#)Breviary.class.php    1.0 05/08/25
     *
     *Copyright 2005-2008 www.8y8u.com.cn
     */


    /**
    * <b>图片缩略图的生成</b><br>
    * @author    Kevin
    * @version    1.1 08/05/19
    * @since       php 5.2.5 mysql 5.0.45
    */

      
class Breviary {


    /**
    * 源图路径
    */    
    public $sourceFile;
    
    /**
    * 目标图路径
    */
    public $targetFile;
    
    /**
    * 目标图宽度
    */
    public $targetWidth = 200;
    
    /**
    * 目标图高度
    */
    public $targetHeight = 200;
    
    /**
    * 水印的图片路径
    */
    public $watemark;
    
    /**
    * 水印的图片位置
    */
    public $position = "left-top";
    
    /**
    * 水印的图片透明度
    */
    private $diaphaneity = 50;
    
    /**
    * 得到图像缩放比率
    * @param    sourceWidth        源图宽
    * @param    sourceHeight    源图高
    * @return    scaling            缩放比率
    */
    
    function getScaling( $sourceWidth, $sourceHeight )
    {
        if( ( $sourceWidth + $sourceHeight ) < ( $this->targetWidth + $this->targetHeight ) ){
            return 1;
        }
        
        $widthScaling = $this->targetWidth / $sourceWidth;
        $heightScaling = $this->targetHeight / $sourceHeight;
        
        $scaling = ( $widthScaling < $heightScaling ) ? $widthScaling : $heightScaling;
                
        return $scaling;
    }
    
    
    /**
    * 生成图片
    * @return    void
    */
    function buildImage ()
    {
        //读取源文件信息
        $sourceInfo = getimagesize( $this->sourceFile );
        
        //计算缩放比例
        $scaling = $this->getScaling( $sourceInfo[0], $sourceInfo[1] );
        $targetWidth = $sourceInfo[0] * $scaling;
        $targetHeight = $sourceInfo[1] * $scaling;
        
        //生成真彩目标图
        $target = imagecreatetruecolor( $targetWidth, $targetHeight );

        //得到源图内容
        switch( $sourceInfo[2] ){
            
            case 1:
                $source = imagecreatefromgif( $this->sourceFile );
            break;
            
            case 2:
                $source = imagecreatefromjpeg( $this->sourceFile );
            break;
            
            case 3:
                $source = imagecreatefrompng( $this->sourceFile );
            break;
            
        }
        
        //将源图复制到目标图
        imagecopyresampled(
                            $target,
                            $source,
                            0,
                            0,
                            0,
                            0,
                            $targetWidth,
                            $targetHeight,
                            $sourceInfo[0],
                            $sourceInfo[1]
                        );
                        
        if( !empty( $this->watemark ) && file_exists( $this->watemark ) ){
            $target = $this->builderWatemark( $target, $targetWidth, $targetHeight );
        }
        
        //保存目标图
        imagejpeg( $target, $this->targetFile, 100 );
        
        
    }
    
    /**
    * 设置图片水印
    * @return    void
    */
    function builderWatemark( &$target, $targetWidth, $targetHeight ){
        
        $sourceInfo = getimagesize( $this->watemark );
        
        if( ( $sourceInfo[0] + $sourceInfo[1] ) > ( $targetWidth + $targetHeight ) ){
            return false;
        }
        
        switch( $sourceInfo[2] ){
            
            case 1:
                $source = imagecreatefromgif( $this->watemark );
            break;
            
            case 2:
                $source = imagecreatefromjpeg( $this->watemark );
            break;
            
            case 3:
                $source = imagecreatefrompng( $this->watemark );
            break;
            
        }
        
        //计算方位
        switch( $this->position ){
            case "left-top":
                $x = $y = 0;
                break;
            case "left-down":
                $x = 0;
                $y = $targetHeight - $sourceInfo[1];
                break;
            case "right-top":
                $x = $targetWidth - $sourceInfo[0];
                $y = 0;
                break;
            case "right-down":
                $x = $targetWidth - $sourceInfo[0];
                $y = $targetHeight - $sourceInfo[1];
                break;
        }
        
        imagecopymerge(
                        $target,
                        $source,
                        $x,
                        $y,
                        0,
                        0,
                        $sourceInfo[0],
                        $sourceInfo[1],
                        $this->diaphaneity
                    );
                    return $target;
    }
    
    
    
    /**
    * 设置源图片路径
    * @return     void
    */
    function setSourceFile( $sf ){
        $this->sourceFile = $sf;
    }
    
    /**
    * 设置目标图路径
    * @return     void
    */
    function setTargetFile( $tf ){
        $this->targetFile = $tf;
    }
    
    /**
    * 设置目标图宽度
    * @return     void
    */
    function setTargetWidth( $tw ){
        $this->targetWidth = $tw;
    }
    
    /**
    * 设置目标图高度
    * @return     void
    */
    function setTargetHeight( $th ){
        $this->targetHeight = $th;
    }
    
    /**
    * 设置水印图片路径
    * @return     void
    */
    function setWatemark( $w ){
        $this->watemark = $w;
    }
    
    /**
    * 设置水印图片位置
    * @return     void
    */
    function setPosition( $p ){
        $this->position = $p;
    }
    
    /**
    * 设置水印图片透明度
    * @return     void
    */
    function setDiaphaneity( $d ){
        $this->diaphaneity = $d;
    }
}

?>
[/php]

作者: fdmk   发布时间: 2008-10-30

好贴,收藏起来,以后用!!!!!!!!!!!!!!!!!!!!!

作者: bitterain   发布时间: 2008-12-02

热门下载

更多