Home >Backend Development >PHP Tutorial >Example of thinkPHP implementation of uploading images and generating thumbnails

Example of thinkPHP implementation of uploading images and generating thumbnails

不言
不言Original
2018-05-10 10:00:201158browse

This article mainly introduces thinkPHP to implement the functions of uploading pictures and generating thumbnails. It analyzes thinkPHP picture uploading and thumbnail setting, generation, saving, database writing and other related operation skills in the form of examples. Friends in need can refer to the following

The example in this article describes how thinkPHP implements the functions of uploading images and generating thumbnails. Share it with everyone for your reference, the details are as follows:

Record the method of uploading images (Upload) and generating thumbnails (Image) in thinkPHP.

Must add # to the html page 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 code

<?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(&#39;user&#39;);
    if(!empty($_POST)){
      $user = $user->create();
      //判断传入的图片有没有问题
      if($_FILES[&#39;pic&#39;][&#39;error&#39;] == 0){
        $config = array(
          &#39;rootPath&#39;  => &#39;./Application/public/image/&#39; // 设置图片保存路径
        );
        //new一个上传模型
        $upload = new Upload($config);
        //上传图片
        $pic = $upload->uploadOne($_FILES[&#39;pic&#39;]);
        //将图片保存到数据库中
        $user[&#39;big_pic&#39;] = $pic[&#39;savepath&#39;].$pic[&#39;savename&#39;];
        //生成缩略图
        $img = new Image();
        //大图片的路径
        $big_img = $upload->rootPath.$user[&#39;big_pic&#39;];
        //打开大图片
        $img->open($big_img);
        //设置图片大小
        $img->thumb(200,300);
        //设置绝对路径
        $small_img = $upload->rootPath.$pic[&#39;savepath&#39;].&#39;small_&#39;.$pic[&#39;savename&#39;];
        //保存
        $img->save($small_img);
        //将图片名称存入数据库
        $user[&#39;img&#39;] = $pic[&#39;savepath&#39;].&#39;small_&#39;.$pic[&#39;savename&#39;];
      }
      $user[&#39;create_date&#39;] = date("Y-m-d H:i:s");
      $msg = "添加失败";
      if(M("user")->add($user))
        $msg = "添加成功";
      $this->redirect(show_list,null,3,$msg);
    }
    $this->display();
  }

The above is the entire content of this article , please pay attention to the PHP Chinese website for more related content.

Related recommendations:

ThinkPHP implements attachment upload function

##thinkphp implements file upload and file download

The above is the detailed content of Example of thinkPHP implementation of uploading images and generating thumbnails. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn