Table of Contents
您可能感兴趣的文章:
Home Backend Development PHP Tutorial thinkphp5 public methods for uploading images and generating thumbnails

thinkphp5 public methods for uploading images and generating thumbnails

Jul 03, 2018 pm 06:01 PM
thinkphp5 upload image Generate thumbnails

下面小编就为大家分享一篇thinkphp5上传图片及生成缩略图公共方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

直接上代码,可以写在公共文件common和继承的基础类中,方便调用

/*
   * $name为表单上传的name值
   * $filePath为为保存在入口文件夹public下面uploads/下面的文件夹名称,没有的话会自动创建
   * $width指定缩略宽度
   * $height指定缩略高度
   * 自动生成的缩略图保存在$filePath文件夹下面的thumb文件夹里,自动创建
   * @return array 一个是图片路径,一个是缩略图路径,如下:
   * array(2) {
     ["img"] => string(57) "uploads/img/20171211\3d4ca4098a8fb0f90e5f53fd7cd71535.jpg"
     ["thumb_img"] => string(63) "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{
        // 上传失败获取错误信息
        return $file->getError();
      }
    }
  }
Copy after login

以上这篇thinkphp5上传图片及生成缩略图公共方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持php中文网。

您可能感兴趣的文章:

微信小程序之支付后调用SDK的异步通知及验证处理订单方法的详解

PHP 使用Echarts生成数据统计报表的实现

PHP根据手机号判断运营商

The above is the detailed content of thinkphp5 public methods for uploading images and generating thumbnails. 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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

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 should I do if I get an error when deploying thinkphp5 in Pagoda? What should I do if I get an error when deploying thinkphp5 in Pagoda? Dec 19, 2022 am 11:04 AM

Solution to the error reported when deploying thinkphp5 in Pagoda: 1. Open the Pagoda server, install the php pathinfo extension and enable it; 2. Configure the ".access" file with the content "RewriteRule ^(.*)$ index.php?s=/$1 [QSA ,PT,L]”; 3. In website management, just enable thinkphp’s pseudo-static.

What should I do if thinkphp5 url rewriting fails? What should I do if thinkphp5 url rewriting fails? Dec 12, 2022 am 09:31 AM

Solution to thinkphp5 url rewriting not working: 1. Check whether the mod_rewrite.so module is loaded in the httpd.conf configuration file; 2. Change None in AllowOverride None to All; 3. Modify the Apache configuration file .htaccess to "RewriteRule ^ (.*)$ index.php [L,E=PATH_INFO:$1]" and save it.

How to get the requested URL in thinkphp5 How to get the requested URL in thinkphp5 Dec 20, 2022 am 09:48 AM

Methods for thinkphp5 to obtain the requested URL: 1. Use the "$request = Request::instance();" method of the "\think\Request" class to obtain the current URL information; 2. Use the built-in helper function "$request-> url()" to obtain the complete URL address including the domain name.

How to remove thinkphp5 title bar icon How to remove thinkphp5 title bar icon Dec 20, 2022 am 09:24 AM

How to remove the thinkphp5 title bar icon: 1. Find the favicon.ico file under the thinkphp5 framework public; 2. Delete the file or choose another picture to rename it to favicon.ico and replace the original favicon.ico file.

What should I do if thinkphp5 post cannot get the value? What should I do if thinkphp5 post cannot get the value? Dec 06, 2022 am 09:29 AM

thinkphp5 post cannot get a value because TP5 uses the strpos function to find the app/json string in the content-type value of the Header. The solution is to set the content-type value of the Header to app/json.

How to post pictures on Douyin comments How to post pictures on Douyin comments Feb 19, 2024 pm 01:10 PM

As one of the most popular short video sharing platforms in the world, Douyin has attracted hundreds of millions of users to join it. When looking at other people's wonderful works, we are often moved by some dynamic, interesting or meaningful moments in them. At this time, we can not only express our opinions and thoughts through text comments, but also express our emotions more vividly through picture comments. So, how to post picture comments on TikTok? First, open the Douyin APP and enter the video you are interested in. Next, we need to determine the operating system of the mobile phone according to the different

How to generate thumbnails after PHP saves remote images to local? How to generate thumbnails after PHP saves remote images to local? Jul 13, 2023 pm 04:28 PM

How to generate thumbnails after PHP saves remote images to local? When developing a website or application, you often encounter situations where you need to save remote images to the local server, and also need to generate thumbnails to improve page loading speed and save bandwidth. This article will introduce how to use PHP to save remote images to local and use the GD library to generate thumbnails. Download remote images to the local server. In PHP, you can use the file_get_contents() function to read the contents of the remote image, and then use file_pu

What should I do if thinkphp5 prompts that the controller does not exist? What should I do if thinkphp5 prompts that the controller does not exist? Dec 06, 2022 am 10:43 AM

Solution to thinkphp5 prompting that the controller does not exist: 1. Check whether the namespace in the corresponding controller is written correctly and change it to the correct namespace; 2. Open the corresponding tp file and modify the class name.

See all articles