亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

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

PHP 上傳圖片并壓縮方法

瀏覽:116日期: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
相關文章:
主站蜘蛛池模板: xxx毛茸茸的亚洲 | 国产免费又色又爽又黄的网站 | 欧美成人免费一区在线播放 | 色黄网站在线观看 | 肉色网站| 亚洲欧美黄 | 日韩精品一区二区三区免费视频 | jizzjizzjizz亚洲护士 | 国产在线一区观看 | 国产午夜永久福利视频在线观看 | 一级一级一片免费 | 久99久精品视频免费观看v | 亚洲精品午夜久久aaa级久久久 | 51久久夜色精品国产 | 国产精品美女福利视频一区 | 久久99精品久久久久久国产越南 | 99久久精品国产自免费 | 亚洲国产一成人久久精品 | 在线看免费观看韩国特黄一级 | 国产精品品福利视频 | 成人在线播放 | 国产精品嫩草影院午夜 | 色拍拍在线精品视频 | 久久香蕉国产线看观看精品蕉 | 免费黄色大片在线观看 | 国产精品嫩草影院在线播放 | 国产亚洲欧美在线观看的 | 国产亚洲欧洲国产综合一区 | 一级黄黄色片 | 欧洲欧美成人免费大片 | 黄色免费网站在线播放 | 色悠久久久| 成人午夜做爰视频免费看 | 99久久伊人一区二区yy5099 | 国产乱人乱精一区二区视频密 | 久本草在线中文字幕亚洲欧美 | 亚洲国产日韩在线人成蜜芽 | 欧美人妖xxx | 亚洲欧美韩日 | 2021久久精品99精品久久 | 女女同性一区二区三区四区 |