此處一次支援上傳2張圖片,上傳後產生原圖和品質較差的圖,原圖用於保存高品質的圖片,品質差的圖用於網頁顯示。
PHP Code
複製程式碼 程式碼如下:
include_once("db.php");
include_once("dbinfo.php");
$connector = new nmdb($主機, $用戶名, $密碼);
$連接器-> select_db($資料庫);
$work_group = check_input($_POST['work_group']);
$name = check_input($_POST['name']);
$tel = check_input($_POST['tel']);
$id_card = check_input($_POST['id_card']);
$work_name = check_input($_POST['work_name']);
$place = check_input($_POST['place']);
$work1 = check_input($_POST['work1']);
date_default_timezone_set('PRC');
$date = date("Y-m-d H:i:s");
$質量= 100;
$質量2 = 70;
$quality_png = 9;
$quality2_png = 7;
if(isset($_FILES['work1']) && $_FILES['work1']['error']==0)
{
$size = getimagesize($_FILES['work1']['tmp_name'] );
$original = $_FILES['work1']['tmp_name'];
$type = $_FILES['work1']['type'];
//回顯$type; 'YmdHis').' _'.preg_replace('`[^a-z0-9-_.]`i','',$_FILES['work1']['name']) ;
$work1 = $
$target = 目錄名(__FILE__).'/uploads/'.$target_name;
switch($type)
case "image/jpeg":
$resultImage = imagecreatefromjpeg($original); $tarultImage, $target, $quality); image/png":
$resultImage = imagecreatefrompng($original);
imagepng($resultImage, $target, $quality_png);
case " image/gif":
)
是resultImage, $target, $quality);
休息;
default :
die("不支援此檔案型別");
退出;
$work1_small = $target_name;
= 目錄名(FI/__FI). uploads/small/'.$target_name;
switch($type)
{
case "image/jpeg":
$resultImage = imagecreatefromjpeg ($original);
imagejpage($resultImqumage);
休息;
case "image/png":
$resultImage = imagecreatefrompng($original);
imagepng($resultImage, $target, $quality2_png);
休息;
case "image/gif":
$resultImage = imagecreatefromgif($original);
imagegif($resultImage, $target, $quality2);
休息;
default :
die("不支援此文件類型");
退出;
}
}
if(isset($_FILES['work2']) && $_FILES['work2']['error']==0)
{
$size = getimagesize($_FILES[work2 ') '][ 'tmp_name']);
$original = $_FILES['work2']['tmp_name'];
$type = $_FILES['work2']['type'];
$target_name = date('YmdHis').'_'.preg_replace('`[^a-z0-9-_.]`i','',$_FILES['work2']['name']) ;
$work2 = $target_name;
$target = 目錄名(__FILE__).'/uploads/'.$target_name;
switch($type)
{
case "image/jpeg":
$resultImage = imagecreatefromjpeg($original);
imagejpeg($resultImage, $target, $quality);
休息;
case "image/png":
$resultImage = imagecreatefrompng($original);
imagepng($resultImage, $target, $quality_png);
休息;
case "image/gif":
$resultImage = imagecreatefromgif($original);
imagegif($resultImage, $target, $quality);
休息;
default :
die("不支援此文件類型");
退出;
}
$work2_small = $target_name;
$target = 目錄名(__FILE__).'/uploads/small/'.$target_name;
switch($type)
{
case "image/jpeg":
$resultImage = imagecreatefromjpeg($original);
imagejpeg($resultImage, $target, $quality2);
休息;
case "image/png":
$resultImage = imagecreatefrompng($original);
imagepng($resultImage, $target, $quality2_png);
休息;
case "image/gif":
$resultImage = imagecreatefromgif($original);
imagegif($resultImage, $target, $quality2);
休息;
default :
die("不支援此文件類型");
退出;
}
}
$sql="insert into photo(work_group,name,tel,id_card,work_name,place,work,work_small,date) values('$work_group','$name','$tel','$id_card',' $work_name','$place','$work1','$work1_small','$date')";
//echo $sql;
$result = $connector -> query($sql) or die(mysql_error ());
if($work2 != "" && $work2_small != null)
{
$sql2="insert into photo(work_group,name,tel,id_card,work_name,place,work,work_small,date) values ('$work_group','$name','$tel','$id_card','$work_name','$place','$work2','$work2_small','$date')";
$ result2 = $connector -> query($sql2) or die(mysql_error());
}
$connector -> close($connector);
$page="../upload.php";
echo "<script>alert('上傳成功!'); window.location = \"".$page."\";</script>";
?>
以上就介紹了 PHP支援多種格式圖片上傳支援jpg、png、gif,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。