您现在的位置:计算机技术学习网 > 技术中心 > WEB编程 > PHP >

一个完整的PHP图片上传,生成缩略图,插入数据库代码流程

来源:未知 责任编辑:智问网络 发表时间:2013-11-08 08:46 点击:

下面是PHP编写上传图片同时生成缩略图和插入数据库的一个完代码。
<?php
$title = $_POST[title];
$descrīption = $_POST[descrīption];
$albumID = $_POST[albumID];
$iscommend = $_POST[iscommend];
$isvisible = $_POST[isvisible];
$uploadtime = date("Y-m-d H:i:s");
$MAX_FILE_SIZE = floor($_POST[MAX_FILE_SIZE] / 1024);

$FileExtName = strtolower(pathinfo($_FILES[url][name],PATHINFO_EXTENSION));
if(!(($FileExtName == gif) or ($FileExtName == jpg) or ($FileExtName == jpeg) or ($FileExtName == png))){
    echo "<scrīpt language=javascrīpt>";
echo " alert(您上传的文件类型不对,本系统仅支持PNG,JPG,GIF三种格式。);";
echo " location=../uploadpicture.php?albumID=$albumID;";
echo "</scrīpt>";
exit;
}
$Filename = date("YmdHis").substr(microtime(),2,5)...$FileExtName;
$PictureDir = substr(pathinfo($_SERVER[scrīpt_FILENAME],PATHINFO_DIRNAME),0,strlen(pathinfo($_SERVER[scrīpt_FILENAME],PATHINFO_DIRNAME))-8);
$UploadURL = $PictureDir./.$PictureFolder.$Filename;
$ThumbsURL = $PictureDir./.$ThumbsFolder.$Filename;
if (!move_uploaded_file($_FILES[url][tmp_name], $UploadURL)){
    echo "<scrīpt language=javascrīpt>";
echo " alert(上传失败,图片大小不能超过 $MAX_FILE_SIZE KB。);";
echo " location=../uploadpicture.php?albumID=$albumID;";
echo "</scrīpt>";
exit;
}
list($RealWidth, $RealHeight) = getimagesize($UploadURL);
if($RealWidth < $MaxThumbWidth){
$percent = 1;
} else {
$percent = $MaxThumbWidth / $RealWidth;
}
$NewWidth = $RealWidth * $percent;
$NewHeight = $RealHeight * $percent;
$thumb = imagecreatetruecolor($NewWidth, $NewHeight);
list($width, $height, $PictureType, $attrib) = getimagesize($UploadURL);
switch($PictureType)
{
   case "1": $source = imagecreatefromgif($UploadURL); break;
   case "2": $source = imagecreatefromjpeg($UploadURL); break;
   case "3": $source = imagecreatefrompng($UploadURL); break;
}
imagecopyresized($thumb, $source, 0, 0, 0, 0, $NewWidth, $NewHeight, $RealWidth, $RealHeight);
switch($PictureType)
{
   case "1": imagegif($thumb, $ThumbsURL); break;
   case "2": imagejpeg($thumb, $ThumbsURL); break;
   case "3": imagepng($thumb, $ThumbsURL); break;
}

$newpicture_SQL = "INSERT INTO pictures (flag,title,url,descrīption,uploadtime,updatetime,isvisible,iscommend) VALUES ($albumID,$title,$Filename,$descrīption,$uploadtime,$uploadtime,$isvisible,$iscommend);";
$setCover = "Update pictures SET url = $Filename WHERE id = $albumID;";
$totalPictures_SQL = "SELECT * FROM pictures WHERE flag = $albumID;";
$totalPictures = mysql_query($totalPictures_SQL, $hesweb) or die(mysql_error());
$totalRows_totalPictures = mysql_num_rows($totalPictures);
$newpicture = mysql_query($newpicture_SQL, $hesweb) or die(mysql_error());
$pictureID = mysql_insert_id($hesweb);
$coverFlag = "UPDATE pictures SET iscover = 1 WHERE id = $pictureID;";
if($totalRows_totalPictures < 1){
     $cover = mysql_query($setCover, $hesweb) or die(mysql_error());
$flag = mysql_query($coverFlag, $hesweb) or die(mysql_error());
}
if($coverFlag){
    echo "<scrīpt language=javascrīpt>";
echo " alert(上传成功。);";
echo " location=../uploadpicture.php?albumID=$albumID;";
echo "</scrīpt>";
}
?>

看啦上面的代码希望大家能有所收获。

    发表评论
    请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
    用户名: 验证码:点击我更换图片
    最新评论 更多>>

    推荐热点

    • PHP测试
    • 十天学会php之第六天
    • 几种显示数据的方法的比较
    • 使用xmlhttp为网站增加域名查询功能
    • PHP+MYSQL+Javascript数据库查询结果的动态显示
    • 查找数组中指定键名的值
    • 用redis实现跨服务器session
    • 用新浪微博接口发送图片微博失败的原因
    • smarty局部缓存技术[源码分析]
    网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
    Copyright © 2008-2015 计算机技术学习交流网. 版权所有

    豫ICP备11007008号-1