Example of implementation of TP upload image and generate thumbnail function

Source: Internet
Author: User
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 (); }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.