SoFunction
Updated on 2025-03-02

PHP image upload class instance code (thumbnail image added)

There is a thumbnail function, but it feels not comprehensive and has some problems. Continue to learn and modify it in the future

<form action="<?php $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" method="post" ><input type="text" name="name" /><input type="file" name="file" /><input type="submit" name='submit' value="submit" ></form> 
 
<?php 
/** 
 * Created by PhpStorm. 
 * User: Administrator 
 * Date: 2016/6/28 
 * Time: 21:04 
 */
 
class upload{ 
   protected $fileMine;//File upload type   protected $filepath;//File upload path   protected $filemax;// File upload size   protected $fileExt;//File upload format   protected $filename;//file name   protected $fileerror;//File error setting   protected $fileflag;//File detection   protected $fileinfo; //FILES 
   protected $ext; //File extension   protected $path; 
 
  //Upload file  public function __construct($filename="file",$filemax=20000000,$filepath="./Uploads",$fileflag=true,$fileExt=array('jpg','exe'),$fileMine=array('image/jpeg')) 
  { 
    $this->filename=$filename; 
    $this->fileinfo=$_FILES[$this->filename]; 
    $this->filemax=$filemax; 
    $this->filepath=$filepath; 
    $this->fileflag=$fileflag; 
    $this->fileExt=$fileExt; 
    $this->fileMine=$fileMine; 
 
    //var_dump($this->filename); 
 
  } 
 
  //Incorrect judgment  public function UpError(){ 
 
      if($this->fileinfo['error']>0){ 
        switch($this->fileinfo['error']) 
        { 
          case 1: 
          $this->fileerror="The upload file size exceeds the maximum value allowed by the server to upload, set the value of the upload_max_filesize option limit in "; 
            break; 
          case 2: 
            $this->fileerror="The upload file size exceeds the value specified by the hidden domain MAX_FILE_SIZE option in the HTML form"; 
            break; 
          case 3: 
            $this->fileerror="The file part is uploaded"; 
            break; 
          case 4: 
            $this->fileerror="No selected upload file"; 
            break; 
          case 5: 
            $this->fileerror="Temporary directory not found"; 
            break; 
          case 6: 
            $this->fileerror="File writing failed"; 
            break; 
          case 7: 
            $this->fileerror="The php file upload extension is not opened"; 
            break; 
          case 8: 
            $this->fileerror=""; 
            break; 
 
        } 
        return false; 
      } 
      return true; 
 
  } 
 
  //Detection of file type  public function UpMine(){ 
    if(!in_array($this->fileinfo['type'],$this->fileMine)) { 
      $this->error="File upload type is incorrect"; 
      return false; 
    } 
    return true; 
 
  } 
  //Detection of file format  public function UpExt(){ 
    $this->ext=pathinfo($this->fileinfo['name'],PATHINFO_EXTENSION); 
    //var_dump($ext); 
    if(!in_array($this->ext,$this->fileExt)){ 
      $this->fileerror="File format is wrong"; 
      return false; 
    } 
    return true; 
  } 
  //Detection of file path  public function UpPath(){ 
    if(!file_exists($this->filepath)){ 
      mkdir($this->filepath,0777,true); 
    } 
  } 
  //Detection of file size  public function UpSize(){ 
    $max=$this->fileinfo['size']; 
    if($max>$this->filemax){ 
      $this->fileerror="The file is too large"; 
      return false; 
    } 
    return true; 
  } 
  //Detect whether the file is HTTP  public function UpPost(){ 
    if(!is_uploaded_file($this->fileinfo['tmp_name'])){ 
      $this->fileerror="Malicious repayment"; 
      return false; 
    } 
    return true; 
  } 
  //File name prevents duplication  public function Upname(){ 
    return md5(uniqid(microtime(true),true)); 
  } 
 
  //Picture thumbnail  public function Smallimg($x=100,$y=100){ 
    $imgAtt=getimagesize($this->path); 
    //Image width, height, type    $imgWidth=$imgAtt[0]; 
    $imgHeight=$imgAtt[1]; 
    $imgext=$imgAtt[2]; 
    //Equi-parameter scaling 
    if(($x/$imgWidth)>($y/$imgHeight)){ 
      $bl=$y/$imgHeight; 
    }else{ 
      $bl=$x/$imgWidth; 
    } 
    $x=floor($imgWidth*$bl); //After zoom    $y=floor($imgHeight*$bl); 
    $images=imagecreatetruecolor($x,$y); 
    $big=imagecreatefromjpeg($this->path); 
    imagecopyresized($images,$big,0,0,0,0,$x,$y,$imgWidth,$imgWidth); 
    switch($imgext){ 
      case 1: 
        $imageout=imagecreatefromgif($this->path); 
        break; 
      case 2: 
        $imageout=imagecreatefromjpeg($this->path); 
        break; 
      case 3: 
        $imageout=imagecreatefromgif($this->path); 
        break; 
    } 
    $im=imagejpeg($images,$this->path); 
 
 
 
 
  } 
 
  //Double file transmission  public function uploads() 
  { 
    if($this->UpError()&&$this->UpMine()&&$this->UpExt()&&$this->UpSize()&&$this->UpPost()){ 
      $this->UpPath(); 
      $names=$this->Upname(); 
      $this->path=$this->filepath.'/'. $names.'.'.$this->ext; 
 
      if(move_uploaded_file($this->fileinfo['tmp_name'], $this->path)){ 
        return $this->path; 
      }else{ 
        $this->fileerror="Upload failed"; 
      } 
    }else{ 
      exit("<b>".$this->fileerror."</b>"); 
    } 
  } 
 
 
} 
 
 
?>
<?php 
  header("content-type:imagejpeg"); 
header("Content-type:text/html;charset=utf-8"); 
 require ''; 
 $u=new upload(); 
 $a=$u->uploads(); 
 
 $c=$u->Smallimg(); 
echo "<img src={$a} />"; 
echo "<img src={$c} />"; 
 
?> 
&lt;!DOCTYPE html&gt; 
&lt;html&gt; 
&lt;head&gt; 
&lt;meta charset="utf-8"&gt; 
&lt;meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"&gt; 
&lt;title&gt;Examples&lt;/title&gt; 
&lt;meta name="description" content=""&gt; 
&lt;meta name="keywords" content=""&gt; 
&lt;link href="" rel="stylesheet"&gt; 
&lt;/head&gt; 
&lt;body&gt; 
  &lt;form action="" enctype="multipart/form-data" method="post" &gt; 
  &lt;input type="text" name="name" /&gt;&lt;input type="file" name="file" /&gt; 
  &lt;input type="submit" name='submit' value="submit" &gt; 
  &lt;/form&gt; 
&lt;/body&gt; 
&lt;/html&gt;

The above PHP image upload example code (with thumbnails added) is all the content I share with you. I hope you can give you a reference and I hope you can support me more.