PHP支援多種格式圖片上傳支援jpg、png、gif

WBOY
發布: 2016-07-29 08:47:09
原創
1218 人瀏覽過

此處一次支援上傳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>";
?>


有些問題需要說明下:
JPEG圖像產生的圖像的品質的是一個範圍從0(最低質量,最小的檔案大小)到100(最高品質,最大檔案大小)。而ImagePNG產生影像的品質範圍從0到9的,傳入的這個範圍以外的參數則該函數不會運作。
判斷上傳檔案的類型,$type = $_FILES['work1']['type'];。
建立各種格式的圖片可以使用這些函數:imagecreatefromjpeg()、imagecreatefrompng()、imagecreatefromgif()。

以上就介紹了 PHP支援多種格式圖片上傳支援jpg、png、gif,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板