SoFunction
Updated on 2025-02-28

PHP upload image class display thumbnail function

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 typeprotected $filepath;//File upload pathprotected $filemax;// File upload sizeprotected $fileExt;//File upload formatprotected $filename;//file nameprotected $fileerror;//File error settingprotected $fileflag;//File detectionprotected $fileinfo; //FILES
protected $ext; //File extensionprotected $path;
//Upload filepublic 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 judgmentpublic 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 typepublic function UpMine(){
if(!in_array($this->fileinfo['type'],$this->fileMine)) {
$this->error="File upload type is incorrect";
return false;
}
return true;
}
//Detection of file formatpublic 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 pathpublic function UpPath(){
if(!file_exists($this->filepath)){
mkdir($this->filepath,0777,true);
}
}
//Detection of file sizepublic 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 HTTPpublic function UpPost(){
if(!is_uploaded_file($this->fileinfo['tmp_name'])){
$this->fileerror="Malicious repayment";
return false;
}
return true;
}
//File name prevents duplicationpublic function Upname(){
return md5(uniqid(microtime(true),true));
}
//Picture thumbnailpublic 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 scalingif(($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 transmissionpublic 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} />";
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Examples</title>
<meta name="description" content="">
<meta name="keywords" content="">
<link href="" rel="stylesheet">
</head>
<body>
<form action="" enctype="multipart/form-data" method="post" >
<input type="text" name="name" /><input type="file" name="file" />
<input type="submit" name='submit' value="submit" >
</form>
</body>
</html>

The above is the relevant knowledge about the PHP upload image display thumbnail function introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!