php积累的一些技巧
时间:2005-12-15 来源:yunshen
php积累的一些技巧
1、关于PHP重定向
方法一:header("Location: index.php");
方法二:echo "window.location ="$PHP_SELF";";
方法三:echo "";
2、获取访问者浏览器
function browse_infor()
{
$browser="";$browserver="";
$Browsers =array("Lynx","MOSAIC","AOL","Opera","JAVA","MacWeb","WebExplorer","OmniWeb");
$Agent = $GLOBALS["HTTP_USER_AGENT"];
for ($i=0; $i 'image/gif',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'jpe' => 'image/jpeg',
'bmp' => 'image/bmp',
'png' => 'image/png',
'tif' => 'image/tiff',
'tiff' => 'image/tiff',
'pict' => 'image/x-pict',
'pic' => 'image/x-pict',
'pct' => 'image/x-pict',
'tif' => 'image/tiff',
'tiff' => 'image/tiff',
'psd' => 'image/x-photoshop',
'swf' => 'application/x-shockwave-flash',
'js' => 'application/x-javascript',
'pdf' => 'application/pdf',
'ps' => 'application/postscript',
'eps' => 'application/postscript',
'ai' => 'application/postscript',
'wmf' => 'application/x-msmetafile',
'css' => 'text/css',
'htm' => 'text/html',
'html' => 'text/html',
'txt' => 'text/plain',
'xml' => 'text/xml',
'wml' => 'text/wml',
'wbmp' => 'image/vnd.wap.wbmp',
'mid' => 'audio/midi',
'wav' => 'audio/wav',
'mp3' => 'audio/mpeg',
'mp2' => 'audio/mpeg',
'avi' => 'video/x-msvideo',
'mpeg' => 'video/mpeg',
'mpg' => 'video/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
'lha' => 'application/x-lha',
'lzh' => 'application/x-lha',
'z' => 'application/x-compress',
'gtar' => 'application/x-gtar',
'gz' => 'application/x-gzip',
'gzip' => 'application/x-gzip',
'tgz' => 'application/x-gzip',
'tar' => 'application/x-tar',
'bz2' => 'application/bzip2',
'zip' => 'application/zip',
'arj' => 'application/x-arj',
'rar' => 'application/x-rar-compressed',
'hqx' => 'application/mac-binhex40',
'sit' => 'application/x-stuffit',
'bin' => 'application/x-macbinary',
'uu' => 'text/x-uuencode',
'uue' => 'text/x-uuencode',
'latex'=> 'application/x-latex',
'ltx' => 'application/x-latex',
'tcl' => 'application/x-tcl',
'pgp' => 'application/pgp',
'asc' => 'application/pgp',
'exe' => 'application/x-msdownload',
'doc' => 'application/msword',
'rtf' => 'application/rtf',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
'mdb' => 'application/x-msaccess',
'wri' => 'application/x-mswrite',
);
5、php生成excel文档
[color="#0000bb"][color="#007700"]);
[color="#0000bb"]header[color="#007700"]([color="#dd0000"]"Content-Disposition:filename=test.xls"[color="#007700"]);
echo [color="#dd0000"]"test1 "[color="#007700"];
echo "test2
"[color="#007700"];
echo [color="#dd0000"]"test1 "[color="#007700"];
echo "test2
"[color="#007700"];
echo [color="#dd0000"]"test1 "[color="#007700"];
echo "test2
"[color="#007700"];
echo [color="#dd0000"]"test1 "[color="#007700"];
echo "test2
"[color="#007700"];
echo [color="#dd0000"]"test1 "[color="#007700"];
echo "test2
"[color="#007700"];
echo [color="#dd0000"]"test1 "[color="#007700"];
echo "test2
"[color="#007700"];
[color="#0000bb"]?>
//改动相应文件头就可以输出.doc .xls等文件格式了
6、时间比较问题
举一个简单例子说明:比如一个论坛对当天发表的贴子用new图片标记一下。
方法一:
//$db->rows[$i][date]中为数据库中datetime字段值.
$today=time();
$theDay=date("Y-m-d H:i:s",$today-24*3600);
$newTag=$db->rows[$i][date]>=$theDay?"":"";
方法二:
$newTag=$db->rows[$i][date]>=date("Y-m-d 00:00:00")?"":"";
7、PHP中对数据库操作的封装
我的例子.........见上面附件!
再增加一个正规表达式例子(自动转化www,ftp,mail开头的链接)
[color="#0000bb"][color="#007700"])
{
[color="#0000bb"]$str [color="#007700"]= [color="#0000bb"]ereg_replace[color="#007700"]([color="#dd0000"]"(www.[a-zA-Z0-9@:%_.~#-?&]+)"[color="#007700"], [color="#dd0000"]" 1"[color="#007700"], [color="#0000bb"]$str[color="#007700"]); [color="#ff8000"]// 自动转化www开头的链接
[color="#0000bb"]$str [color="#007700"]= [color="#0000bb"]ereg_replace[color="#007700"]([color="#dd0000"]"(((f|ht){1}tp://)[a-zA-Z0-9@:%_.~#-?&]+)"[color="#007700"], [color="#dd0000"]"1"[color="#007700"], [color="#0000bb"]$str[color="#007700"]); [color="#ff8000"]//转化http://开头和ftp://开头的链接
[color="#0000bb"]$str [color="#007700"]= [color="#0000bb"]str_replace[color="#007700"]([color="#dd0000"]'linkHttp://'[color="#007700"],[color="#dd0000"]'http://'[color="#007700"],[color="#0000bb"]$str[color="#007700"]);
[color="#0000bb"]$str [color="#007700"]= [color="#0000bb"]ereg_replace[color="#007700"]([color="#dd0000"]"([_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3})"[color="#007700"],[color="#dd0000"]"1"[color="#007700"], [color="#0000bb"]$str[color="#007700"]); [color="#ff8000"]// 转化邮件地址
[color="#007700"]return [color="#0000bb"]$str[color="#007700"];
}
[color="#ff8000"]//for example
[color="#007700"]echo [color="#0000bb"]enable_url[color="#007700"]([color="#dd0000"]"www.snowai.com"[color="#007700"]);
[color="#0000bb"]?>
__________________
取得文件扩展名方法
[color="#0000bb"][color="#ff8000"]//方法一:
[color="#007700"]function [color="#0000bb"]extend[color="#007700"]([color="#0000bb"]$file_name[color="#007700"])
{
[color="#0000bb"]$retval[color="#007700"]=[color="#dd0000"]""[color="#007700"];
[color="#0000bb"]$pt[color="#007700"]=[color="#0000bb"]strrpos[color="#007700"]([color="#0000bb"]$file_name[color="#007700"], [color="#dd0000"]"."[color="#007700"]);
if ([color="#0000bb"]$pt[color="#007700"]) [color="#0000bb"]$retval[color="#007700"]=[color="#0000bb"]substr[color="#007700"]([color="#0000bb"]$file_name[color="#007700"], [color="#0000bb"]$pt[color="#007700"]+[color="#0000bb"]1[color="#007700"], [color="#0000bb"]strlen[color="#007700"]([color="#0000bb"]$file_name[color="#007700"]) - [color="#0000bb"]$pt[color="#007700"]);
return ([color="#0000bb"]$retval[color="#007700"]);
}
[color="#ff8000"]//方法二(php4.03及更高版本)
[color="#0000bb"]$extend [color="#007700"]= [color="#0000bb"]pathinfo[color="#007700"]([color="#0000bb"]$file_name[color="#007700"]);
[color="#0000bb"]$extend [color="#007700"]= [color="#0000bb"]strtolower[color="#007700"]([color="#0000bb"]$extend[color="#007700"][[color="#dd0000"]"extension"[color="#007700"]]);
echo [color="#0000bb"]$extend[color="#007700"];
[color="#0000bb"]?>
Re: 再增加一个正规表达式例子(自动转化www,ftp,mail开头的链接)
quote:
--------------------------------------------------------------------------------
最初由 snowai 发布
[color="#0000bb"][color="#007700"])
{
[color="#0000bb"]$str [color="#007700"]= [color="#0000bb"]ereg_replace[color="#007700"]([color="#dd0000"]"(www.[a-zA-Z0-9@:%_.~#-?&]+)"[color="#007700"], [color="#dd0000"]" 1"[color="#007700"], [color="#0000bb"]$str[color="#007700"]); [color="#ff8000"]// 自动转化www开头的链接
[color="#0000bb"]$s[color="#007700"]...[color="#0000bb"]以下省略
[color="#007700"]--------------------------------------------------------------------------------
[color="#000000"])上传图片页面:(片段)
在中间加入下面程序,(这段程序很有用,可以在前台,用户端判断用户上传的文件类型,控制用户的上传文件)
页面中加入上传表单:
注:请先上传照片,后填下表。第一张为封面照片,只能传jpg图片,小于300K,少于9张。
上传照片:
2)处理页面程序,包含判断图片大小,上传拷贝,缩图,生成小图文件,图片ID存入数据库等:(片段)
[color="#0000bb"][color="#ff8000"]//取得上传文件大小,300K:
[color="#007700"]if ([color="#0000bb"]$zhaopian_up_size[color="#007700"]>[color="#0000bb"]307200[color="#007700"]) {
[color="#0000bb"]$wrong_massage[color="#007700"]=[color="#dd0000"]"对不起,你上传的文件大小超过了300K,请缩小图再传!"[color="#007700"];
[color="#0000bb"]Header[color="#007700"]([color="#dd0000"]"Location:include/wrong_massage.php?wrong_massage=$wrong_massage"[color="#007700"]);
exit();
}
[color="#ff8000"]//自动缩图$srcFile原文件,大图;$photo_small目标文件,小图;$dstW,$dstH是小图的宽,高。
[color="#007700"]function [color="#0000bb"]makethumb[color="#007700"]([color="#0000bb"]$srcFile[color="#007700"],[color="#0000bb"]$photo_small[color="#007700"],[color="#0000bb"]$dstW[color="#007700"],[color="#0000bb"]$dstH[color="#007700"]) {
[color="#0000bb"]$data [color="#007700"]= [color="#0000bb"]GetImageSize[color="#007700"]([color="#0000bb"]$srcFile[color="#007700"],&[color="#0000bb"]$info[color="#007700"]);
switch ([color="#0000bb"]$data[color="#007700"][[[color="#0000bb"]2[color="#007700"]]]) {
case [color="#0000bb"]1[color="#007700"]: [color="#ff8000"]//图片类型,1是GIF图
[color="#0000bb"]$im [color="#007700"]= @[color="#0000bb"]ImageCreateFromGIF[color="#007700"]([color="#0000bb"]$srcFile[color="#007700"]);
break;
case [color="#0000bb"]2[color="#007700"]: [color="#ff8000"]//图片类型,2是JPG图
[color="#0000bb"]$im [color="#007700"]= @[color="#0000bb"]imagecreatefromjpeg[color="#007700"]([color="#0000bb"]$srcFile[color="#007700"]);
break;
case [color="#0000bb"]3[color="#007700"]: [color="#ff8000"]//图片类型,3是PNG图
[color="#0000bb"]$im [color="#007700"]= @[color="#0000bb"]ImageCreateFromPNG[color="#007700"]([color="#0000bb"]$srcFile[color="#007700"]);
break;
}
[color="#0000bb"]$srcW[color="#007700"]=[color="#0000bb"]ImageSX[color="#007700"]([color="#0000bb"]$im[color="#007700"]);
[color="#0000bb"]$srcH[color="#007700"]=[color="#0000bb"]ImageSY[color="#007700"]([color="#0000bb"]$im[color="#007700"]);
[color="#0000bb"]$ni[color="#007700"]=[color="#0000bb"]ImageCreate[color="#007700"]([color="#0000bb"]$dstW[color="#007700"],[color="#0000bb"]$dstH[color="#007700"]);
[color="#0000bb"]ImageCopyResized[color="#007700"]([color="#0000bb"]$ni[color="#007700"],[color="#0000bb"]$im[color="#007700"],[color="#0000bb"]0[color="#007700"],[color="#0000bb"]0[color="#007700"],[color="#0000bb"]0[color="#007700"],[color="#0000bb"]0[color="#007700"],[color="#0000bb"]$dstW[color="#007700"],[color="#0000bb"]$dstH[color="#007700"],[color="#0000bb"]$srcW[color="#007700"],[color="#0000bb"]$srcH[color="#007700"]);
[color="#0000bb"]ImageJpeg[color="#007700"]([color="#0000bb"]$ni[color="#007700"],[color="#0000bb"]$photo_small[color="#007700"]);
[color="#ff8000"]//ImageJpeg($ni); //在显示图片时用,把注释取消,可以直接在页面显示出图片。
[color="#007700"]}
[color="#0000bb"]$srcFile[color="#007700"]=[color="#0000bb"]$zhaopian_up[color="#007700"];
[color="#ff8000"]//取得文件扩展名:
[color="#0000bb"]$type[color="#007700"]=[color="#0000bb"]substr[color="#007700"]([color="#0000bb"]strrchr[color="#007700"]([color="#0000bb"]$zhaopian_up_name[color="#007700"],[color="#dd0000"]"."[color="#007700"]),[color="#0000bb"]1[color="#007700"]);
[color="#0000bb"]$photo_ID[color="#007700"]=[color="#0000bb"]time[color="#007700"]();
[color="#0000bb"]$photo_path[color="#007700"]=[color="#dd0000"]"user_picture/$user/"[color="#007700"];
if([color="#0000bb"]is_dir[color="#007700"]([color="#0000bb"]$photo_path[color="#007700"])!=[color="#0000bb"]TRUE[color="#007700"]) [color="#0000bb"]mkdir[color="#007700"]([color="#0000bb"]$photo_path[color="#007700"],[color="#0000bb"]0777[color="#007700"]);
[color="#0000bb"]$photo_small[color="#007700"]=[color="#0000bb"]$photo_path[color="#007700"].[color="#dd0000"]"/"[color="#007700"].[color="#0000bb"]$photo_ID[color="#007700"].[color="#dd0000"]"_s."[color="#007700"].[color="#0000bb"]$type[color="#007700"]; [color="#ff8000"]//小图
[color="#0000bb"]$photo_s[color="#007700"]=[color="#0000bb"]fopen[color="#007700"]([color="#0000bb"]$photo_small[color="#007700"],[color="#dd0000"]"w+"[color="#007700"]);
if ([color="#0000bb"]$type[color="#007700"]==[color="#dd0000"]"png" [color="#007700"]|| [color="#0000bb"]$type[color="#007700"]==[color="#dd0000"]"jpg"[color="#007700"]) [color="#0000bb"]makethumb[color="#007700"]([color="#0000bb"]$srcFile[color="#007700"],[color="#0000bb"]$photo_small[color="#007700"],[color="#0000bb"]110[color="#007700"],[color="#0000bb"]130[color="#007700"]);[color="#ff8000"]//110,130为小图宽,高
[color="#0000bb"]?>
[color="#000000"]在显示图片的程序中,只需从数据库中读出图片ID,用$photo_path."/".$photo_ID."_s.".$type就是显示小图,加个链接,就可以显示大图$photo_path."/".$photo_ID.".".$type;
1、关于PHP重定向
方法一:header("Location: index.php");
方法二:echo "window.location ="$PHP_SELF";";
方法三:echo "";
2、获取访问者浏览器
function browse_infor()
{
$browser="";$browserver="";
$Browsers =array("Lynx","MOSAIC","AOL","Opera","JAVA","MacWeb","WebExplorer","OmniWeb");
$Agent = $GLOBALS["HTTP_USER_AGENT"];
for ($i=0; $i 'image/gif',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'jpe' => 'image/jpeg',
'bmp' => 'image/bmp',
'png' => 'image/png',
'tif' => 'image/tiff',
'tiff' => 'image/tiff',
'pict' => 'image/x-pict',
'pic' => 'image/x-pict',
'pct' => 'image/x-pict',
'tif' => 'image/tiff',
'tiff' => 'image/tiff',
'psd' => 'image/x-photoshop',
'swf' => 'application/x-shockwave-flash',
'js' => 'application/x-javascript',
'pdf' => 'application/pdf',
'ps' => 'application/postscript',
'eps' => 'application/postscript',
'ai' => 'application/postscript',
'wmf' => 'application/x-msmetafile',
'css' => 'text/css',
'htm' => 'text/html',
'html' => 'text/html',
'txt' => 'text/plain',
'xml' => 'text/xml',
'wml' => 'text/wml',
'wbmp' => 'image/vnd.wap.wbmp',
'mid' => 'audio/midi',
'wav' => 'audio/wav',
'mp3' => 'audio/mpeg',
'mp2' => 'audio/mpeg',
'avi' => 'video/x-msvideo',
'mpeg' => 'video/mpeg',
'mpg' => 'video/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
'lha' => 'application/x-lha',
'lzh' => 'application/x-lha',
'z' => 'application/x-compress',
'gtar' => 'application/x-gtar',
'gz' => 'application/x-gzip',
'gzip' => 'application/x-gzip',
'tgz' => 'application/x-gzip',
'tar' => 'application/x-tar',
'bz2' => 'application/bzip2',
'zip' => 'application/zip',
'arj' => 'application/x-arj',
'rar' => 'application/x-rar-compressed',
'hqx' => 'application/mac-binhex40',
'sit' => 'application/x-stuffit',
'bin' => 'application/x-macbinary',
'uu' => 'text/x-uuencode',
'uue' => 'text/x-uuencode',
'latex'=> 'application/x-latex',
'ltx' => 'application/x-latex',
'tcl' => 'application/x-tcl',
'pgp' => 'application/pgp',
'asc' => 'application/pgp',
'exe' => 'application/x-msdownload',
'doc' => 'application/msword',
'rtf' => 'application/rtf',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
'mdb' => 'application/x-msaccess',
'wri' => 'application/x-mswrite',
);
5、php生成excel文档
[color="#0000bb"][color="#007700"]);
[color="#0000bb"]header[color="#007700"]([color="#dd0000"]"Content-Disposition:filename=test.xls"[color="#007700"]);
echo [color="#dd0000"]"test1 "[color="#007700"];
echo "test2
"[color="#007700"];
echo [color="#dd0000"]"test1 "[color="#007700"];
echo "test2
"[color="#007700"];
echo [color="#dd0000"]"test1 "[color="#007700"];
echo "test2
"[color="#007700"];
echo [color="#dd0000"]"test1 "[color="#007700"];
echo "test2
"[color="#007700"];
echo [color="#dd0000"]"test1 "[color="#007700"];
echo "test2
"[color="#007700"];
echo [color="#dd0000"]"test1 "[color="#007700"];
echo "test2
"[color="#007700"];
[color="#0000bb"]?>
//改动相应文件头就可以输出.doc .xls等文件格式了
6、时间比较问题
举一个简单例子说明:比如一个论坛对当天发表的贴子用new图片标记一下。
方法一:
//$db->rows[$i][date]中为数据库中datetime字段值.
$today=time();
$theDay=date("Y-m-d H:i:s",$today-24*3600);
$newTag=$db->rows[$i][date]>=$theDay?"":"";
方法二:
$newTag=$db->rows[$i][date]>=date("Y-m-d 00:00:00")?"":"";
7、PHP中对数据库操作的封装
我的例子.........见上面附件!
再增加一个正规表达式例子(自动转化www,ftp,mail开头的链接)
[color="#0000bb"][color="#007700"])
{
[color="#0000bb"]$str [color="#007700"]= [color="#0000bb"]ereg_replace[color="#007700"]([color="#dd0000"]"(www.[a-zA-Z0-9@:%_.~#-?&]+)"[color="#007700"], [color="#dd0000"]" 1"[color="#007700"], [color="#0000bb"]$str[color="#007700"]); [color="#ff8000"]// 自动转化www开头的链接
[color="#0000bb"]$str [color="#007700"]= [color="#0000bb"]ereg_replace[color="#007700"]([color="#dd0000"]"(((f|ht){1}tp://)[a-zA-Z0-9@:%_.~#-?&]+)"[color="#007700"], [color="#dd0000"]"1"[color="#007700"], [color="#0000bb"]$str[color="#007700"]); [color="#ff8000"]//转化http://开头和ftp://开头的链接
[color="#0000bb"]$str [color="#007700"]= [color="#0000bb"]str_replace[color="#007700"]([color="#dd0000"]'linkHttp://'[color="#007700"],[color="#dd0000"]'http://'[color="#007700"],[color="#0000bb"]$str[color="#007700"]);
[color="#0000bb"]$str [color="#007700"]= [color="#0000bb"]ereg_replace[color="#007700"]([color="#dd0000"]"([_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3})"[color="#007700"],[color="#dd0000"]"1"[color="#007700"], [color="#0000bb"]$str[color="#007700"]); [color="#ff8000"]// 转化邮件地址
[color="#007700"]return [color="#0000bb"]$str[color="#007700"];
}
[color="#ff8000"]//for example
[color="#007700"]echo [color="#0000bb"]enable_url[color="#007700"]([color="#dd0000"]"www.snowai.com"[color="#007700"]);
[color="#0000bb"]?>
__________________
取得文件扩展名方法
[color="#0000bb"][color="#ff8000"]//方法一:
[color="#007700"]function [color="#0000bb"]extend[color="#007700"]([color="#0000bb"]$file_name[color="#007700"])
{
[color="#0000bb"]$retval[color="#007700"]=[color="#dd0000"]""[color="#007700"];
[color="#0000bb"]$pt[color="#007700"]=[color="#0000bb"]strrpos[color="#007700"]([color="#0000bb"]$file_name[color="#007700"], [color="#dd0000"]"."[color="#007700"]);
if ([color="#0000bb"]$pt[color="#007700"]) [color="#0000bb"]$retval[color="#007700"]=[color="#0000bb"]substr[color="#007700"]([color="#0000bb"]$file_name[color="#007700"], [color="#0000bb"]$pt[color="#007700"]+[color="#0000bb"]1[color="#007700"], [color="#0000bb"]strlen[color="#007700"]([color="#0000bb"]$file_name[color="#007700"]) - [color="#0000bb"]$pt[color="#007700"]);
return ([color="#0000bb"]$retval[color="#007700"]);
}
[color="#ff8000"]//方法二(php4.03及更高版本)
[color="#0000bb"]$extend [color="#007700"]= [color="#0000bb"]pathinfo[color="#007700"]([color="#0000bb"]$file_name[color="#007700"]);
[color="#0000bb"]$extend [color="#007700"]= [color="#0000bb"]strtolower[color="#007700"]([color="#0000bb"]$extend[color="#007700"][[color="#dd0000"]"extension"[color="#007700"]]);
echo [color="#0000bb"]$extend[color="#007700"];
[color="#0000bb"]?>
Re: 再增加一个正规表达式例子(自动转化www,ftp,mail开头的链接)
quote:
--------------------------------------------------------------------------------
最初由 snowai 发布
[color="#0000bb"][color="#007700"])
{
[color="#0000bb"]$str [color="#007700"]= [color="#0000bb"]ereg_replace[color="#007700"]([color="#dd0000"]"(www.[a-zA-Z0-9@:%_.~#-?&]+)"[color="#007700"], [color="#dd0000"]" 1"[color="#007700"], [color="#0000bb"]$str[color="#007700"]); [color="#ff8000"]// 自动转化www开头的链接
[color="#0000bb"]$s[color="#007700"]...[color="#0000bb"]以下省略
[color="#007700"]--------------------------------------------------------------------------------
[color="#000000"])上传图片页面:(片段)
在中间加入下面程序,(这段程序很有用,可以在前台,用户端判断用户上传的文件类型,控制用户的上传文件)
页面中加入上传表单:
注:请先上传照片,后填下表。第一张为封面照片,只能传jpg图片,小于300K,少于9张。
上传照片:
2)处理页面程序,包含判断图片大小,上传拷贝,缩图,生成小图文件,图片ID存入数据库等:(片段)
[color="#0000bb"][color="#ff8000"]//取得上传文件大小,300K:
[color="#007700"]if ([color="#0000bb"]$zhaopian_up_size[color="#007700"]>[color="#0000bb"]307200[color="#007700"]) {
[color="#0000bb"]$wrong_massage[color="#007700"]=[color="#dd0000"]"对不起,你上传的文件大小超过了300K,请缩小图再传!"[color="#007700"];
[color="#0000bb"]Header[color="#007700"]([color="#dd0000"]"Location:include/wrong_massage.php?wrong_massage=$wrong_massage"[color="#007700"]);
exit();
}
[color="#ff8000"]//自动缩图$srcFile原文件,大图;$photo_small目标文件,小图;$dstW,$dstH是小图的宽,高。
[color="#007700"]function [color="#0000bb"]makethumb[color="#007700"]([color="#0000bb"]$srcFile[color="#007700"],[color="#0000bb"]$photo_small[color="#007700"],[color="#0000bb"]$dstW[color="#007700"],[color="#0000bb"]$dstH[color="#007700"]) {
[color="#0000bb"]$data [color="#007700"]= [color="#0000bb"]GetImageSize[color="#007700"]([color="#0000bb"]$srcFile[color="#007700"],&[color="#0000bb"]$info[color="#007700"]);
switch ([color="#0000bb"]$data[color="#007700"][[[color="#0000bb"]2[color="#007700"]]]) {
case [color="#0000bb"]1[color="#007700"]: [color="#ff8000"]//图片类型,1是GIF图
[color="#0000bb"]$im [color="#007700"]= @[color="#0000bb"]ImageCreateFromGIF[color="#007700"]([color="#0000bb"]$srcFile[color="#007700"]);
break;
case [color="#0000bb"]2[color="#007700"]: [color="#ff8000"]//图片类型,2是JPG图
[color="#0000bb"]$im [color="#007700"]= @[color="#0000bb"]imagecreatefromjpeg[color="#007700"]([color="#0000bb"]$srcFile[color="#007700"]);
break;
case [color="#0000bb"]3[color="#007700"]: [color="#ff8000"]//图片类型,3是PNG图
[color="#0000bb"]$im [color="#007700"]= @[color="#0000bb"]ImageCreateFromPNG[color="#007700"]([color="#0000bb"]$srcFile[color="#007700"]);
break;
}
[color="#0000bb"]$srcW[color="#007700"]=[color="#0000bb"]ImageSX[color="#007700"]([color="#0000bb"]$im[color="#007700"]);
[color="#0000bb"]$srcH[color="#007700"]=[color="#0000bb"]ImageSY[color="#007700"]([color="#0000bb"]$im[color="#007700"]);
[color="#0000bb"]$ni[color="#007700"]=[color="#0000bb"]ImageCreate[color="#007700"]([color="#0000bb"]$dstW[color="#007700"],[color="#0000bb"]$dstH[color="#007700"]);
[color="#0000bb"]ImageCopyResized[color="#007700"]([color="#0000bb"]$ni[color="#007700"],[color="#0000bb"]$im[color="#007700"],[color="#0000bb"]0[color="#007700"],[color="#0000bb"]0[color="#007700"],[color="#0000bb"]0[color="#007700"],[color="#0000bb"]0[color="#007700"],[color="#0000bb"]$dstW[color="#007700"],[color="#0000bb"]$dstH[color="#007700"],[color="#0000bb"]$srcW[color="#007700"],[color="#0000bb"]$srcH[color="#007700"]);
[color="#0000bb"]ImageJpeg[color="#007700"]([color="#0000bb"]$ni[color="#007700"],[color="#0000bb"]$photo_small[color="#007700"]);
[color="#ff8000"]//ImageJpeg($ni); //在显示图片时用,把注释取消,可以直接在页面显示出图片。
[color="#007700"]}
[color="#0000bb"]$srcFile[color="#007700"]=[color="#0000bb"]$zhaopian_up[color="#007700"];
[color="#ff8000"]//取得文件扩展名:
[color="#0000bb"]$type[color="#007700"]=[color="#0000bb"]substr[color="#007700"]([color="#0000bb"]strrchr[color="#007700"]([color="#0000bb"]$zhaopian_up_name[color="#007700"],[color="#dd0000"]"."[color="#007700"]),[color="#0000bb"]1[color="#007700"]);
[color="#0000bb"]$photo_ID[color="#007700"]=[color="#0000bb"]time[color="#007700"]();
[color="#0000bb"]$photo_path[color="#007700"]=[color="#dd0000"]"user_picture/$user/"[color="#007700"];
if([color="#0000bb"]is_dir[color="#007700"]([color="#0000bb"]$photo_path[color="#007700"])!=[color="#0000bb"]TRUE[color="#007700"]) [color="#0000bb"]mkdir[color="#007700"]([color="#0000bb"]$photo_path[color="#007700"],[color="#0000bb"]0777[color="#007700"]);
[color="#0000bb"]$photo_small[color="#007700"]=[color="#0000bb"]$photo_path[color="#007700"].[color="#dd0000"]"/"[color="#007700"].[color="#0000bb"]$photo_ID[color="#007700"].[color="#dd0000"]"_s."[color="#007700"].[color="#0000bb"]$type[color="#007700"]; [color="#ff8000"]//小图
[color="#0000bb"]$photo_s[color="#007700"]=[color="#0000bb"]fopen[color="#007700"]([color="#0000bb"]$photo_small[color="#007700"],[color="#dd0000"]"w+"[color="#007700"]);
if ([color="#0000bb"]$type[color="#007700"]==[color="#dd0000"]"png" [color="#007700"]|| [color="#0000bb"]$type[color="#007700"]==[color="#dd0000"]"jpg"[color="#007700"]) [color="#0000bb"]makethumb[color="#007700"]([color="#0000bb"]$srcFile[color="#007700"],[color="#0000bb"]$photo_small[color="#007700"],[color="#0000bb"]110[color="#007700"],[color="#0000bb"]130[color="#007700"]);[color="#ff8000"]//110,130为小图宽,高
[color="#0000bb"]?>
[color="#000000"]在显示图片的程序中,只需从数据库中读出图片ID,用$photo_path."/".$photo_ID."_s.".$type就是显示小图,加个链接,就可以显示大图$photo_path."/".$photo_ID.".".$type;
相关阅读 更多 +