THINKPHP5 uploading images and generating thumbnail image methods

Source: Internet
Author: User
This article is mainly for you to share a thinkphp5 upload pictures and generate thumbnails public methods, has a good reference value, I hope to be helpful to everyone. Follow the small series together to see it, hope to help everyone.

Directly on the code, can be written in the common file common and inheritance of the base class, convenient to call

/* * $name The name value that is uploaded for the form * $filePath the folder names below uploads/for saving in the Portal folder public, no words will be automatically created * $width Specify the thumbnail width * $height Specify the thumbnail height * from  Generated thumbnails are saved in the Thumb folder under the $filepath folder, automatically created * @return array one is the picture path, one is the thumbnail path, as follows: * Array (2) {["img"] and string (57) "Uploads/img/20171211\3d4ca4098a8fb0f90e5f53fd7cd71535.jpg" ["thumb_img"] + string (+) "uploads/img/thumb/     20171211/3d4ca4098a8fb0f90e5f53fd7cd71535.jpg "} */Protected function UploadFile ($name, $filePath, $width, $height) {    $file = Request ()->file ($name); if ($file) {$filePaths = Root_path. ' Public '. Ds. ' Uploads '.      DS. $filePath;      if (!file_exists ($filePaths)) {mkdir ($filePaths, 0777,true);      } $info = $file->move ($filePaths); if ($info) {$imgpath = ' uploads/'. $filePath. '        /'. $info->getsavename ();        $image = \think\image::open ($imgpath); $date _path = ' uploads/'. $filePath. '        /thumb/'. Date (' Ymd ');      if (!file_exists ($date _path)) {mkdir ($date _path,0777,true);  } $thumb _path = $date _path. '        /'. $info->getfilename ();        $image->thumb ($width, $height)->save ($thumb _path);        $data [' img '] = $imgpath;        $data [' thumb_img '] = $thumb _path;      return $data;      }else{//upload failed to get error message return $file->geterror (); }    }  }

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.