這篇文章將為大家詳細(xì)講解有關(guān)thinkPHP如何實現(xiàn)上傳圖片及生成縮略圖功能,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
記錄一下在thinkPHP上傳圖片的方法(Upload)和生成縮略圖(Image)的方法.
html頁面form中必須加enctype="multipart/form-data"
<form action="__SELF__" method="post" enctype="multipart/form-data"> <table width="100%"class="cont"> <tr> <td>照片:</td> <td width="20%"><input type="file" name="pic" id="pic" /></td> <td colspan="3"><input class="btn" type="submit" value="提交" /></td> <td> </td> </tr> </table> </form>
php代碼
<?php namespace Admin\Controller; use Org\Util\Date; use Think\Controller; use Think\Image; use Think\Upload; class UserController extends Controller { public function add(){ $user = M('user'); if(!empty($_POST)){ $user = $user->create(); //判斷傳入的圖片有沒有問題 if($_FILES['pic']['error'] == 0){ $config = array( 'rootPath' => './Application/public/image/' // 設(shè)置圖片保存路徑 ); //new一個上傳模型 $upload = new Upload($config); //上傳圖片 $pic = $upload->uploadOne($_FILES['pic']); //將圖片保存到數(shù)據(jù)庫中 $user['big_pic'] = $pic['savepath'].$pic['savename']; //生成縮略圖 $img = new Image(); //大圖片的路徑 $big_img = $upload->rootPath.$user['big_pic']; //打開大圖片 $img->open($big_img); //設(shè)置圖片大小 $img->thumb(200,300); //設(shè)置絕對路徑 $small_img = $upload->rootPath.$pic['savepath'].'small_'.$pic['savename']; //保存 $img->save($small_img); //將圖片名稱存入數(shù)據(jù)庫 $user['img'] = $pic['savepath'].'small_'.$pic['savename']; } $user['create_date'] = date("Y-m-d H:i:s"); $msg = "添加失敗"; if(M("user")->add($user)) $msg = "添加成功"; $this->redirect(show_list,null,3,$msg); } $this->display(); }
關(guān)于“thinkPHP如何實現(xiàn)上傳圖片及生成縮略圖功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
本文題目:thinkPHP如何實現(xiàn)上傳圖片及生成縮略圖功能-創(chuàng)新互聯(lián)
本文URL:http://aaarwkj.com/article12/dsppdc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、網(wǎng)頁設(shè)計公司、網(wǎng)站改版、Google、網(wǎng)站設(shè)計公司、網(wǎng)站營銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容