<?<span>php
</span><span>
<span>session_start</span><span>();
</span><span>
if
</span>(<span>
$_POST
</span>[
'submit'
] ==
'Upload'
<span>)
{
</span><span>
if
</span>(<span>
$_FILES
</span>[
'uploadfile'
][
'error'
] !=<span> UPLOAD_ERR_OK)
{
</span><span>
switch
</span>(<span>
$_FILES
</span>[
'uploadfile'
][
'error'
<span>])
{
</span><span>
case
</span> UPLOAD_ERR_INI_SIZE: <span>
<span>
die
</span>(
'上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值'
<span>);
</span><span>
break
</span><span>;
</span><span>
case
</span> UPLOAD_ERR_FORM_SIZE: <span>
<span>
die
</span>(
'上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值'
<span>);
</span><span>
break
</span><span>;
</span><span>
case
</span> UPLOAD_ERR_PARTIAL: <span>
<span>
die
</span>(
'文件只有部分被上传'
<span>);
</span><span>
break
</span><span>;
</span><span>
case
</span> UPLOAD_ERR_NO_FILE: <span>
<span>
die
</span>(
'没有文件被上传'
<span>);
</span><span>
break
</span><span>;
</span><span>
case
</span> UPLOAD_ERR_NO_TMP_DIR: <span>
<span>
die
</span>(
'找不到临时文件夹'
<span>);
</span><span>
break
</span><span>;
</span><span>
case
</span> UPLOAD_ERR_CANT_WRITE: <span>
<span>
die
</span>(
'文件写入失败'
<span>);
</span><span>
break
</span><span>;
</span><span>
case
</span> UPLOAD_ERR_EXTENSION: <span>
<span>
die
</span>(
'其他异常'
<span>);
</span><span>
break
</span><span>;
}
}
</span><span>
<span>
switch
</span>(<span>
$type
</span><span>)
{
</span><span>
case
</span> IMAGETYPE_GIF:
<span>
$ext
</span> =
'.gif'
<span>;
</span><span>
break
</span><span>;
</span><span>
case
</span> IMAGETYPE_JPEG:
<span>
$ext
</span> =
'.jpg'
<span>;
</span><span>
break
</span><span>;
</span><span>
case
</span> IMAGETYPE_PNG:
<span>
$ext
</span> =
'.png'
<span>;
</span><span>
break
</span><span>;
</span><span>
default
</span> :
<span>
die
</span>(
'您上传的文件不支持的文件类型。'
<span>);
}
</span><span>
<span>
$_SESSION
</span>[
'ext'
] = <span>
$ext
</span><span>;
}
</span><span>
</span>
<span>
<span>
$coon
</span> = <span>mysql_connect</span>(
"localhost"
,
"root"
,
"lifu"
)
or
<span>
die
</span>(
'不能连接到数据库'
<span>);
</span><span>
if
</span> (!<span>
$coon
</span><span>)
{
</span><span>
die
</span>(
'无法连接: '
. <span>mysql_error</span><span>());
}
</span><span>
if
</span> (<span>mysql_query</span>(
"CREATE DATABASE if not exists moviesite"
,<span>
$coon
</span>)) <span>
<span> {
</span><span>
echo
</span>
"数据库创建成功<br>"
<span>;
}
</span><span>
else
</span><span>
{
</span><span>
echo
</span>
"数据库创建错误: "
. <span>mysql_error</span><span>();
}
</span><span>
<span>mysql_select_db</span>(
"moviesite"
, <span>
$coon
</span><span>);
</span><span>
<span>
$sql
</span> = "<span>CREATE TABLE
if
not exists images
(
image_id int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(image_id),
image_caption varchar(15),
image_username varchar(15),
image_filename varchar(15),
image_date datetime
)</span>"<span>;
</span><span>mysql_query</span>(<span>
$sql
</span>,<span>
$coon
</span><span>);
</span><span>mysql_close</span>(<span>
$coon
</span><span>);
</span><span>
<span>
$db
</span> = <span>mysql_connect</span>(
'localhost'
,
'root'
,
'lifu'
)
or
<span>
die
</span>(
'不能连接到数据库'
<span>);
</span><span>mysql_select_db</span>(
'moviesite'
,<span>
$db
</span>)
or
<span>
die
</span>(<span>mysql_error</span>(<span>
$db
</span><span>));
</span><span>
<span>
$dir
</span> =
'D:\phpStudy\WWW\loadimages\images'
<span>;
</span><span>
<span>
$thumbdir
</span> =
'D:\phpStudy\WWW\loadimages\images\thumbs'
<span>;
</span><span>
<span>putenv</span>(
'GDFONTPATH='
.
'C:\Windows\Fonts'
<span>);
</span><span>
$font
</span> =
"C:\Windows\Fonts\arial.ttf"
<span>;
</span><span>
<span>
if
</span>(<span>
$_POST
</span>[
'submit'
] ==
'Upload'
<span>)
{ </span><span>
</span>
<span>
$image_caption
</span> = <span>
$_POST
</span>[
'caption'
<span>];
</span><span>
$image_username
</span> = <span>
$_POST
</span>[
'username'
<span>];
</span><span>
$image_date
</span> = <span>
date
</span>(
'Y-m-D'
<span>);
</span><span>
</span>
<span>list</span>(<span>
$width
</span>,<span>
$height
</span>,<span>
$type
</span>,<span>
$attr
</span>) = <span>
getimagesize
</span>(<span>
$_FILES
</span>[
'uploadfile'
][
'tmp_name'
<span>]);
</span><span>
<span>
switch
</span>(<span>
$type
</span><span>)
{
</span><span>
case
</span> IMAGETYPE_GIF:
<span>
$image
</span> = imagecreatefromgif(<span>
$_FILES
</span>[
'uploadfile'
][
'tmp_name'
])
or
<span>
die
</span>(
'你上传的文件是不支持的文件类型'
<span>);
</span><span>
$ext
</span> =
'.gif'
<span>;
</span><span>
$array
</span> = <span>
getimagesize
</span>(<span>
$_FILES
</span>[
'uploadfile'
][
'tmp_name'
<span>]);
</span><span>
break
</span><span>;
</span><span>
case
</span> IMAGETYPE_JPEG:
<span>
$image
</span> = imagecreatefromjpeg(<span>
$_FILES
</span>[
'uploadfile'
][
'tmp_name'
])
or
<span>
die
</span>(
'你上传的文件是不支持的文件类型'
<span>);
</span><span>
$ext
</span> =
'.jpg'
<span>;
</span><span>
$array
</span> = <span>
getimagesize
</span>(<span>
$_FILES
</span>[
'uploadfile'
][
'tmp_name'
<span>]);
</span><span>
break
</span><span>;
</span><span>
case
</span> IMAGETYPE_PNG:
<span>
$image
</span> = imagecreatefrompng(<span>
$_FILES
</span>[
'uploadfile'
][
'tmp_name'
])
or
<span>
die
</span>(
'你上传的文件是不支持的文件类型'
<span>);
</span><span>
$ext
</span> =
'.png'
<span>;
</span><span>
$array
</span> = <span>
getimagesize
</span>(<span>
$_FILES
</span>[
'uploadfile'
][
'tmp_name'
<span>]);
</span><span>
break
</span><span>;
</span><span>
default
</span> :
<span>
die
</span>(
'您上传的文件不支持的文件类型。'
<span>);
}
</span><span>
<span>
$big
</span> = imagecreatetruecolor(1280, 960<span>);
</span><span>
imagecopyresampled(<span>
$big
</span>, <span>
$image
</span>, 0, 0, 0, 0, 1280, 960, <span>
$array
</span>[0], <span>
$array
</span>[1<span>]);
</span><span>
$query
</span> =
'insert into images(image_caption,image_username,image_date) values ("'
.<span>
$image_caption
</span>.
'","'
.<span>
$image_username
</span>.
'",now())'
<span>;
</span><span>mysql_query</span>(<span>
$query
</span> , <span>
$db
</span>)
or
<span>
die
</span>(<span>mysql_error</span>(<span>
$db
</span><span>));
</span><span>
$last_id
</span> = <span>mysql_insert_id</span><span>();
</span><span>
<span>
$imagename
</span> = <span>
$last_id
</span>.<span>
$ext
</span><span>;
</span><span>
$image_id
</span> = <span>
$last_id
</span><span>;
</span><span>
$query
</span> =
'update images set image_filename="'
.<span>
$imagename
</span>.
'" where image_id='
.<span>
$last_id
</span><span>;
</span><span>mysql_query</span>(<span>
$query
</span> , <span>
$db
</span>)
or
<span>
die
</span>(<span>mysql_error</span>(<span>
$db
</span><span>));
</span><span>
<span>
switch
</span>(<span>
$type
</span><span>)
{
</span><span>
case
</span> IMAGETYPE_GIF:<span>
imagegif(</span><span>
$big
</span>,<span>
$dir
</span>.
'/'
.<span>
$imagename
</span><span>);
</span><span>
break
</span><span>;
</span><span>
case
</span> IMAGETYPE_JPEG:<span>
imagejpeg(</span><span>
$big
</span>,<span>
$dir
</span>.
'/'
.<span>
$imagename
</span><span>);
</span><span>
break
</span><span>;
</span><span>
case
</span> IMAGETYPE_PNG:<span>
imagepng(</span><span>
$big
</span>,<span>
$dir
</span>.
'/'
.<span>
$imagename
</span><span>);
</span><span>
break
</span><span>;
}
</span><span>
imagedestroy(<span>
$big
</span><span>);
imagedestroy(</span><span>
$image
</span><span>);
}
</span><span>
else
</span> <span>
<span> {
</span><span>
$query
</span> =
'select image_id,image_caption,image_username,image_date from images where image_id='
.<span>
$_POST
</span>[
'id'
<span>];
</span><span>
$result
</span> = <span>mysql_query</span>(<span>
$query
</span>,<span>
$db
</span>)
or
<span>
die
</span>(<span>mysql_error</span>(<span>
$db
</span><span>));
</span><span>
<span>extract</span>(<span>mysql_fetch_assoc</span>(<span>
$result
</span><span>));
</span><span>list</span>(<span>
$width
</span>,<span>
$height
</span>,<span>
$type
</span>,<span>
$attr
</span>) = <span>
getimagesize
</span>(<span>
$dir
</span>.
'/'
.<span>
$image_id
</span>.<span>
$_SESSION
</span>[
'ext'
<span>]);
}
</span><span>
<span>
if
</span>(<span>
$_POST
</span>[
'submit'
] ==
'Save'
<span>)
{
</span><span>
if
</span>(<span>isset</span>(<span>
$_POST
</span>[
'id'
]) && <span>ctype_digit</span>(<span>
$_POST
</span>[
'id'
]) && <span>
file_exists
</span>(<span>
$dir
</span>.
'/'
.<span>
$_POST
</span>[
'id'
].<span>
$_SESSION
</span>[
'ext'
<span>]))
{
</span><span>
if
</span>(<span>
$_SESSION
</span>[
'ext'
] ==
'.jpg'
<span>)
{
</span><span>
$image
</span> = imagecreatefromjpeg(<span>
$dir
</span>.
'/'
.<span>
$_POST
</span>[
'id'
].<span>
$_SESSION
</span>[
'ext'
<span>]);
}
</span><span>
if
</span>(<span>
$_SESSION
</span>[
'ext'
] ==
'.png'
<span>)
{
</span><span>
$image
</span> = imagecreatefrompng(<span>
$dir
</span>.
'/'
.<span>
$_POST
</span>[
'id'
].<span>
$_SESSION
</span>[
'ext'
<span>]);
}
</span><span>
if
</span>(<span>
$_SESSION
</span>[
'ext'
] ==
'.gif'
<span>)
{
</span><span>
$image
</span> = imagecreatefromgif(<span>
$dir
</span>.
'/'
.<span>
$_POST
</span>[
'id'
].<span>
$_SESSION
</span>[
'ext'
<span>]);
}
}
</span><span>
else
</span><span>
{
</span><span>
die
</span>(
'指定的无效图像'
<span>);
}
</span><span>
$effect
</span> = (<span>isset</span>(<span>
$_POST
</span>[
'effect'
])) ? <span>
$_POST
</span>[
'effect'
] : -1<span>;
</span><span>
switch
</span>(<span>
$effect
</span><span>)
{
</span><span>
case
</span> IMG_FILTER_NEGATE:<span>
imagefilter(</span><span>
$image
</span> , IMG_FILTER_NEGATE); <span>
<span>
break
</span><span>;
</span><span>
case
</span> IMG_FILTER_GRAYSCALE:<span>
imagefilter(</span><span>
$image
</span> , IMG_FILTER_GRAYSCALE); <span>
<span>
break
</span><span>;
</span><span>
case
</span> IMG_FILTER_EMBOSS:<span>
imagefilter(</span><span>
$image
</span> , IMG_FILTER_EMBOSS); <span>
<span>
break
</span><span>;
</span><span>
case
</span> IMG_FILTER_GAUSSIAN_BLUR:<span>
imagefilter(</span><span>
$image
</span> , IMG_FILTER_GAUSSIAN_BLUR); <span>
<span>
break
</span><span>;
}
</span><span>
<span>
if
</span>(<span>isset</span>(<span>
$_POST
</span>[
'emb_caption'
<span>]))
{ </span><span>
</span><span>
imagettftext(</span><span>
$image
</span> , 12 , 0 , 20 , 20 , 0 , <span>
$font
</span> , <span>
$image_caption
</span><span>);
}
</span><span>
<span>
if
</span>(<span>isset</span>(<span>
$_POST
</span>[
'emb_logo'
<span>]))
{
</span><span>
<span>list</span>(<span>
$wmk_width
</span> , <span>
$wmk_height
</span>) = <span>
getimagesize
</span>(
'images/logo.png'
<span>);
</span><span>
<span>
$x
</span> = <span>
$width
</span>-<span>
$wmk_width
</span><span>;
</span><span>
$y
</span> = <span>
$height
</span>-<span>
$wmk_height
</span><span>;
</span><span>
$wmk
</span> = imagecreatefrompng(
'images/logo.png'
<span>);
</span><span>
<span>
</span><span>
imagecopymerge(</span><span>
$image
</span> , <span>
$wmk
</span> , <span>
$x
</span> , <span>
$y
</span> , 0 , 0 , <span>
$wmk_width
</span> , <span>
$wmk_height
</span> , 20<span>);
</span><span>
imagedestroy(<span>
$wmk
</span><span>);
}
</span><span>
<span>
$thumb_width
</span> = <span>
$width
</span> * 0.10<span>;
</span><span>
$thumb_height
</span> = <span>
$height
</span> * 0.10<span>;
</span><span>
<span>
$thumb
</span> = imagecreatetruecolor(<span>
$thumb_width
</span>, <span>
$thumb_height
</span><span>);
imagecopyresampled(</span><span>
$thumb
</span>, <span>
$image
</span>, 0, 0, 0, 0, <span>
$thumb_width
</span>, <span>
$thumb_height
</span>, <span>
$width
</span>, <span>
$height
</span><span>);
</span><span>
<span>
if
</span>(<span>
$_SESSION
</span>[
'ext'
] ==
'.jpg'
<span>)
{
imagejpeg(</span><span>
$thumb
</span>, <span>
$thumbdir
</span> .
'/'
. <span>
$_POST
</span>[
'id'
] . <span>
$_SESSION
</span>[
'ext'
], 100<span>);
}
</span><span>
if
</span>(<span>
$_SESSION
</span>[
'ext'
] ==
'.png'
<span>)
{</span>
imagepng(<span>
$thumb
</span>, <span>
$thumbdir
</span> .
'/'
. <span>
$_POST
</span>[
'id'
] . <span>
$_SESSION
</span>[
'ext'
<span>]);
}
</span><span>
if
</span>(<span>
$_SESSION
</span>[
'ext'
] ==
'.gif'
<span>)
{
imagegif(</span><span>
$thumb
</span>, <span>
$thumbdir
</span> .
'/'
. <span>
$_POST
</span>[
'id'
] . <span>
$_SESSION
</span>[
'ext'
<span>]);
}
</span><span>
echo
</span>
"修改好的缩略图:<img src='"
.
"images/thumbs/"
. <span alt=
"php随笔1-php图片处理,"
>
$_POST
</span>['id
'] . <span>$_SESSION</span>['
ext
']."'
alt=
''
/> <br />"<span>;
</span><span>
imagedestroy(<span>
$thumb
</span><span>);
</span><span>
<span>
if
</span>(<span>
$_SESSION
</span>[
'ext'
] ==
'.jpg'
<span>)
{
imagejpeg(</span><span>
$image
</span> , <span>
$dir
</span>.
'/'
.<span>
$_POST
</span>[
'id'
].<span>
$_SESSION
</span>[
'ext'
] , 100<span>);
}
</span><span>
if
</span>(<span>
$_SESSION
</span>[
'ext'
] ==
'.png'
<span>)
{
</span><span>
imagepng(<span>
$image
</span> , <span>
$dir
</span>.
'/'
.<span>
$_POST
</span>[
'id'
].<span>
$_SESSION
</span>[
'ext'
<span>]);
}
</span><span>
if
</span>(<span>
$_SESSION
</span>[
'ext'
] ==
'.gif'
<span>)
{
imagegif(</span><span>
$image
</span> , <span>
$dir
</span>.
'/'
.<span>
$_POST
</span>[
'id'
].<span>
$_SESSION
</span>[
'ext'
<span>]);
}
</span><span>
echo
</span>
"修改好的大图:<img src='"
.
"images/"
. <span alt=
"php随笔1-php图片处理,"
>
$_POST
</span>['id
'] . <span>$_SESSION</span>['
ext
']."'
alt=
''
/> <br />"<span>;
</span>?>
<html>
<head>
<title>你的图片在这!</title>
</head>
<body>
<h1 id=
"你的图片被保存"
>你的图片被保存!</h1>
<img src=
"images/<?php echo <span alt="
php随笔1-php图片处理,
" >$_POST</span>['id'];?>.jpg"
alt=
""
/>
</body>
</html>
<?<span>php
}
</span><span>
else
</span><span>
{
</span>?>
<html>
<head>
<title>你的图片在这!</title>
</head>
<body>
<p>这里是你上传到服务器上的图片:</p>
</body>
</html>
<?<span>php
</span><span>
if
</span>(<span>
$_POST
</span>[
'submit'
] ==
'Upload'
<span>)
{
</span><span>
$imagename
</span> =
'images/'
.<span>
$image_id
</span>.<span>
$_SESSION
</span>[
'ext'
<span>];
}
</span><span>
else
</span><span>
{
</span><span>
$imagename
</span> =
'image_effect.php?id='
.<span>
$image_id
</span>.
'&e='
.<span>
$_POST
</span>[
'effect'
<span>];
</span><span>
if
</span>(<span>isset</span>(<span>
$_POST
</span>[
'emb_caption'
<span>]))
{
</span><span>
$imagename
</span> .=
'&capt='
.<span>urlencode</span>(<span>
$image_caption
</span><span>);
}
</span><span>
if
</span>(<span>isset</span>(<span>
$_POST
</span>[
'emb_logo'
<span>]))
{
</span><span>
$imagename
</span> .=
'&logo=1'
<span>;
}
}
</span>?>
<div>
<?php <span>
echo
</span>
"src:"
.<span>
$imagename
</span> ; ?>
<img src=
"<?php echo <span alt="
php随笔1-php图片处理,
" >$imagename</span>;?>"
>
as
</span>:</td>
<td><?php <span>
echo
</span>
"moviesite数据库images表第"
.<span>
$image_id
</span> .
"行!"
;?></td>
</tr>
<tr>
<td>Height:</td>
<td><?php <span>
echo
</span> <span>
$height
</span>;?></td>
</tr>
<tr>
<td>Widht:</td>
<td><?php <span>
echo
</span> <span>
$width
</span>;?></td>
</tr>
<tr>
<td>Upload <span>
date
</span>:</td>
<td><?php <span>
echo
</span> <span>
$image_date
</span>;?></td>
</tr>
</table>
<p>你可以从下面的选项列表中对你的图像应用一个特殊的效果。注:使用任何一个过滤器,保存一个图像 <em>可以撤消</em></p>
<form action=
"<?php echo <span>$_SERVER</span>['PHP_SELF'];?>"
method=
"post"
>
<input type=
"hidden"
name=
"id"
value=
"<?php echo <span>$image_id</span>;?>"
/>
<select name=
"effect"
id=
""
>
<option value=
"-1"
>保持原状</option>
<?<span>php
</span><span>
echo
</span>
'<option value="'
.IMG_FILTER_GRAYSCALE.
'" '
<span>;
</span><span>
if
</span>(<span>isset</span>(<span>
$_POST
</span>[
'effect'
]) && <span>
$_POST
</span>[
'effect'
] ==<span> IMG_FILTER_GRAYSCALE)
{
</span><span>
echo
</span>
'selected="selected"'
<span>;
}
</span><span>
echo
</span>
' >灰度图像</option>'
<span>;
</span><span>
echo
</span>
'<option value="'
.IMG_FILTER_GAUSSIAN_BLUR.
'"'
<span>;
</span><span>
if
</span>(<span>isset</span>(<span>
$_POST
</span>[
'effect'
]) && <span>
$_POST
</span>[
'effect'
] ==<span> IMG_FILTER_GAUSSIAN_BLUR)
{
</span><span>
echo
</span>
' selected="selected"'
<span>;
}
</span><span>
echo
</span>
'>高斯算法模糊图像</option>'
<span>;
</span><span>
echo
</span>
'<option value="'
.IMG_FILTER_EMBOSS.
'"'
<span>;
</span><span>
if
</span>(<span>isset</span>(<span>
$_POST
</span>[
'effect'
]) && <span>
$_POST
</span>[
'effect'
] ==<span> IMG_FILTER_EMBOSS)
{
</span><span>
echo
</span>
'selected="selected"'
<span>;
}
</span><span>
echo
</span>
'>图像浮雕化</option>'
<span>;
</span><span>
echo
</span>
'<option value="'
.IMG_FILTER_NEGATE.
'"'
<span>;
</span><span>
if
</span>(<span>isset</span>(<span>
$_POST
</span>[
'effect'
]) && <span>
$_POST
</span>[
'effect'
] ==<span> IMG_FILTER_NEGATE)
{
</span><span>
echo
</span>
'selected="selected"'
<span>;
}
</span><span>
echo
</span>
'>图像中所有颜色反转</option>'
<span>;
</span>?>
</select><br />
<?<span>php
</span><span>
echo
</span>
'<input type="checkbox" name="emb_caption"'
<span>;
</span><span>
if
</span>(<span>isset</span>(<span>
$_POST
</span>[
'emb_caption'
<span>]))
{
</span><span>
echo
</span>
' checked="checked"'
<span>;
}
</span><span>
echo
</span>
' />是否嵌入在图像的标题?'
<span>;
</span><span>
echo
</span>
'<br />'
<span>;
</span><span>
<span>
echo
</span>
'<input type="checkbox" name="emb_logo" '
<span>;
</span><span>
if
</span>(<span>isset</span>(<span>
$_POST
</span>[
'emb_logo'
<span>]))
{
</span><span>
echo
</span>
'checked="checked"'
<span>;
}
</span><span>
echo
</span>
' />是否嵌入668LOGO?'
.
'<br />'
<span>;
</span>?>
<input type=
"submit"
value=
"Save"
name=
"submit"
/>
<!--<input type=
"submit"
value=
"Preview"
name=
"submit"
/>-->
</form>
</div>
<?<span>php
}
</span><span>mysql_close</span>(<span>
$db
</span><span>);
</span>?>