ONE'S 博客

  • 首页
  • 生活
  • 工作
  • 爱好
    • C #
    • javascript
    • Linux
    • PHP编程
    • python
    • thinkphp
    • 旅游风景
    • 电脑网络
    • 脑力运动
    • 瞎编乱造
  • 书籍
  • 经验
  • 健康
  • 交易
  • 时间
  • 作品
  • 小说
Kratos
专注于用户阅读体验的响应式博客主题
  1. 首页
  2. 爱好拓展
  3. PHP编程
  4. 正文

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

2019年8月7日 791点热度 0人点赞 0条评论

两种上传图片的方法都已经验证

首先index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="action.php" method="post" enctype="multipart/form-data">
    请选择您要上传的文件:<input type="file" name='myFile1' />
    <input type="submit" value="上传文件" />
</form>
</body>
</html>

action.php

<?php
header('content-type:text/html;charset=utf-8');
require_once 'upload.php';
$u=new upload('myFile1','500000','upload');
$a=$u->uploads();
$c=$u->Smallimg();
echo "<a href='$a'><img src='$c' /></a>";

upload.php

<?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>");
        }
    }
}

标签: 图片上传
最后更新:2019年8月7日

飞翔的mouse

这个人很懒,什么都没留下

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

飞翔的mouse

这个人很懒,什么都没留下

分类目录
  • C #
  • javascript
  • Linux
  • PHP编程
  • python
  • thinkphp
  • 书籍摘录
  • 健康医药
  • 工作感悟
  • 心理咨询
  • 投资理财
  • 旅游风景
  • 时间管理
  • 未分类
  • 生活情感
  • 电脑网络
  • 瞎编乱造
  • 经验分享
  • 脑力运动
  • 软件作品
最新 热点 随机
最新 热点 随机
远离负能量之人 废掉一个人最隐蔽的方式:让他吃低级的苦 男人为什么越来越现实 2023成人高考禁止携带手机 巡察工作经验所得 99%是钱的问题
男人为什么越来越现实远离负能量之人废掉一个人最隐蔽的方式:让他吃低级的苦
烟,这个东西,果真烦人 明天会计证考试 网站百度正常收录开始 使用dedecms自带采集功能的文字过滤与替换的方法 距离未尝不好 腾讯云购买的一款短信套餐使用方式
最近评论
飞翔的mouse 发布于 1 年前(11月12日) 好像没!!!
涛涛 发布于 1 年前(11月11日) 中了没有?
daxi 发布于 5 年前(11月04日) 学无止境,认真拜读!
飞翔的mouse 发布于 7 年前(06月15日) 在手机浏览的情况下,文章内的图片拉伸我的站有问题,你的站没有。求解决方案呀MJ
M.J 发布于 7 年前(06月15日) @飞翔的mouse 哈哈哈哈,谢谢支持,我这程序猿
标签聚合
学习 计划 学习计划 工作 会计证 php 生活 从业资格

COPYRIGHT © 2023 ONE'S 博客. ALL RIGHTS RESERVED.