PHP實(shí)現(xiàn)圖片上傳并壓縮
本文實(shí)例講解了PHP圖片上傳并壓縮的實(shí)現(xiàn)方法,分享給大家供大家參考,具體內(nèi)容如下
使用到三個(gè)文件
- connect.php:連接數(shù)據(jù)庫
- test_upload.php:執(zhí)行SQL語句
- upload_img.php:上傳圖片并壓縮
三個(gè)文件代碼如下:
連接數(shù)據(jù)庫: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: %s\n", mysqli_connect_error()); exit(); } ?>
執(zhí)行SQL語句:test_upload.php
<?php require ("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 //設(shè)置文件保存目錄 $uploaddir = "upfiles/"; //設(shè)置允許上傳文件的類型 $type=array("jpg","gif","bmp","jpeg","png"); //獲取文件后綴名函數(shù) function fileext($filename) { return substr(strrchr($filename, '.'), 1); } //生成隨機(jī)文件名函數(shù) 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;//文件類型錯(cuò)誤 $page_result=$text; $retArray = array('ret_code' => $ret_code,'page_result'=>$page_result); $retJson = json_encode($retArray); echo $retJson; return; } //生成目標(biāo)文件的文件名 else { $filename=explode(".",$_FILES['filename']['name']); do { $filename[0]=random(10); //設(shè)置隨機(jī)數(shù)長度 $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; //以上與下面段注釋可以聯(lián)合使用,可以使圖片根據(jù)計(jì)算出來的比例壓縮 $file_type=$_FILES["filename"]['type']; function ResizeImage($uploadfile,$maxwidth,$maxheight,$name) { //取得當(dāng)前圖片大小 $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//默認(rèn)jpg { $im = imagecreatefromjpeg($uploadfile); } if($im) { ResizeImage($im,$pic_width_max,$pic_height_max,$uploadfile_resize); ImageDestroy ($im); } } ?>
請(qǐng)按照現(xiàn)實(shí)情況更改connect.php,test_upload.php中對(duì)應(yīng)的信息。
以上就是PHP實(shí)現(xiàn)圖片上傳并壓縮的方法,希望對(duì)大家的學(xué)習(xí)php程序設(shè)計(jì)有所幫助
相關(guān)文章
PHP實(shí)現(xiàn)將多個(gè)文件中的內(nèi)容合并為新文件的方法示例
這篇文章主要介紹了PHP實(shí)現(xiàn)將多個(gè)文件中的內(nèi)容合并為新文件的方法,涉及php編碼轉(zhuǎn)換、文件與目錄的遍歷以及文件讀寫相關(guān)操作技巧,需要的朋友可以參考下2017-06-06PHP基于關(guān)聯(lián)數(shù)組20行代碼搞定約瑟夫問題示例
這篇文章主要介紹了PHP基于關(guān)聯(lián)數(shù)組20行代碼搞定約瑟夫問題,結(jié)合具體實(shí)例分析了php使用關(guān)聯(lián)數(shù)組解決約瑟夫問題的相關(guān)操作技巧,需要的朋友可以參考下2017-11-11php+ajax簡單實(shí)現(xiàn)全選刪除的方法
這篇文章主要介紹了php+ajax簡單實(shí)現(xiàn)全選刪除的方法,結(jié)合實(shí)例形式分析了html+js前臺(tái)全選及通過ajax與后臺(tái)php交互實(shí)現(xiàn)批量刪除的具體操作步驟與相關(guān)技巧,需要的朋友可以參考下2016-12-12php基于表單密碼驗(yàn)證與HTTP驗(yàn)證用法實(shí)例
這篇文章主要介紹了php基于表單密碼驗(yàn)證與HTTP驗(yàn)證用法,以實(shí)例形式較為詳細(xì)的分析了表單密碼驗(yàn)證與HTTP驗(yàn)證的原理與相關(guān)注意事項(xiàng),具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-01-01php sprintf()函數(shù)讓你的sql操作更安全
本函數(shù)用來將字符串格式化。參數(shù) format 是轉(zhuǎn)換的格式,以百分比符號(hào) % 開始到轉(zhuǎn)換字符為止。而在轉(zhuǎn)換的格式間依序包括了2008-07-07PHP ajax 異步執(zhí)行不等待執(zhí)行結(jié)果的處理方法
這篇文章主要介紹了PHP ajax 異步執(zhí)行不等待執(zhí)行結(jié)果的處理方法,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-05-05