Home Backend Development PHP Tutorial Implementation example of tp uploading pictures and generating thumbnails function

Implementation example of tp uploading pictures and generating thumbnails function

Oct 30, 2017 am 09:13 AM
Function generate

The example of this article describes how thinkPHP implements the functions of uploading pictures 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.

In the html page form Must add 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>
Copy after login

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('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']);
        //将图片保存到数据库中
        $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);
        //将图片名称存入数据库
        $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();
  }
Copy after login

The above is the detailed content of Implementation example of tp uploading pictures and generating thumbnails function. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What functions does Doubao app have? What functions does Doubao app have? Mar 01, 2024 pm 10:04 PM

What functions does Doubao app have?

The difference between vivox100s and x100: performance comparison and function analysis The difference between vivox100s and x100: performance comparison and function analysis Mar 23, 2024 pm 10:27 PM

The difference between vivox100s and x100: performance comparison and function analysis

What is Discuz? Definition and function introduction of Discuz What is Discuz? Definition and function introduction of Discuz Mar 03, 2024 am 10:33 AM

What is Discuz? Definition and function introduction of Discuz

Comparative analysis of the functions and performance of JPA and MyBatis Comparative analysis of the functions and performance of JPA and MyBatis Feb 19, 2024 pm 05:43 PM

Comparative analysis of the functions and performance of JPA and MyBatis

What exactly is self-media? What are its main features and functions? What exactly is self-media? What are its main features and functions? Mar 21, 2024 pm 08:21 PM

What exactly is self-media? What are its main features and functions?

PHP Tips: Quickly Implement Return to Previous Page Function PHP Tips: Quickly Implement Return to Previous Page Function Mar 09, 2024 am 08:21 AM

PHP Tips: Quickly Implement Return to Previous Page Function

What is a Bluetooth adapter used for? What is a Bluetooth adapter used for? Feb 19, 2024 pm 05:22 PM

What is a Bluetooth adapter used for?

What are the functions of Xiaohongshu account management software? How to operate a Xiaohongshu account? What are the functions of Xiaohongshu account management software? How to operate a Xiaohongshu account? Mar 21, 2024 pm 04:16 PM

What are the functions of Xiaohongshu account management software? How to operate a Xiaohongshu account?

See all articles