PHP之图片上传类实例代码(加了缩略图)

前端之家收集整理的这篇文章主要介绍了PHP之图片上传类实例代码(加了缩略图)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

缩略图功能 但是 感觉不全面,而且有点问题,继续学习,将来以后修改

PHP;">

<?php
/**

  • Created by PhpStorm.
  • User: Administrator
  • Date: 2016/6/28
  • Time: 21:04
    */

class upload{
protected $fileMine;//文件上传类型
protected $filepath;//文件上传路径
protected $filemax;//文件上传大小
protected $fileExt;//文件上传格式
protected $filename;//文件名
protected $fileerror;//文件出错设置
protected $fileflag;//文件检测
protected $fileinfo; //FILES
protected $ext; //文件扩展
protected $path;

//文件上传
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); 

}

//错误判断
public function UpError(){

  if($this->fileinfo['error']>0){ 
    switch($this->fileinfo['error']) 
    { 
      case 1: 
      $this->fileerror="<a href="https://www.jb51.cc/tag/shangchuan/" target="_blank" class="keywords">上传</a><a href="https://www.jb51.cc/tag/wenjian/" target="_blank" class="keywords">文件</a>大小超过服务器允许<a href="https://www.jb51.cc/tag/shangchuan/" target="_blank" class="keywords">上传</a>的最大值,<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>.ini中设置upload_max_filesize选项限制的值 "; 
        break; 
      case 2: 
        $this->fileerror="<a href="https://www.jb51.cc/tag/shangchuan/" target="_blank" class="keywords">上传</a><a href="https://www.jb51.cc/tag/wenjian/" target="_blank" class="keywords">文件</a>大小超过HTML表单中隐藏域MAX_FILE_SIZE选项指定的值"; 
        break; 
      case 3: 
        $this->fileerror="<a href="https://www.jb51.cc/tag/wenjian/" target="_blank" class="keywords">文件</a>部分被<a href="https://www.jb51.cc/tag/shangchuan/" target="_blank" class="keywords">上传</a>"; 
        break; 
      case 4: 
        $this->fileerror="没有选择<a href="https://www.jb51.cc/tag/shangchuan/" target="_blank" class="keywords">上传</a><a href="https://www.jb51.cc/tag/wenjian/" target="_blank" class="keywords">文件</a>"; 
        break; 
      case 5: 
        $this->fileerror="未找到临时目录"; 
        break; 
      case 6: 
        $this->fileerror="<a href="https://www.jb51.cc/tag/wenjian/" target="_blank" class="keywords">文件</a>写入失败"; 
        break; 
      case 7: 
        $this->fileerror="<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a><a href="https://www.jb51.cc/tag/wenjian/" target="_blank" class="keywords">文件</a><a href="https://www.jb51.cc/tag/shangchuan/" target="_blank" class="keywords">上传</a>扩展没有打开 "; 
        break; 
      case 8: 
        $this->fileerror=""; 
        break; 

    } 
    return false; 
  } 
  return true; 

}

//检测文件类型
public function UpMine(){
if(!in_array($this->fileinfo['type'],$this->fileMine)) {
$this->error="文件上传类型不对";
return false;
}
return true;

}
//检测文件格式
public function UpExt(){
$this->ext=pathinfo($this->fileinfo['name'],PATHINFO_EXTENSION);
//var_dump($ext);
if(!in_array($this->ext,$this->fileExt)){
$this->fileerror="文件格式不对";
return false;
}
return true;
}
//检测文件路径
public function UpPath(){
if(!file_exists($this->filepath)){
mkdir($this->filepath,0777,true);
}
}
//检测文件大小
public function UpSize(){
$max=$this->fileinfo['size'];
if($max>$this->filemax){
$this->fileerror="文件过大";
return false;
}
return true;
}
//检测文件是否HTTP
public function UpPost(){
if(!is_uploaded_file($this->fileinfo['tmp_name'])){
$this->fileerror="恶意上偿还";
return false;
}
return true;
}
//文件名防止重复
public function Upname(){
return md5(uniqid(microtime(true),true));
}

//图片缩略图
public function Smallimg($x=100,$y=100){
$imgAtt=getimagesize($this->path);
//图像宽,高,类型
$imgWidth=$imgAtt[0];
$imgHeight=$imgAtt[1];
$imgext=$imgAtt[2];
//等比列缩放

if(($x/$imgWidth)>($y/$imgHeight)){ 
  $bl=$y/$imgHeight; 
}else{ 
  $bl=$x/$imgWidth; 
} 
$x=floor($imgWidth*$bl); //缩放后 
$y=floor($imgHeight*$bl); 
$images=imagecreatetruecolor($x,$y); 
$big=imagecreatefromjpeg($this->path); 
imagecopyresized($images,$big,$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); 

}

//文件双传
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="<a href="https://www.jb51.cc/tag/shangchuan/" target="_blank" class="keywords">上传</a>失败"; 
  } 
}else{ 
  exit("<b>".$this->fileerror."</b>"); 
} 

}

}

?>

PHP;"> uploads();

$c=$u->Smallimg();
echo "";
echo "";

?>

PHP;"> <Meta charset="utf-8"> <Meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> Examples <Meta name="description" content=""> <Meta name="keywords" content="">

以上这篇PHP图片上传类实例代码(加了缩略图)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程之家。

原文链接:https://www.f2er.com/php/19467.html

猜你在找的PHP相关文章