1)上传图片页面:(片段)
在<head></head>中间加入下面程序,(这段程序很有用,可以在前台,用户端判断用户上传的文件类型,控制用户的上传文件)
<script language="JavaScript">
<!--
function CheckForm(theForm){
var fileext=theForm.zhaopian_up.value.substring(theForm.zhaopian_up.value.length-4,theForm.zhaopian_up.value.length)fileext=fileext.toLowerCase()if (!(fileext=='.jpg' || fileext=='.png')){alert("对不起,不正确的照片文件,必须为*.jpg或*.png文件 !");
theForm.zhaopian_up.focus();return false;}return true;}// -->
页面中加入上传表单:
<FORM ENCTYPE="multipart/form-data" ACTION="photo_upload2.php" METHOD="POST" οnsubmit="return CheckForm(this)">
注:请先上传照片,后填下表。第一张为封面照片,只能传jpg图片,小于300K,少于9张。
<p class="big"> 上传照片:</p>
<input type="file" name="zhaopian_up" size="20">
<INPUT TYPE="submit" value="上传">
</FORM>
2)处理页面程序,包含判断图片大小,上传拷贝,缩图,生成小图文件,图片ID存入数据库等:(片段)
<?
//取得上传文件大小,300K:
if ($zhaopian_up_size>307200) {
$wrong_massage="对不起,你上传的文件大小超过了300K,请缩小图再传!";
Header("Location:include/wrong_massage.php?wrong_massage=$wrong_massage");
exit();}
//自动缩图$srcFile原文件,大图;$photo_small目标文件,小图;$dstW,$dstH是小图的宽,高。function makethumb($srcFile,$photo_small,$dstW,$dstH) {
$data = GetImageSize($srcFile,&$info);
switch ($data[[2]]) {
case 1: //图片类型,1是GIF图
$im = @ImageCreateFromGIF($srcFile);
break;
case 2: //图片类型,2是JPG图
$im = @imagecreatefromjpeg($srcFile);
break;
case 3: //图片类型,3是PNG图
$im = @ImageCreateFromPNG($srcFile);
break;
}
$srcW=ImageSX($im);
$srcH=ImageSY($im);
$ni=ImageCreate($dstW,$dstH);
ImageCopyResized($ni,$im,0,0,0,0,$dstW,$dstH,$srcW,$srcH);
ImageJpeg($ni,$photo_small);
//ImageJpeg($ni);
//在显示图片时用,把注释取消,可以直接在页面显示出图片。
}
$srcFile=$zhaopian_up;
//取得文件扩展名:
$type=substr(strrchr($zhaopian_up_name,"."),1);
$photo_ID=time();
$photo_path="user_picture/$user/";
if(is_dir($photo_path)!=TRUE) mkdir($photo_path,0777);
$photo_small=$photo_path."/".$photo_ID."_s.".$type;
//小图
$photo_s=fopen($photo_small,"w+");
if ($type=="png" || $type=="jpg") makethumb($srcFile,$photo_small,110,130);
//110,130为小图宽,高
?>
在显示图片的程序中,只需从数据库中读出图片ID,用$photo_path."/".$photo_ID."_s.".$type就是显示小图,加个链接,就可以显示大图$photo_path."/".$photo_ID.".".$type;
在<head></head>中间加入下面程序,(这段程序很有用,可以在前台,用户端判断用户上传的文件类型,控制用户的上传文件)
<script language="JavaScript">
<!--
function CheckForm(theForm){
var fileext=theForm.zhaopian_up.value.substring(theForm.zhaopian_up.value.length-4,theForm.zhaopian_up.value.length)fileext=fileext.toLowerCase()if (!(fileext=='.jpg' || fileext=='.png')){alert("对不起,不正确的照片文件,必须为*.jpg或*.png文件 !");
theForm.zhaopian_up.focus();return false;}return true;}// -->
页面中加入上传表单:
<FORM ENCTYPE="multipart/form-data" ACTION="photo_upload2.php" METHOD="POST" οnsubmit="return CheckForm(this)">
注:请先上传照片,后填下表。第一张为封面照片,只能传jpg图片,小于300K,少于9张。
<p class="big"> 上传照片:</p>
<input type="file" name="zhaopian_up" size="20">
<INPUT TYPE="submit" value="上传">
</FORM>
2)处理页面程序,包含判断图片大小,上传拷贝,缩图,生成小图文件,图片ID存入数据库等:(片段)
<?
//取得上传文件大小,300K:
if ($zhaopian_up_size>307200) {
$wrong_massage="对不起,你上传的文件大小超过了300K,请缩小图再传!";
Header("Location:include/wrong_massage.php?wrong_massage=$wrong_massage");
exit();}
//自动缩图$srcFile原文件,大图;$photo_small目标文件,小图;$dstW,$dstH是小图的宽,高。function makethumb($srcFile,$photo_small,$dstW,$dstH) {
$data = GetImageSize($srcFile,&$info);
switch ($data[[2]]) {
case 1: //图片类型,1是GIF图
$im = @ImageCreateFromGIF($srcFile);
break;
case 2: //图片类型,2是JPG图
$im = @imagecreatefromjpeg($srcFile);
break;
case 3: //图片类型,3是PNG图
$im = @ImageCreateFromPNG($srcFile);
break;
}
$srcW=ImageSX($im);
$srcH=ImageSY($im);
$ni=ImageCreate($dstW,$dstH);
ImageCopyResized($ni,$im,0,0,0,0,$dstW,$dstH,$srcW,$srcH);
ImageJpeg($ni,$photo_small);
//ImageJpeg($ni);
//在显示图片时用,把注释取消,可以直接在页面显示出图片。
}
$srcFile=$zhaopian_up;
//取得文件扩展名:
$type=substr(strrchr($zhaopian_up_name,"."),1);
$photo_ID=time();
$photo_path="user_picture/$user/";
if(is_dir($photo_path)!=TRUE) mkdir($photo_path,0777);
$photo_small=$photo_path."/".$photo_ID."_s.".$type;
//小图
$photo_s=fopen($photo_small,"w+");
if ($type=="png" || $type=="jpg") makethumb($srcFile,$photo_small,110,130);
//110,130为小图宽,高
?>
在显示图片的程序中,只需从数据库中读出图片ID,用$photo_path."/".$photo_ID."_s.".$type就是显示小图,加个链接,就可以显示大图$photo_path."/".$photo_ID.".".$type;