PHP支持多种格式图片上传(支持jpg、png、gif)

前端之家收集整理的这篇文章主要介绍了PHP支持多种格式图片上传(支持jpg、png、gif)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

此处一次支持上传2个图片上传生成原图和质量较差的图,原图用于保存质量高的图片,质量差的图用于网页显示
PHP Code
<div class="codetitle"><a style="CURSOR: pointer" data="5932" class="copybut" id="copybut5932" onclick="doCopy('code5932')"> 代码如下:

<div class="codebody" id="code5932">
<?PHP
include_once("db.PHP");
include_once("dbinfo.PHP");
$connector = new nmdb($host,$username,$password);
$connector -> select_db($database);
$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");
$quality = 100;
$quality2 = 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'];
//echo $type;
$targetname = date('YmdHis').''.pregreplace('`[^a-z0-9-.]i&#039;,&#039;&#039;,$_FILES&#91;&#039;work1&#039;&#93;&#91;&#039;name&#039;&#93;); <BR>$work1 = $target_name; <BR>$target = dirname(__FILE__).&#039;/uploads/&#039;.$target_name; <BR>switch($type) <BR>{ <BR>case "image/jpeg": <BR>$resultImage = imagecreatefromjpeg($original); <BR>imagejpeg($resultImage,$target,$quality); <BR>break; <BR>case "image/png": <BR>$resultImage = imagecreatefrompng($original); <BR>imagepng($resultImage,$quality_png); <BR>break; <BR>case "image/gif": <BR>$resultImage = imagecreatefromgif($original); <BR>imagegif($resultImage,$quality); <BR>break; <BR>default : <BR>die("<a href="https://www.jb51.cc/tag/buzhichi/" target="_blank" class="keywords">不支持</a>此<a href="https://www.jb51.cc/tag/wenjian/" target="_blank" class="keywords">文件</a>类型"); <BR>exit; <BR>} <BR>$work1_small = $target_name; <BR>$target = dirname(__FILE__).&#039;/uploads/small/&#039;.$target_name; <BR>switch($type) <BR>{ <BR>case "image/jpeg": <BR>$resultImage = imagecreatefromjpeg($original); <BR>imagejpeg($resultImage,$quality2); <BR>break; <BR>case "image/png": <BR>$resultImage = imagecreatefrompng($original); <BR>imagepng($resultImage,$quality2_png); <BR>break; <BR>case "image/gif": <BR>$resultImage = imagecreatefromgif($original); <BR>imagegif($resultImage,$quality2); <BR>break; <BR>default : <BR>die("<a href="https://www.jb51.cc/tag/buzhichi/" target="_blank" class="keywords">不支持</a>此<a href="https://www.jb51.cc/tag/wenjian/" target="_blank" class="keywords">文件</a>类型"); <BR>exit; <BR>} <BR>} <BR>if(isset($_FILES&#91;&#039;work2&#039;&#93;) &amp;&amp; $_FILES&#91;&#039;work2&#039;&#93;&#91;&#039;error&#039;&#93;==0) <BR>{ <BR>$size = getimagesize($_FILES&#91;&#039;work2&#039;&#93;&#91;&#039;tmp_name&#039;&#93;); <BR>$original = $_FILES&#91;&#039;work2&#039;&#93;&#91;&#039;tmp_name&#039;&#93;; <BR>$type = $_FILES&#91;&#039;work2&#039;&#93;&#91;&#039;type&#039;&#93;; <BR>$target_name = date(&#039;YmdHis&#039;).&#039;_&#039;.preg_replace(&#039;[^a-z0-9-_.]`i',$_FILES['work2']['name']);
$work2 = $target_name;
$target = dirname(FILE).'/uploads/'.$target_name;
switch($type)
{
case "image/jpeg":
$resultImage = imagecreatefromjpeg($original);
imagejpeg($resultImage,$quality);
break;
default :
die("不支持文件类型");
exit;
}
$work2_small = $target_name;
$target = dirname(FILE).'/uploads/small/'.$target_name;
switch($type)
{
case "image/jpeg":
$resultImage = imagecreatefromjpeg($original);
imagejpeg($resultImage,$quality2);
break;
default :
die("不支持文件类型");
exit;
}
}
$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,'$work2','$work2_small','$date')";
$result2 = $connector -> query($sql2) or die(MysqL_error());
}
$connector -> close($connector);
$page="../upload.PHP";
echo "";
?>

有些问题需要说明下:
JPEG图像生成的图像的质量的是一个范围从0(最低质量,最小的文件大小)到100(最高质量,最大文件大小)。而ImagePNG生成图像的质量范围从0到9的,传入的这个范围以外的参数则该函数不会工作。
判断上传文件的类型,$type = $_FILES['work1']['type'];。
创建各种格式的图片可以使用这些函数:imagecreatefromjpeg()、imagecreatefrompng()、imagecreatefromgif()。

原文链接:https://www.f2er.com/php/27536.html

猜你在找的PHP相关文章