PHP上传图片类显示缩略图功能

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

<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="提交" ></form>
<?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="上传文件大小超过服务器允许上传的最大值,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(){
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,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);
}
//文件双传
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
header("content-type:imagejpeg");
header("Content-type:text/html;charset=utf-8");
require 'list.php';
$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="ce.php" enctype="multipart/form-data" method="post" >
<input type="text" name="name" /><input type="file" name="file" />
<input type="submit" name='submit' value="提交" >
</form>
</body>
</html>

以上所述是小编给大家介绍的PHP上传图片类显示缩略图功能的相关知识,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!

(0)

相关推荐

  • php上传图片类及用法示例

    本文实例讲述了php上传图片类及用法.分享给大家供大家参考,具体如下: 1.类文件名为:upclass.php <?php class upclass{ public $previewsize=0.125 ; //预览图片比例 public $preview=0; //是否生成预览,是为1,否为0 public $datetime; //随机数 public $ph_name; //上传图片文件名 public $ph_tmp_name; //图片临时文件名 public $ph_path=&quo

  • 超级好用的一个php上传图片类(随机名,缩略图,加水印)

    Upimages.class.php php上传类 复制代码 代码如下: <?php class UpImages { var $annexFolder = "upload";//附件存放点,默认为:annex var $smallFolder = "small";//缩略图存放路径,注:必须是放在 $annexFolder下的子目录,默认为:smallimg var $markFolder = "mark";//水印图片存放处 var $

  • php图片上传类 附调用方法

    本文实例为大家分享php图片上传类,供大家参考,具体内容如下 调用方法: <?php header("Content-Type:text/html; charset=utf-8"); //类的实例化: include("uppoo.php");//类的文件名是upoop.php $up=newupphoto; $submit=$_POST['submit']; if($submit=="上传"){ $up->get_ph_tmpnam

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

    有缩略图功能 但是 感觉不全面,而且有点问题,继续学习,将来以后修改下 <form action="<?php $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" method="post" ><input type="text" name="name" /><input type="file&q

  • php版阿里云OSS图片上传类详解

    本文实例讲述了php版阿里云OSS图片上传类.分享给大家供大家参考,具体如下: 1.阿里云基本函数 /** * 把本地变量的内容到文件 * 简单上传,上传指定变量的内存值作为object的内容 */ public function putObject($imgPath,$object) { $content = file_get_contents($imgPath); // 把当前文件的内容获取到传入文件中 $options = array(); try { $this->ossClient->

  • php 图片加水印与上传图片加水印php类

    一个正规的网站,在需要上传图片时,往往都会需要在图片上增加自己网站的LOGO水印.那么如何实现这一步骤呢?首先让我们来了解PHP图片加水印的原理. 通过判断文件类型建立图形,然后把其复制到原建立的图形上,填充并建立rectangle,以备写入imagestring()或是在原已经定好的图像程序当中判断水印类型:一是字符串,另是增加一个图形对象在上面.以下是PHP图片加水印的转载! 参数说明: $max_file_size : 上传文件大小限制, 单位BYTE $destination_folde

  • php另类上传图片的方法(PHP用Socket上传图片)

    服务器端: 复制代码 代码如下: <?phpset_time_limit(10);//* 设置不显示任何错误 *///error_reporting(0); function varinfo($str) {echo "<PRe>";var_dump($str);echo "<pre>";} $commonProtocol = getprotobyname("tcp");$socket = socket_create(

  • php 图片上传类代码

    先来个简单的: 复制代码 代码如下: <? //http://www.jb51.net class upLoad{ public $length; //限定文件大小 public $file; //判断此类是用于图片上传还是文件上传 public $fileName; //文件名 public $fileTemp; //上传临时文件 public $fileSize; //上传文件大小 public $error; //上传文件是否有错,php4没有 public $fileType; //上传

  • PHP实现多图片上传类实例

    本文所述为一个实用的PHP多图片文件上传类,其支持gif.jpg.jpeg.pjpeg.png格式的多图片上传功能,类中还可限制图片类型.上传图片的大小.设置上传目录.一些提交判断等功能.此外该类并不局限于图片的上传,也可以上传TXT/RAR等文件类型,只是需要对代码进行一下修改,感兴趣的读者可以自己尝试一下. php多图片上传类完整功能代码如下: <?php class more_file_upload{ const FILE_PATH = '/uploadfile/'; //默认文件上传的目

  • php封装的单文件(图片)上传类完整实例

    本文实例讲述了php封装的单文件(图片)上传类.分享给大家供大家参考,具体如下: <?php //封装php中的单文件(图片)上传类 /* //参数1:$file 文件数组 5个属性值 name,type,size,tmp,error //参数2:文件保存的路径$path //参数3:文件上传允许的类型 $allow数组 $allow=array('image/jpeg','image/jpg','image/png','image/gif') //参数4: 允许文件上传的最大大小 $size

  • PHP实现原生态图片上传封装类方法

    PHP图片上传类,经典方式,不过上传效率还算可以,我自己用过的一个类,当时对这个类做了些修改,以满足自己特定功能的需要,对PHP熟悉的,可对这个上传类做优化和修改,后附有调用方法,让PHP开发者上传图片轻松容易就做到,先上类代码: <?php class FileUpload_Single { //user define ------------------------------------- var $accessPath ; var $fileSize=200; var $defineTy

  • PHP图片上传类带图片显示

    这是一个PHP的文件上传类带图片显示的.其实做成函数就可以了.不过还是做成类好玩一点.~~~~ 本来应该用JS来验证上传文件类型的.但懒得做了. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3

随机推荐