标题:
php给上传图片加水印图案
[打印本页]
作者:
php
时间:
2007-7-28 23:08
标题:
php给上传图片加水印图案
<
form
action
=
"<?=$_SERVER['PHP_SELF']?>"
method
=
"post"
enctype
=
"multipart/form-data"
name
=
"form1"
id
=
"form1"
>
<
table
width
=
"486"
border
=
"0"
align
=
"center"
cellpadding
=
"0"
cellspacing
=
"0"
>
<
tr
>
<
td
width
=
"498"
height
=
"28"
background
=
"image/top.gif"
>
<
div
align
=
"center"
class
=
"style2"
>
上传图片
<
/
div
>
<
/
td
>
<
/
tr
>
<
tr
>
<
td
>
&
nbsp
;
<
/
td
>
<
/
tr
>
<
tr
>
<
td
>
<
div
align
=
"right"
>
<
table
width
=
"426"
border
=
"0"
align
=
"center"
cellpadding
=
"0"
cellspacing
=
"0"
>
<
tr
>
<
td
width
=
"50"
height
=
"32"
nowrap
=
"nowrap"
>
路径:
<
/
td
>
<
td
width
=
"362"
>
<
input
name
=
"upfile"
type
=
"file"
id
=
"upfile"
size
=
"40"
/
>
<
/
td
>
<
/
tr
>
<
tr
>
<
td
height
=
"27"
colspan
=
"2"
>
<
div
align
=
"center"
>
<
input
type
=
"submit"
name
=
"Submit"
value
=
"提交"
/
>
&
nbsp
;
&
nbsp
;
<
input
type
=
"reset"
name
=
"Submit"
value
=
"重置"
/
>
<
/
div
>
<
/
td
>
<
/
tr
>
<
/
table
>
<
/
div
>
<
div
align
=
"center"
>
<
/
div
>
<
/
td
>
<
/
tr
>
<
tr
>
<
td
>
&
nbsp
;
<
/
td
>
<
/
tr
>
<
INPUT
TYPE
=
"hidden"
name
=
'action'
value
=
'doup'
>
<
tr
>
<
td
height
=
"28"
background
=
"image/top.gif"
>
&
nbsp
;
<
/
td
>
<
/
tr
>
<
tr
>
<
td
>
&
nbsp
;
<
/
td
>
<
/
tr
>
<
/
table
>
<
/
form
>
<
br
>
<
A
href
=
http
:
/
/
phpshao
.
cublog
.
cn
target
=
_blank
>
龙的心专栏phpshao
.
cublog
.
cn
<
/
A
>
<
?
if
(
$
_POST
[
'action'
]
=
=
'doup'
)
{
$
uptypes
=
array
(
'image/jpg'
,
'image/jpeg'
,
'image/pjpeg'
,
'image/gif'
)
;
//上传图片文件类型列表
$
wFile
=
$
_FILES
[
'upfile'
]
;
//取得文件路径
$
waterimg
=
"water.gif"
;
//水印图片路径
//print_r($wFile);
if
(
in_array
(
$
wFile
[
'type'
]
,
$
uptypes
)
)
{
//检查文件类型,若上传的文件为jpg或gif图片则加水印
if
(
strstr
(
$
wFile
[
'type'
]
,
"jp"
)
)
{
//若上传图片类型为jpg,pjpeg,jpeg,则用imagecreatefromjpeg读取目标文件
$
im
=
imageCreatefromjpeg
(
$
wFile
[
'tmp_name'
]
)
;
$
wfilew
=
imagesx
(
$
im
)
;
//取得图片的宽
$
wfileh
=
imagesy
(
$
im
)
;
//取得图片的高
}
else
{
//否则若上传图片类型为gif,则用imagecreatefromgif读取目标文件
$
im
=
imageCreatefromgif
(
$
wFile
[
'tmp_name'
]
)
;
$
wfilew
=
imagesx
(
$
im
)
;
//取得图片的宽
$
wfileh
=
imagesy
(
$
im
)
;
//取得图片的高
}
//设定混合模式
imagealphablending
(
$
im
,
true
)
;
//读取水印文件
$
im2
=
imagecreatefrompng
(
$
waterimg
)
;
//若水印图片为jpg,则此去可改为$im2 = imagecreatefromjpeg($waterimg)
//$white = imagecolorallocate($im2, 255, 255, 255);
// imagecolortransparent($im2,$white); //设透明色,这两句可不要
$
waterw
=
imagesx
(
$
im2
)
;
//取得水印图片的宽
$
waterh
=
imagesy
(
$
im2
)
;
//取得水印图片的高
//随机放水印到图片中
$
randval
=
rand
(
0
,
9
)
;
//在0-9之间产生随机数
if
(
$
randval
=
=
0
|
|
$
randval
=
=
3
|
|
$
randval
=
=
2
|
|
$
randval
=
=
8
|
|
$
randval
=
=
7
)
{
//此处还可完善放更多位置
$
wimgx
=
5
;
$
wimgy
=
5
;
//放左上角
}
else
{
$
wimgx
=
$
wfilew
-
5
-
$
waterw
;
$
wimgy
=
$
wfileh
-
5
-
$
waterh
;
//放右上角
}
//拷贝水印到目标文件
imagecopy
(
$
im
,
$
im2
,
$
wimgx
,
$
wimgy
,
0
,
0
,
$
waterw
,
$
waterh
)
;
//输出图片
if
(
strstr
(
$
wFile
[
'type'
]
,
"jp"
)
)
{
//同上
imagejpeg
(
$
im
,
$
wFile
[
'tmp_name'
]
)
;
}
else
{
imagegif
(
$
im
,
$
wFile
[
'tmp_name'
]
)
;
}
imagedestroy
(
$
im
)
;
imagedestroy
(
$
im2
)
;
copy
(
$
wFile
[
'tmp_name'
]
,
$
wFile
[
'name'
]
)
;
//上传
}
else
echo
"图片不符合!!"
;
}
?
>
欢迎光临 编程开发论坛 (http://bbs.lihuasoft.net/)
Powered by Discuz! 6.0.0