欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php 图片上传类代码

程序员文章站 2023-12-02 16:24:16
先来个简单的: 复制代码 代码如下:
先来个简单的:
复制代码 代码如下:

<?
////www.jb51.net
class upload{
public $length; //限定文件大小
public $file; //判断此类是用于图片上传还是文件上传
public $filename; //文件名
public $filetemp; //上传临时文件
public $filesize; //上传文件大小
public $error; //上传文件是否有错,php4没有
public $filetype; //上传文件类型
public $directory; //
public $maxlen;
public $errormsg;
function __construct($length,$file=true,$directory)
{
$this->maxlen=$length;
$this->length=$length*1024;
$this->file=$file; //true为一般文件,false为图片的判断
$this->directory=$directory;
}
public function uploadfile($filefield)
{
$this->filename=$filefield['name'];
$this->filetemp=$filefield['tmp_name'];
$this->error=$filefield['error'];
$this->filetype=$filefield['type'];
$this->filesize=$filefield['size'];
$pathsign = directory_separator; // /
if($this->file) //一般文件上传
{
$path = $this->_iscreateddir($this->directory);//取得路径
if($path)////www.jb51.net
{
$createfiletype = $this->_getfiletype($this->filename);//设置文件类别
$createfilename=uniqid(rand()); //随机产生文件名
$thisdir=$this->directory.$pathsign.$createfilename.".".$createfiletype;
if(@move_uploaded_file($this->filetemp,$thisdir)) //把临时文件移动到规定的路径下
{
return $thisdir;
}
}
}else{ //图片上传
$path = $this->_iscreateddir($this->directory);//取得路径
if($path)//路径存在////www.jb51.net
{
$createfiletype = $this->_getfiletype($this->filename);//设置文件类别
$createfilename=uniqid(rand());
return @move_uploaded_file($this->filetemp,$this->directory.$pathsign.$createfilename.".".$createfiletype) ? true : false;
}
}
}
public function _isbig($length,$fsize) //返回文件是否超过规定大小
{
return $fsize>$length ? true : false;
}
public function _getfiletype($filename) //获得文件的后缀
{
return end(explode(".",$filename));
}
public function _isimg($filetype) //上传图片类型是否允许
{
$type=array("jpeg","gif","jpg","bmp");
$filetype=strtolower($filetype);
$filearray=explode("/",$filetype);
$file_type=end($filearray);
return in_array($file_type,$type);////www.jb51.net
}
public function _iscreateddir($path) //路径是否存在,不存在就创建
{
if(!file_exists($path))
{
return @mkdir($path,0755)?true:false; //权限755//
}
else
{
return true;
}
}
public function showerror() //显示错误信息
{////www.jb51.net
echo "<script language ='javascript'>\n history.back();\n alert(' $this->errormsg');\n </script> \n";
exit();
}
}
class multiupload extends upload{
public $files;
function __construct($arrayfiles,$length,$file=true,$directory)
{
$this->files=$arrayfiles;
parent::__construct($length,$file,$directory);
}
function uploadmultifile()
{
$arr=array();
if($this->_judge()||$this->_judge()=="no") //文件全部符合规格,就开始上传
{
foreach($this->files as $key=>$value)
{
$strdir=parent::uploadfile($this->files[$key]);
array_push($arr, $strdir);
}
////www.jb51.net
return $arr;
}else
{
return false;
}
}
function _judge()
{
if($this->file)
{
foreach($this->files as $key=>$value)
{
if($this->_isbig($this->length,$value['size']))
{
$this->errormsg="文件超过 $this->maxlen k";
parent::showerror();
}
if($value['error']=upload_err_no_file)
{
//$this->errormsg="上传文件出现错误";
//parent::showerror();
return "no";
}
}
return true;
}else
{
////www.jb51.net
foreach($this->files as $key=>$value)
{
if($this->_isbig($this->length,$value['size']))
{
$this->errormsg="图片超过$this->maxlen";
parent::showerror();
}
if($value['error']!=0)
{
$this->errormsg="上传图片出现错误";
parent::showerror();
}
if(!$this->_isimg($value['type']))
{
$this->errormsg="图片格式不对";
parent::showerror();
}
}
return true;
}
}
}
?>

再来个复杂点的,可以自动生成缩略图的功能的php上传类
开始第一步:
创建文件夹,布局:
annex:附件(该目录下存放上传的原图片)
|— smallimg:存放缩略图片
|— mark:存放水印图片
include:存放类文件,字体(本程序代码使用的是:04b_08__.ttf)
|— upfile.php:集成简单上传,生成缩略图及水印的类文件信息
|— 04b_08__.ttf:字体文件
test.php:测试文件
第二步上传类
upfile.php
复制代码 代码如下:

<?php
class upimages {
var $annexfolder = "annex";//附件存放点,默认为:annex
var $smallfolder = "smallimg";//缩略图存放路径,注:必须是放在 $annexfolder下的子目录,默认为:smallimg
var $markfolder = "mark";//水印图片存放处
var $upfiletype = "jpg gif png";//上传的类型,默认为:jpg gif png rar zip
var $upfilemax = 1024;//上传大小限制,单位是“kb”,默认为:1024kb
var $fonttype;//字体
var $maxwidth = 500; //图片最大宽度
var $maxheight = 600; //图片最大高度
function upimages($annexfolder,$smallfolder,$includefolder) {
$this->annexfolder = $annexfolder;
$this->smallfolder = $smallfolder;
$this->fonttype = $includefolder."/04b_08__.ttf";
}
function upload($inputname) {
$imagename = time();//设定当前时间为图片名称
if(@empty($_files[$inputname]["name"])) die(error("没有上传图片信息,请确认"));
$name = explode(".",$_files[$inputname]["name"]);//将上传前的文件以“.”分开取得文件类型
$imgcount = count($name);//获得截取的数量
$imgtype = $name[$imgcount-1];//取得文件的类型
if(strpos($this->upfiletype,$imgtype) === false) die(error("上传文件类型仅支持 ".$this->upfiletype." 不支持 ".$imgtype));
$photo = $imagename.".".$imgtype;//写入数据库的文件名
$uploadfile = $this->annexfolder."/".$photo;//上传后的文件名称
$upfileok = move_uploaded_file($_files[$inputname]["tmp_name"],$uploadfile);
if($upfileok) {
$imgsize = $_files[$inputname]["size"];
$ksize = round($imgsize/1024);
if($ksize > ($this->upfilemax*1024)) {
@unlink($uploadfile);
die(error("上传文件超过 ".$this->upfilemax."kb"));
}
} else {
die(error("上传图片失败,请确认你的上传文件不超过 $upfilemax kb 或上传时间超时"));
}
return $photo;
}
function getinfo($photo) {
$photo = $this->annexfolder."/".$photo;
$imageinfo = getimagesize($photo);
$imginfo["width"] = $imageinfo[0];
$imginfo["height"] = $imageinfo[1];
$imginfo["type"] = $imageinfo[2];
$imginfo["name"] = basename($photo);
return $imginfo;
}
function smallimg($photo,$width=128,$height=128) {
$imginfo = $this->getinfo($photo);
$photo = $this->annexfolder."/".$photo;//获得图片源
$newname = substr($imginfo["name"],0,strrpos($imginfo["name"], "."))."_thumb.jpg";//新图片名称
if($imginfo["type"] == 1) {
$img = imagecreatefromgif($photo);
} elseif($imginfo["type"] == 2) {
$img = imagecreatefromjpeg($photo);
} elseif($imginfo["type"] == 3) {
$img = imagecreatefrompng($photo);
} else {
$img = "";
}
if(empty($img)) return false;
$width = ($width > $imginfo["width"]) ? $imginfo["width"] : $width;
$height = ($height > $imginfo["height"]) ? $imginfo["height"] : $height;
$srcw = $imginfo["width"];
$srch = $imginfo["height"];
if ($srcw * $width > $srch * $height) {
$height = round($srch * $width / $srcw);
} else {
$width = round($srcw * $height / $srch);
}
if (function_exists("imagecreatetruecolor")) {
$newimg = imagecreatetruecolor($width, $height);
imagecopyresampled($newimg, $img, 0, 0, 0, 0, $width, $height, $imginfo["width"], $imginfo["height"]);
} else {
$newimg = imagecreate($width, $height);
imagecopyresized($newimg, $img, 0, 0, 0, 0, $width, $height, $imginfo["width"], $imginfo["height"]);
}
if ($this->tofile) {
if (file_exists($this->annexfolder."/".$this->smallfolder."/".$newname)) @unlink($this->annexfolder."/".$this->smallfolder."/".$newname);
imagejpeg($newimg,$this->annexfolder."/".$this->smallfolder."/".$newname);
return $this->annexfolder."/".$this->smallfolder."/".$newname;
} else {
imagejpeg($newimg);
}
imagedestroy($newimg);
imagedestroy($img);
return $newname;
}
function watermark($photo,$text) {
$imginfo = $this->getinfo($photo);
$photo = $this->annexfolder."/".$photo;
$newname = substr($imginfo["name"], 0, strrpos($imginfo["name"], ".")) . "_mark.jpg";
switch ($imginfo["type"]) {
case 1:
$img = imagecreatefromgif($photo);
break;
case 2:
$img = imagecreatefromjpeg($photo);
break;
case 3:
$img = imagecreatefrompng($photo);
break;
default:
return false;
}
if (empty($img)) return false;
$width = ($this->maxwidth > $imginfo["width"]) ? $imginfo["width"] : $this->maxwidth;
$height = ($this->maxheight > $imginfo["height"]) ? $imginfo["height"] : $this->maxheight;
$srcw = $imginfo["width"];
$srch = $imginfo["height"];
if ($srcw * $width > $srch * $height) {
$height = round($srch * $width / $srcw);
} else {
$width = round($srcw * $height / $srch);
}
if (function_exists("imagecreatetruecolor")) {
$newimg = imagecreatetruecolor($width, $height);
imagecopyresampled($newimg, $img, 0, 0, 0, 0, $width, $height, $imginfo["width"], $imginfo["height"]);
} else {
$newimg = imagecreate($width, $height);
imagecopyresized($newimg, $img, 0, 0, 0, 0, $width, $height, $imginfo["width"], $imginfo["height"]);
}
$white = imagecolorallocate($newimg, 255, 255, 255);
$black = imagecolorallocate($newimg, 0, 0, 0);
$alpha = imagecolorallocatealpha($newimg, 230, 230, 230, 40);
imagefilledrectangle($newimg, 0, $height-26, $width, $height, $alpha);
imagefilledrectangle($newimg, 13, $height-20, 15, $height-7, $black);
imagettftext($newimg, 4.9, 0, 20, $height-14, $black, $this->fonttype, $text[0]);
imagettftext($newimg, 4.9, 0, 20, $height-6, $black, $this->fonttype, $text[1]);
if($this->tofile) {
if (file_exists($this->annexfolder."/".$this->markfolder."/".$newname)) @unlink($this->annexfolder."/".$this->markfolder."/".$newname);
imagejpeg($newimg,$this->annexfolder."/".$this->markfolder."/".$newname);
return $this->annexfolder."/".$this->markfolder."/".$newname;
} else {
imagejpeg($newimg);
}
imagedestroy($newimg);
imagedestroy($img);
return $newname;
}
}
?>

第三步:测试上传类
test.php
复制代码 代码如下:

<?php
$annexfolder = "annex";
$smallfolder = "smallimg";
$markfolder = "mark";
$includefolder = "include";
require("./".$includefolder."/upfile.php");
$img = new upimages($annexfolder,$smallfolder,$includefolder);
$text = array("www.jb51.net","all rights reserved");
if(@$_get["go"]) {
$photo = $img->upload("upfile");
$img->maxwidth = $img->maxheight = 350;//设置生成水印图像值
$img->tofile = true;
$newsmallimg = $img->smallimg($photo);
$newmark = $img->watermark($photo,$text);
echo "<img src='".$newsmallimg."' border='0'><br><br>";
echo "<img src='".$newmark."' border='0'><br><br>";
echo "<a href='./test.php'>继续上传</a>";
} else {
?>
<form method="post" action="./test.php?go=go" enctype="multipart/form-data">
<input type="file" name="upfile"><br><br>
<input type="submit" value="上传">
</form>
<?php
}
?>