ThinkPHP5.0 Image upload to generate thumbnail example code description

  • 2021-10-24 19:11:48
  • OfStack

Many friends encountered such a problem, pictures uploaded to generate thumbnails, and many people tested successfully on this machine (win), but made errors after uploading to linux server.

I have the same problem. On-line 1 check, there are countless people said that the server temporary file directory authority problem.

After several thoughts, I found that this is not the case.

The fundamental reason is that the information saved to the variable is the previous one, and then move moved to its specified directory, and the temporary file no longer exists. Therefore, when generating thumbnails again, open is needed, and the file address should be the directory + file name defined by oneself. However, in many instance documents, the information before move is used.

In addition, under win server, after move, files have been generated in the specified directory, and temporary files have not been deleted. Therefore, thumbnails can be generated with the information before move.

I hope a few words can help you with the same problem.

The following is an example code to introduce the method of uploading thumbnails from ThinkPHP 5.0 pictures.

The code looks like this:


<?php
namespace app\common\controller;
use app\common\model\Goods;
class Tools
{
 public static function upload_goods_img($whereName="", $width="", $height="")
 {
 //  Open the relative path of the picture 
 $imgpath = config('img_path');
 //  Absolute path 
 $imgRootPath = config('imgRootPath');
 $storeId = ' Customize ';
 $merchantId = ' Customize ';
 $old_filename = $storeId . $merchantId . time();
 $filename = $storeId . $merchantId . time() . mt_rand(1000, 9999);
 $type = Goods::upload($whereName, $old_filename);
 if($type) 
 {
  $savepath = $imgRootPath . '/' . $whereName . '/' . $filename . '.' . $type;
  $thumbfile = $filename . '.' . $type;
  $thumbName = $imgpath . '/' . $whereName . '/' . $thumbfile;
  $image = \think\Image::open($imgpath . '/'. $whereName .'/' . $old_filename . '.' . $type);
  $image->thumb($width, $height, \think\Image::THUMB_FIXED)->save($thumbName);
  $data = [
  'access_url' => $imgRootPath . '/' . $whereName . '/' . $filename . '.' . $type,
  'filename' => $thumbfile,
  ];
  return $data;
 }
 } 
}

Call:


class Goods
{
 public function upload_sku()
 {
 $whereName = 'goods/sku';
 $width = 750;
 $height = 750;
 $data = Tools::upload_goods_img($whereName,$width, $height);
 return returnJson(1, ' Upload succeeded ', $data);;
 }
}

PS: Let's look at the method of uploading pictures in 1 code tp5 and generate corresponding thumbnails


// Object that receives the uploaded file name
$file = $this->_req->file("upload_head_image");
// Move uploaded files to public/uploads/user
$info = $file->validate(['size'=>5242880,'ext'=>'jpg,jpeg,png'])->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'user');
if($info){
 $pic = new \app\home\model\User();
 $pic_url = $pic->thumbImage($file,$info);
 $user['portrait'] = 'uploads/user/'.$pic_url;
 //print_r($pic_url);exit();
 }

///model The code in is as follows 
 /**
 * [ Generate thumbnails of user avatars, 180 , 50]
 * @param [type] $file [ Get uploaded file $_FILE]
 * @param [type] $pic [ Path to upload file ]
 * @return [type] [ Returns the processed file path ]
 */
 public function thumbImage($file,$pic){
 $image = \think\Image::open($file);
 $getSaveName = str_replace('\\','/',$pic->getSaveName());
$portrait_thumbnail_180= 'uploads/user/'.str_replace($pic->getFilename(),'180_'.$pic->getFilename(),$getSaveName);
$image->thumb(180,180,\think\Image::THUMB_CENTER)->save(ROOT_PATH . 'public' . DS . $portrait_thumbnail_180,null,100,true);
 $portrait_thumbnail_80 = 'uploads/user/'.str_replace($pic->getFilename(),'80_'.$pic->getFilename(),$getSaveName);
 $image->thumb(80,80,\think\Image::THUMB_CENTER)->save(ROOT_PATH . 'public' . DS . $portrait_thumbnail_80,null,100,true);
 $portrait_thumbnail_50 = 'uploads/user/'.str_replace($pic->getFilename(),'50_'.$pic->getFilename(),$getSaveName);
 $image->thumb(50,50,\think\Image::THUMB_CENTER)->save(ROOT_PATH . 'public' . DS . $portrait_thumbnail_50,null,100,true);
if ($image) {
  return $getSaveName;
 }
 }

Summarize


Related articles: