喜悦国际村 
» 游客:  注册 | 登录 | 搜索 | 统计 | 喜悦证交所 | 帮助

RSS 订阅当前论坛  

$5.95 Web Hosting     

上一主题 下一主题
     
标题: [问题] 求PHP图片水印的简单例子  
 
dayrui
新手上路
Rank: 1



UID 84015
精华 0
积分 19
帖子 9
金钱 19 喜悦币
威望 0
人脉 0
阅读权限 10
注册 2006-11-29
状态 离线
[广告]: Enom域名自助付费 自助注册 自助PUSH 主流域名COM等一律57.99元年
求PHP图片水印的简单例子

要简单的 不要太复杂了
2008-6-24 05:28 PM#1
查看资料  发短消息  顶部
 
开花石头 (南冥洗)
版主
Rank: 7Rank: 7Rank: 7
努力把内裤穿在外面


UID 7949
精华 12
积分 25623
帖子 4518
金钱 25503 喜悦币
威望 120
人脉 0
阅读权限 100
注册 2001-4-19
状态 离线
[广告]: 代充Paypal帐号美元


<?php

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


    /**
    * <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->targetFile100 );
        
        
    }
    
    
/**
    * 设置图片水印
    * @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;
    }
}

?>




2008-6-24 06:12 PM#2
查看资料  访问主页  Blog  发短消息  QQ  ICQ 状态  顶部
 
dayrui
新手上路
Rank: 1



UID 84015
精华 0
积分 19
帖子 9
金钱 19 喜悦币
威望 0
人脉 0
阅读权限 10
注册 2006-11-29
状态 离线
[推荐阅读] utf-8下简体繁体转换的思考与困惑
收藏了 谢谢
2008-6-24 06:23 PM#3
查看资料  发短消息  顶部
 
niexa123
注册会员
Rank: 2
中级会员



UID 50950
精华 0
积分 152
帖子 259
金钱 152 喜悦币
威望 0
人脉 0
阅读权限 20
注册 2004-11-18
状态 离线
[推荐阅读] 请问,再次打开当前页面时,如何保留原有数据?
不错,收下



http://www.ml188.org
2008-6-24 08:32 PM#4
查看资料  访问主页  Blog  发短消息  QQ  顶部
     


  可打印版本 | 推荐给朋友 | 订阅主题 | 收藏主题 | 开通个人空间  


 




Powered by Discuz! 6.1.0  © 2001-2010 Comsenz Inc.
Processed in 0.041871 second(s), 6 queries

(冀ICP备05009913号) 管理员:sadly 邮箱/MSN: sadly@phpx.com QQ:824008(长隐) 清除 Cookies - - Archiver - WAP