PHP实现图片上传并生成缩略图

解决上传文件失败后,网上找了一段不错的(比较全面且严谨)PHP后台实现文件处理的代码,并解决原来代码中两个错误后记录如下:
需要修改的字段有$filename,$filemax,$filepath,其余基本不需要再做修改了

<?php

class upload{
    protected $filename;//文件名
    protected $fileMine;//文件上传类型
    protected $filepath;//文件上传路径
    protected $filemax;//文件上传大小
    protected $fileExt;//文件上传格式
    protected $fileerror;//文件出错设置
    protected $fileflag;//文件检测
    protected $fileinfo; //FILES
    protected $ext; //文件扩展
    protected $path;
//文件上传
    public function __construct($filename="N_File",$filemax=5000000,$filepath="Notices",$fileflag=true,$fileExt=array('gif','jpeg','pjpeg','jpg','png'),$fileMine=array('image/gif','image/jpeg','image/pjpeg','image/jpg','image/png'))
    {
        $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="上传文件大小超过服务器允许上传的最大值,php.ini中设置upload_max_filesize选项限制的值 ";
                    break;
                case 2:
                    $this->fileerror="上传文件大小超过HTML表单中隐藏域MAX_FILE_SIZE选项指定的值";
                    break;
                case 3:
                    $this->fileerror="文件部分被上传";
                    break;
                case 4:
                    $this->fileerror="没有选择上传文件";
                    break;
                case 5:
                    $this->fileerror="未找到临时目录";
                    break;
                case 6:
                    $this->fileerror="文件写入失败";
                    break;
                case 7:
                    $this->fileerror="php文件上传扩展没有打开";
                    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(){
        $mtime=date('Y-m-d');
        $this->filepath=$this->filepath.'/'.$mtime;
        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=300,$y=300){
        $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);
        switch($imgext){
            case 1:
                $imageout=imagecreatefromgif($this->path);
                break;
            case 2:
                $imageout=imagecreatefromjpeg($this->path);
                break;
            case 3:
                $imageout=imagecreatefrompng($this->path);
                break;
            default:
                $imageout=imagecreatefromjpeg($this->path);
        }
        imagecopyresized($images,$imageout,0,0,0,0,$x,$y,$imgWidth,$imgHeight);
        $names=$this->Upname();
        $this->path=$this->filepath.'/'. $names.'.'.$this->ext;
        imagejpeg($images,$this->path);
        return $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="上传失败";
            }
        }else{
            exit("<b>".$this->fileerror."</b>");
        }
    }
}
?>

调用很简单,在文件上传检查的PHP页面加入:

    $u=new upload();
    $a=$u->uploads();
    $c=$u->Smallimg();
    echo "<a href='$a'><img src='$c' /></a>"
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 195,980评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,422评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,130评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,553评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,408评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,326评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,720评论 3 386
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,373评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,678评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,722评论 2 312
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,486评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,335评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,738评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,009评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,283评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,692评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,893评论 2 335

推荐阅读更多精彩内容

  • iOS开发系列--网络开发 概览 大部分应用程序都或多或少会牵扯到网络开发,例如说新浪微博、微信等,这些应用本身可...
    lichengjin阅读 3,626评论 2 7
  • Php:脚本语言,网站建设,服务器端运行 PHP定义:一种服务器端的HTML脚本/编程语言,是一种简单的、面向对象...
    廖马儿阅读 2,112评论 2 38
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,135评论 25 707
  • 本文包括:1、文件上传概述2、利用 Commons-fileupload 组件实现文件上传3、核心API——Dis...
    廖少少阅读 12,495评论 5 91
  • 做个简单幸福的人,不再奢求其他!
    优素阅读 129评论 0 0