if(!empty($_FILES["fileField"]["name"])){//檢測表單傳遞文件數據 $fileinfo = $_FILES["fileField"]; if($fileinfo['size'] < 10000000 && $fileinfo['size'] > 0){//大小 ...
if(!empty($_FILES["fileField"]["name"])){//檢測表單傳遞文件數據
$fileinfo = $_FILES["fileField"];
if($fileinfo['size'] < 10000000 && $fileinfo['size'] > 0){//大小篩選
if($fileinfo['type'] == 'image/jpeg'||'image/jpg'||'image/png'||'image/gif' ){//類型篩選
if(!file_exists("./uploadimages")){
mkdir("./uploadimages");
}
move_uploaded_file($fileinfo['tmp_name'],"uploadimages/".$fileinfo['name']) or die("文件上傳失敗");
$path=(string)$fileinfo['name'];
$sql="insert into banner values(' ','$path','$alt','$riqi',' ','$url')";
$insert_id=mysql_query($sql);
if($insert_id){
makezoom("uploadimages/".$fileinfo['name'],200,100,'uploadimages/200_100/');
makezoom("uploadimages/".$fileinfo['name'],1920,900,'uploadimages/1920_900/');
echo "<script>alert('添加成功');</script>";
echo "<script language=\"javascript\">setTimeout('window.history.back(-1)',0);</script>";
}else{
$error=mysql_error();
echo $error;
//echo "<script>alert('添加失敗'.$error);</script>";
echo "<script language=\"javascript\">setTimeout('window.history.back(-1)',3000);</script>";
}
}else{echo "(請上傳正確格式的圖片,jpg,png,gif)";
echo "<script language=\"javascript\">setTimeout('window.history.back(-1)',3000);</script>";}
}else{
echo ("圖片過大或者未知...");
echo "<script language=\"javascript\">setTimeout('window.history.back(-1)',3000);</script>";
}
}
function makezoom($src='moban.com/images/bg1.jpg',$width=200,$height=200,$dir = 'uploadimages/200_200/',$delsrc=false)//產生縮略圖函數
{
if(!file_exists($dir)){
mkdir($dir);
}
$srcInfo = getimagesize($src);//獲取圖片信息數組
$srcType = image_type_to_extension($srcInfo[2], false);//獲取圖片類型
$fun = 'imagecreatefrom' . $srcType;//由圖片文件創建圖像資源
$srcImage = $fun($src);
//創建縮略圖資源
//$scale = $srcInfo[0] / $srcInfo[1];
//$height = $width / $scale;
$zoomImg = imagecreatetruecolor($width, $height);
//圖像資源放到縮略圖資源上
imagecopyresampled($zoomImg, $srcImage, 0, 0, 0, 0, $width, $height, $srcInfo[0], $srcInfo[1]);
//輸出縮略圖資源
$output = 'image' . $srcType;
$output($zoomImg, $dir. basename($src));
//銷毀資源
imagedestroy($srcImage);
imagedestroy($zoomImg);
//刪除原圖
if($delsrc){
unlink($src);
}
}