thinkPHP實現(xià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/' // 設置圖片保存路徑 ); //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); //設置圖片大小 $img->thumb(200,300); //設置絕對路徑 $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(); }
更多關于thinkPHP相關內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結》、《ThinkPHP常用方法總結》、《codeigniter入門教程》、《CI(CodeIgniter)框架進階教程》、《Zend FrameWork框架入門教程》及《PHP模板技術總結》。
希望本文所述對大家基于ThinkPHP框架的PHP程序設計有所幫助。
相關文章
PHP經(jīng)典面試題之設計模式(經(jīng)常遇到)
php中設計模式非常多,但是設計模式在php面試題經(jīng)常會提到,本文主要給大家介紹php經(jīng)典面試題之設計模式,需要的朋友一起看看吧2015-10-10關于laravel-admin ueditor 集成并解決刷新的問題
今天小編就為大家分享一篇關于laravel-admin ueditor 集成并解決刷新的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10PHP+Mysql+Ajax+JS實現(xiàn)省市區(qū)三級聯(lián)動
最近做了個項目,需要用到省市區(qū)三級聯(lián)動,上網(wǎng)翻了不少資料,于是有了下面的思路和代碼2014-05-05