This paper describes the thinkphp implementation of uploading images and generating thumbnails. Share to everyone for your reference, as follows:
Make a note of how to upload a picture in thinkphp (Upload) and generate a thumbnail image (image).
enctype= "Multipart/form-data" must be added to the HTML page form
<form action= "Self" method= "post" enctype= "Multipart/form-data" > <table width= "100%" class= "cont" > <tr> <td> photos:</td> <td width= "20%" ><input type= "file" name= "pic" id= "pic"/> </td> <td colspan= "3" ><input class= "btn" type= "Submit" value= "Submit"/></td> <TD > </td> </tr> </table></form>
PHP code
<?phpnamespace 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 (); Determine if the incoming picture has a problem if ($_files[' pic ' [' error '] = = 0) {$config = array (' rootpath ' = './application/pu blic/image/'//Set picture save path); New an upload model $upload = new Upload ($config); Upload image $pic = $upload->uploadone ($_files[' pic '); Save the picture to the database $user [' big_pic '] = $pic [' Savepath ']. $pic [' Savename ']; Generate thumbnail $img = new Image (); The path to the large picture $big _img = $upload->rootpath. $user [' big_pic ']; Open large picture $img->open ($big _img); Set the picture size $img->thumb (200,300); Set absolute path $small _img = $upload->rootpath. $pic [' Savepath ']. ' Small_ '. $pic [' Savename ']; Save $img->save ($small _img); Save picture Name to database $user [' img '] = $pic [' Savepath ']. ' Small_ '. $pic [' Savename ']; } $user [' create_date '] = Date ("y-m-d h:i:s"); $msg = "Add Failed"; if (M ("User")->add ($user)) $msg = "Add Success"; $this->redirect (show_list,null,3, $msg); } $this->display (); }