PHP图片上传加水印




<?php

D+t"g-V$iR;ujbj0 
]J%QQ:tc6W;pgf0
/******************************************************************************PHPChina 开源社区门户o"]+r"O�F?J
 PHPChina 开源社区门户*OFRT_*T,/
参数说明:
Dj;xVDE9a8i�z w)d0$max_file_size  : 上传文件大小限制, 单位BYTE
/yt.S],K+bE0$destination_folder : 上传文件路径PHPChina 开源社区门户r x2dwx*/ j0y
$watermark   : 是否附加水印(1为加水印,其他为不加水印);
5cQ2mS;i'y(V0 
6m$j(L%je%vP L0******************************************************************************/
PHPChina 开源社区门户tXpR"]W3N e nm/t(?
 PHPChina 开源社区门户b)q6|1rG*X*mo v
//上传文件类型列表 PHPChina 开源社区门户#l@}A%[!b
 PHPChina 开源社区门户-r"]�lG8Gy5V
$uptypes = array ( PHPChina 开源社区门户;?F}Z+@a
    
' image/jpg ' , PHPChina 开源社区门户�^U3BRy
    
' image/jpeg ' ,PHPChina 开源社区门户pRv,gh^_
    
' image/png ' ,
z.zgH�_P0    
' image/pjpeg ' ,
;Nr,Q1[WV0    
' image/gif ' ,
(W~Q HB(z+R?0R(N(_"`2n0    
' image/bmp ' ,
M&us.W&L0    
' image/x-png ' PHPChina 开源社区门户,}7VxHN'V
) ;PHPChina 开源社区门户Q5R2wA*G+RVb Z7a;b,N_
 PHPChina 开源社区门户oEG;e{iY~
 
(u2aKL)t J"X0
$max_file_size = 2000000 ;      //上传文件大小限制, 单位BYTE
6b1P*Cr2|R!]jc8k0
$destination_folder = " uploadimg/ " ; //上传文件路径
Fg3l |$r/T0
$watermark = 1 ;      //是否附加水印(1为加水印,其他为不加水印);
` ZH9|6I;W|1@0
$watertype = 1 ;      //水印类型(1为文字,2为图片)
`{$c"b)]d!k$M0
$waterposition = 1 ;      //水印位置(1为左下角,2为右下角,3为左上角,4为右上角,5为居中); PHPChina 开源社区门户%ah]4j)c$[R#R W
$waterstring = " http://www.xplore.cn/ " //水印字符串 PHPChina 开源社区门户"Ox7d7R/B:W*tA8^,SR
$waterimg = " xplore.gif " ;    //水印图片 PHPChina 开源社区门户)c-Uy@(Z.aw wT
$imgpreview = 1 ;      //是否生成预览图(1为生成,其他为不生成); PHPChina 开源社区门户A ?W1x(I:SHw"T
$imgpreviewsize = 1 / 2 ;    //缩略图比例 PHPChina 开源社区门户X0ftss0kAz] b
 PHPChina 开源社区门户t r*Sz+dewNcd"M
?>
D[ Py*]?0 PHPChina 开源社区门户2A.u i u?Ot ` tuh
<html>
`d3i5txn4`x0<head>PHPChina 开源社区门户-|h-dm6V6d&t1} _$B
<title>ZwelL图片上传程序</title>PHPChina 开源社区门户0`3u a-d8S4NID2?
<style type="text/css">
~ d_"{_V(w0<!--PHPChina 开源社区门户:x4w#G"w.~ dC
body
#U8[#P1@8wH0{ PHPChina 开源社区门户qr jL5a~1mp
     font-size: 9pt;PHPChina 开源社区门户`TX#P3Ao.@k
}PHPChina 开源社区门户4X6Z6N2VC|/
inputPHPChina 开源社区门户x_D9M"N-Pd'z b
{
0{y&QB&B*Ur)m I0     background-color: #66CCFF;PHPChina 开源社区门户;h)X$l?'Z0F'zO
     border: 1px inset #CCCCCC;
!cI*oZle*Y3AZu0}PHPChina 开源社区门户@&O X)/:p)~+n
-->
,Nx,|uN0</style>
QR9n!~|w0</head>PHPChina 开源社区门户.{7uPq9Z d+t9il
<body>
-q3X.DH+}b~,j0<form enctype="multipart/form-data" method="post" name="upform">
Ut2d(}{#f2c&a7]0  上传文件:
!u-j/o"H1m{!Ve~_0  <input name="upfile" type="file">
_'m;{,_ b `�ws k0  <input type="submit" value="上传"><br>PHPChina 开源社区门户-/H:t$Mvp
  允许上传的文件类型为:
<?= implode ( ' , ' , $uptypes ) ?>
3Gn8Ap;[7Dkc6Y p%s0</form>
v,/t)zO.@/0
<?php PHPChina 开源社区门户?CkN||'fV!_
if ( $_SERVER [ ' REQUEST_METHOD ' ] == ' POST ' ) PHPChina 开源社区门户E�_[,q O9s ?C H
{ PHPChina 开源社区门户 U3d7fp{,W/OO�u
    
if ( ! is_uploaded_file ( $_FILES [ " upfile " ][ tmp_name ]))
8k5Z,~2`&wM#|8z%m0    
//是否存在文件
D3t;gNhz7i"y7m0    
{
@ DC&[-Bc WF0        
echo " 图片不存在! " ;PHPChina 开源社区门户0wx�em)B'h7Mz%j
        
exit ;
4@y:Y7W5`G$EUJ:D0    
}
c*Ts;oVu7F0    
$file = $_FILES [ " upfile " ] ;
+S2oZ$Dw0 
D,m@Nh0    
if ( $max_file_size < $file [ " size " ]) PHPChina 开源社区门户@3o&d0K,^/GYwE+[6X+q
    
//检查文件大小
JKx0XH_0    
{
C:Kl"]q]1{-ys0        
echo " 文件太大! " ;PHPChina 开源社区门户[ L!B4PXIVnx j ~
        
exit ;PHPChina 开源社区门户 @(Uz"Yl)M�V
    
} PHPChina 开源社区门户E(Rd!~.p&/3o6/K
 PHPChina 开源社区门户4|{ x*m`:I4xY9I
    
if ( ! in_array ( $file [ " type " ] , $uptypes ))
7f Z{Q(NHp0    
//检查文件类型 PHPChina 开源社区门户,Kq^cQC
    
{
'RzcW$U5qp0        
echo " 文件类型不符! " . $file [ " type " ] ;
2Oc+H]0K0        
exit ;PHPChina 开源社区门户+_Pf({m*_0`;{cQ
    
}
wX6C*B1}})h8V0 PHPChina 开源社区门户Ou#wq0m `
    
if ( ! file_exists ( $destination_folder )) PHPChina 开源社区门户�UO�j�nsm3Y
    
{
O/NnN|G0        
mkdir ( $destination_folder ) ;
P[&Fe)zvC*O0    
}
D$d9h{&G_y /0 
6t2]S"i)d4d/j2@s0    
$filename = $file [ " tmp_name " ] ;
xwI6^6WK+t II]0    
$image_size = getimagesize ( $filename ) ;
2J]1e)C1lR0    
$pinfo = pathinfo ( $file [ " name " ]) ;PHPChina 开源社区门户"/ D`!dQ:F
    
$ftype = $pinfo [ ' extension ' ] ;PHPChina 开源社区门户@N�A5tF3m/k
    
$destination = $destination_folder . time () . " . " . $ftype ;PHPChina 开源社区门户f,th8z#W Y3r
 PHPChina 开源社区门户MvJ2R1|&N
    
if ( file_exists ( $destination ) && $overwrite != true ) PHPChina 开源社区门户/t ok3`6dC}(CF
    
{ PHPChina 开源社区门户s0@Q,rI
        
echo " 同名文件已经存在了 " ;PHPChina 开源社区门户0? mi5T.l)j'R$`z"R
        
exit ;
6B9A3d3J yT3fv0    
} PHPChina 开源社区门户@$m8t]Tr
 PHPChina 开源社区门户 OUo4oB
    
if ( ! move_uploaded_file ( $filename , $destination )) PHPChina 开源社区门户N2SXm9Q.j3J
    
{
9M(iBa/Xz_0        
echo " 移动文件出错 " ;
*oVj0_s0        
exit ;PHPChina 开源社区门户[;R8~H q*[v
    
}
f1?.k-[o0 PHPChina 开源社区门户 ^8|5ym(s2n q
    
$pinfo = pathinfo ( $destination ) ;
E4[DSv0    
$fname = $pinfo [ basename ] ;
X1IO,A$A&D0 PHPChina 开源社区门户$b�|ovHg$K/l
    
echo " < font color=red>已经成功上传</font><br>文件名:  <font color= blue> " . $destination_folder . $fname . " </font><br> " ;
o"k m�f/ay1lV0    
echo " 宽度: " . $image_size [ 0 ] ;
vQ-I^4M3?'e+w-I0    
echo " 长度: " . $image_size [ 1 ] ;PHPChina 开源社区门户n�O-S/:ovQsak
    
echo " <br> 大小: " . $file [ " size " ] . " bytes " ;PHPChina 开源社区门户{"~ma,]pt6n0m
 PHPChina 开源社区门户;U�z&`(}6a [
    
if ( $watermark == 1 )
|I:g1f$jqL8h0    
{
3^ u2E^$RlS/D"I0        
$iinfo = getimagesize ( $destination , $iinfo ) ;
eI!G zP.a)w4_0        
$nimage = imagecreatetruecolor ( $image_size [ 0 ] , $image_size [ 1 ]) ;PHPChina 开源社区门户V:k?&AeJDD
        
$white = imagecolorallocate ( $nimage , 255 , 255 , 255 ) ;
0Y2c^&O*M.@0        
$black = imagecolorallocate ( $nimage , 0 , 0 , 0 ) ;PHPChina 开源社区门户:CF5Y8t*`'~
        
$red = imagecolorallocate ( $nimage , 255 , 0 , 0 ) ;
`Z3q%~i@3B p0        
imagefill ( $nimage , 0 , 0 , $white ) ;PHPChina 开源社区门户A)N-c7xYB:dU
        
switch ( $iinfo [ 2 ]) PHPChina 开源社区门户-W*[I }5J Q Kx/I!G
        
{ PHPChina 开源社区门户w2hM6/my
            
case 1 :
#K9SE| |&Y/m6}0            
$simage = imagecreatefromgif ( $destination ) ;
A&KQO,e+Q(T0            
break ;PHPChina 开源社区门户)OW6xF[Z�ysc!y
 PHPChina 开源社区门户xR/#/'E^M e
            
case 2 :PHPChina 开源社区门户:S'iK#m!x1@-C
            
$simage = imagecreatefromjpeg ( $destination ) ;PHPChina 开源社区门户.v&J~;~1/�m
            
break ;
N)_y9y&A'r&Z5]c!H0 PHPChina 开源社区门户9s/S�{L%SHi5c
            
case 3 :PHPChina 开源社区门户Q-J~Ba]
            
$simage = imagecreatefrompng ( $destination ) ;PHPChina 开源社区门户*/(nk9{(/s&S]5N
            
break ;
*R h's�H~EG6@7@0 
j+Sw:@dD%C0            
case 6 :PHPChina 开源社区门户1wN.G8z a}Pd;d
            
$simage = imagecreatefromwbmp ( $destination ) ;
!Ly,V0j3Mu)]2p0            
break ;PHPChina 开源社区门户;u-p cy-ei7J _
 
Ku.T.d'Au0            
default :PHPChina 开源社区门户Gr%n+x7R$q[:~
            
die ( " 不支持的文件类型 " ) ;PHPChina 开源社区门户q Ug'y"}1p
            
exit ;PHPChina 开源社区门户B&?z�PkM%Q s
        
} PHPChina 开源社区门户/JO&l?8N2p9u;z'O
 PHPChina 开源社区门户3Z!x*k,ll
        
imagecopy ( $nimage , $simage , 0 , 0 , 0 , 0 , $image_size [ 0 ] , $image_size [ 1 ]) ;PHPChina 开源社区门户j]pw,u6U(R J
        
imagefilledrectangle ( $nimage , 1 , $image_size [ 1 ] - 15 , 80 , $image_size [ 1 ] , $white ) ;PHPChina 开源社区门户uUs7JdR�@
 
.b]2Jt']3F/mcb"U0        
switch ( $watertype )
r B Y6fB*M&S0        
{
ks9I_'U,PK0            
case 1 :    //加水印字符串 PHPChina 开源社区门户?-Rcp4j
            
imagestring ( $nimage , 2 , 3 , $image_size [ 1 ] - 15 , $waterstring , $black ) ;
f7l(E} P+_0a c'S1C(C0            
break ;PHPChina 开源社区门户"dLx)RD-U j[
 
9h1y c#Le9i/0            
case 2 :    //加水印图片
|2M.je,f9W @7|0            
$simage1 = imagecreatefromgif ( " xplore.gif " ) ;
*t&}(G,L:E'_0            
imagecopy ( $nimage , $simage1 , 0 , 0 , 0 , 0 , 85 , 15 ) ;PHPChina 开源社区门户#A,]/Rv?4G,gp
            
imagedestroy ( $simage1 ) ;
x/ KI*t9e0            
break ;
.{qH$eZ0        
}
Xg c%/x0O}?0 PHPChina 开源社区门户 m9J�tT5k6I�]C7@$|
        
switch ( $iinfo [ 2 ])
7F;H$Bj7l-q5I X F0        
{ PHPChina 开源社区门户a'xH2s$[8f_ Gc
            
case 1 :
N3j)c+q�Z"|N4Y�o-k0            
//imagegif($nimage, $destination);
z#}n7Q:Pr0            
imagejpeg ( $nimage , $destination ) ;
1^KA?�zJ0            
break ;
Br(V!@I#X%}0 
/e+C5rZ%grJ3q'z&z0            
case 2 :PHPChina 开源社区门户,i'OF9Y(IB V
            
imagejpeg ( $nimage , $destination ) ;PHPChina 开源社区门户wb:l ci:P
            
break ;PHPChina 开源社区门户U(Q./L/OFE
 PHPChina 开源社区门户+s Ktj`)e L
            
case 3 :
0}/q,Np!hX:_1Q0            
imagepng ( $nimage , $destination ) ;PHPChina 开源社区门户3@�GK)j!K W Y
            
break ;
ZJQrV%q0 
n$A"Pv]f0            
case 6 :PHPChina 开源社区门户�u0z]$H%t-Qw:W)z
            
imagewbmp ( $nimage , $destination ) ;
~Ny'Y/_8EeX5b0            
//imagejpeg($nimage, $destination);
[^"^,hY4zsg0Q8b0            
break ;PHPChina 开源社区门户!/ver-xt
        
} PHPChina 开源社区门户5P1j;e]3v(xy
 PHPChina 开源社区门户&T!_1@KJq�a
        
//覆盖原上传文件
IBG'} S"VE#Q0        
imagedestroy ( $nimage ) ;
xnd5l O$t{0        
imagedestroy ( $simage ) ;
1b _;w d%z:CH0    
} PHPChina 开源社区门户$dSNu5r
 
/ZRe0/!D0    
if ( $imgpreview == 1 )
f"Zf3Tg VJL0    
{
'k7Z$L#cMY3R q:`0        
echo " <br>图片预览:<br> " ;PHPChina 开源社区门户co5dh$p"Q,{7wg^
        
echo " <img src= /" " . $destination . " /" width= " . ( $image_size [ 0 ] * $imgpreviewsize ) . " height= " . ( $image_size [ 1 ] * $imgpreviewsize ) ;PHPChina 开源社区门户M3CU"wb/%Fmr
        
echo " alt= /" 图片预览: /r 文件名: " . $destination . " /r 上传时间: /" > " ;PHPChina 开源社区门户+fX3v$r)} J�M
    
} PHPChina 开源社区门户4x9@z.g8[9{.R&SA
 
.Zo a R!dDte ZAx R0
}
5W y6d%f ?F0 
g6k6L9p3Y2fu)g0
?> PHPChina 开源社区门户�]P/S-Wt@rZw
</body>
%I6qv:_J }2^0</html>
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值