国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

PHP 上傳圖片并壓縮方法

瀏覽:91日期:2022-09-12 11:37:00

使用到三個文件

connect.php:連接數據庫用 test_upload.php:執行SQL語句用 upload_img.php:上傳圖片并壓縮用

三個文件代碼如下:

connect.php

<?php$db_host = ’’;$db_user = ’’;$db_psw = ’’;$db_name = ’’;$db_port = ’’;$sqlconn=new mysqli($db_host,$db_user,$db_psw,$db_name);$q='set names utf8;';$result=$sqlconn->query($q);if (mysqli_connect_errno()) {printf('Connect failed: %sn', mysqli_connect_error());exit();}?>

<p><b>test_upload.php</b></p><?phprequire ('connect.php');require ('upload_img.php');$real_img=$uploadfile; $small_img=$uploadfile_resize;$insert_sql = 'insert into img (real_img,small_img) values (?,?)';$result = $sqlconn -> prepare($insert_sql);$result -> bind_param('ss', $real_img,$small_img);$result -> execute();?>

upload_img.php

<?php //設置文件保存目錄$uploaddir = 'upfiles/'; //設置允許上傳文件的類型$type=array('jpg','gif','bmp','jpeg','png'); //獲取文件后綴名函數 function fileext($filename) { return substr(strrchr($filename, ’.’), 1); } //生成隨機文件名函數 function random($length) { $hash = ’CR-’; $chars = ’ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz’; $max = strlen($chars) - 1; mt_srand((double)microtime() * 1000000); for($i = 0; $i < $length; $i++) { $hash .= $chars[mt_rand(0, $max)]; } return $hash; } $a=strtolower(fileext($_FILES[’filename’][’name’])); //判斷文件類型 if(!in_array(strtolower(fileext($_FILES[’filename’][’name’])),$type)) { $text=implode(',',$type); $ret_code=3;//文件類型錯誤 $page_result=$text; $retArray = array(’ret_code’ => $ret_code,’page_result’=>$page_result); $retJson = json_encode($retArray); echo $retJson; return;} //生成目標文件的文件名 else{ $filename=explode('.',$_FILES[’filename’][’name’]); do { $filename[0]=random(10); //設置隨機數長度 $name=implode('.',$filename); //$name1=$name.'.Mcncc'; $uploadfile=$uploaddir.$name; } while(file_exists($uploadfile)); if (move_uploaded_file($_FILES[’filename’][’tmp_name’],$uploadfile)) { if(is_uploaded_file($_FILES[’filename’][’tmp_name’])) { $ret_code=1;//上傳失敗} else {//上傳成功$ret_code=0; }} $retArray = array(’ret_code’ => $ret_code);$retJson = json_encode($retArray);echo $retJson;}//壓縮圖片$uploaddir_resize='upfiles_resize/';$uploadfile_resize=$uploaddir_resize.$name;//$pic_width_max=120;//$pic_height_max=90;//以上與下面段注釋可以聯合使用,可以使圖片根據計算出來的比例壓縮$file_type=$_FILES['filename'][’type’];function ResizeImage($uploadfile,$maxwidth,$maxheight,$name){ //取得當前圖片大小 $width = imagesx($uploadfile); $height = imagesy($uploadfile); $i=0.5; //生成縮略圖的大小 if(($width > $maxwidth) || ($height > $maxheight)) {/*$widthratio = $maxwidth/$width;$heightratio = $maxheight/$height;if($widthratio < $heightratio){ $ratio = $widthratio;}else{ $ratio = $heightratio;}$newwidth = $width * $ratio;$newheight = $height * $ratio;*/$newwidth = $width * $i;$newheight = $height * $i;if(function_exists('imagecopyresampled')){ $uploaddir_resize = imagecreatetruecolor($newwidth, $newheight); imagecopyresampled($uploaddir_resize, $uploadfile, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);}else{ $uploaddir_resize = imagecreate($newwidth, $newheight); imagecopyresized($uploaddir_resize, $uploadfile, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);}ImageJpeg ($uploaddir_resize,$name);ImageDestroy ($uploaddir_resize); } else {ImageJpeg ($uploadfile,$name); }}if($_FILES['filename'][’size’]){ if($file_type == 'image/pjpeg'||$file_type == 'image/jpg'|$file_type == 'image/jpeg') {//$im = imagecreatefromjpeg($_FILES[$upload_input_name][’tmp_name’]);$im = imagecreatefromjpeg($uploadfile); } elseif($file_type == 'image/x-png') {//$im = imagecreatefrompng($_FILES[$upload_input_name][’tmp_name’]);$im = imagecreatefromjpeg($uploadfile); } elseif($file_type == 'image/gif') {//$im = imagecreatefromgif($_FILES[$upload_input_name][’tmp_name’]);$im = imagecreatefromjpeg($uploadfile); } else//默認jpg {$im = imagecreatefromjpeg($uploadfile); } if($im) {ResizeImage($im,$pic_width_max,$pic_height_max,$uploadfile_resize); ImageDestroy ($im); }} ?>

請按照現實情況更改connect.php,test_upload.php中對應的信息。

標簽: PHP
相關文章:
主站蜘蛛池模板: 欧美午夜免费一级毛片 | 欧美成人性动漫在线观看 | 免费一级a毛片在线播放视 免费一级α片在线观看 | 俄罗斯小屁孩cao大人免费 | 欧美国产综合视频 | 偷拍精品视频一区二区三区 | www.亚洲综合 | 欧美一区二区在线观看免费网站 | 泰国一级毛片aaa下面毛多 | 国产成人综合网在线观看 | 精品欧美一区二区三区在线观看 | 亚洲精品综合一区二区三区 | 91b站 | 日本成人免费观看 | 亚洲人成毛片线播放 | 可以免费看黄色的网站 | 亚洲综合小视频 | 夜鲁夜鲁夜鲁在线观看福利 | 青青草福利视频 | 一二三中文乱码亚洲乱码 | 欧美日韩精品免费一区二区三区 | 久久综合综合久久 | 国产一区欧美 | 精品国产成人a在线观看 | 男女男精品视频在线播放 | 成人a影片在线观看 | 国产成a人片在线观看视频 国产成版人视频网站免费下 | 久久精品高清 | 偷柏自拍亚洲欧美综合在线图 | 国产日产久久 | 深夜福利成人 | 美女很黄很黄是免费的·无遮挡网站 | 一本高清 | 成年网在线观看免费观看网址 | 香蕉国产人午夜视频在线 | 亚洲精品aaa| 萌白酱白丝护士服喷水铁牛tv | 美女把张开腿男生猛戳免费视频 | 欧美日韩成人 | www亚洲成人 | 日韩欧美中文字幕在线播放 |