php支持生成缩略图文件上传代码
<!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.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<?php
class upfileclass {
var $upfile, $upfile_name, $upfile_size;
# $upfile 临时文件名 $_files['tmp_name'] ,$upfile_name 文件名 $_files['name'] ,$upfile_size 文件大小$_files['size'];
var $new_upfile_name; # 上传后的文件名称 ;
var $fleth, $fileextent; # 文件扩展名(类型) ;
var $f1, $f2, $f3; # 文件保存路径(多级) upfiles/2008-01/08/;
var $filename; # 文件(带路径) ;
var $filepath; #相对路径用来删除文件;
var $maxsize, $file_type; # 允许上传文件的大小 允许上传文件的类型 ;
var $buildfile,$newfile,$file_width,$file_height,$rate;
function upfileclass($upfile,$upfile_name,$upfile_size){
$this->upfile = $upfile;
$this->upfile_name = $upfile_name;
$this->upfile_size = $upfile_size;
$this->new_upfile_name = $this->createnewfilename($this->upfile_name);
$this->f1 = "upfiles";
$this->f2 = $this->f1."/".date('y')."-".date('m');
$this->f3 = $this->f2."/".date('d');
$this->filename = $this->f3 . "/" . $this->new_upfile_name;
$this->maxsize = 500*1024; # 文件大小 500kb
$this->file_type = "gif/jpg/jpeg/png/bmp"; # 允许上传的文件类型
}
# 创建新文件名 (原文件名)
function createnewfilename($file_name){
$this->fleth = explode(".",$file_name);
$this->fileextent = $this->fleth[(int)count($this->fleth)-1]; # 获取文件后缀;
$tmps教程tr = date('ymd').rand(0,time()) . "." .$this->fileextent; # 创建新文件名;
return $tmpstr;
}
# 检测文件类型是否正确
function chk_fileextent(){
$iwtrue = 0;
$fle = explode("/",$this->file_type);
for($i=0; $i < count($fle); $i++){
if($this->fileextent == $fle[$i]){
$iwtrue = (int) $iwtrue + 1;
}
}
if( $iwtrue == 0 ){
$this->msg("文件不符合 ".$this->file_type." 格式!");
}
}
# 提示错误信息并终止操作
function msg($error){
echo "<script language="网页特效"> ";
echo " alert('".$error."'); ";
echo " window.history.back(); ";
echo "</script> ";
die();
}
# 保存文件
function savefile(){
$this->chk_fileextent();
$this->chk_filesize();
$this->createfolder( "../".$this->f1 );
$this->createfolder( "../".$this->f2 );
$this->createfolder( "../".$this->f3 );
return $this->chk_savefile();
}
# 检测上传结果是否成功
function chk_savefile(){
$copymsg = copy($this->upfile,"../".$this->filename);
if( $copymsg ){
return $this->filename;
}
else{
$this->msg("文件上传失败! 请重新上传! ");
}
}
# 创建文件夹
function createfolder($foldername){
if( !is_dir($foldername) ){
mkdir($foldername,0777);
}
}
# 检测文件大小
function chk_filesize(){
if( $this->upfile_size > $this->maxsize ){
$this->msg("目标文件不能大于". $this->maxsize/1024 ." kb");
}
}
# 删除文件($filepath 文件相对路径)
function deletefile($filepath){
if( !is_file($filepath) ){
return false;
}
else{
$ending = @unlink($filepath);
return $ending;
}
}
/*
函数:生成缩略图
makebuild("/www.111cn.net/a.jpg","news/b.jpg","100");
参数:
echo $buildfile; 原图 带路径
echo $newfile; 生成的缩略图 带路径
echo $file_width; 缩略图宽度值
echo $file_height; 缩略图高度值 (默认为宽度的比例值)
echo $rate; 缩略图象品质;
*/
function makebuild($buildfile,$newfile,$file_width,$file_height=0,$rate=100) {
if(!is_file($buildfile)){
$this->msg("文件 ".$buildfile." 不是一个有效的图形文件! 系统无法生成该文件的缩略图!");
return false;
}
$data = getimagesize($buildfile);
switch($data[2]){
case 1:
$im = @imagecreatefromgif($buildfile);
break;
case 2:
$im = @imagecreatefromjpeg($buildfile);
break;
case 3:
$im = @imagecreatefrompng($buildfile);
break;
}
if(!$im){
return false;
}
else{
$srcw = imagesx($im); # 取得原图宽度;
$srch = imagesy($im); # 取得原图高度;
$dstx = 0;
$dsty = 0;
if($file_height==0){
$file_height = $file_width/$srcw*$srch;
}
if ($srcw*$file_height>$srch*$file_width){
$ffile_height = round($srch*$file_width/$srcw);
$dsty = floor(($file_height-$ffile_height)/2);
$ffile_width = $file_width;
}
else {
$ffile_width = round($srcw*$file_height/$srch);
$dstx = floor(($file_width-$ffile_width)/2);
$ffile_height = $file_height;
}
$ni = imagecreatetruecolor($file_width,$file_height);
$dstx = ($dstx<0)?0:$dstx;
$dsty = ($dstx<0)?0:$dsty;
$dstx = ($dstx>($file_width/2))?floor($file_width/2):$dstx;
$dsty = ($dsty>($file_height/2))?floor($file_height/s):$dsty;
imagecopyresized($ni,$im,$dstx,$dsty,0,0,$ffile_width,$ffile_height,$srcw,$srch);
imagejpeg($ni,$newfile,$rate); # 生成缩略图;
imagedestroy($im); # imagedestroy(resource) 释放image关联的内存
}
}
}
?>
</body>
</html>
有3个文件:
authcode.php-----验证码的生成php文件
authcode.html-----前台显示页面
dealauthcode.php-----ajax提交到的后台处理判断验证码是否正确的处理页面
*/
?>
前台调用验证码代码
代码如下 | 复制代码 |
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> |
dealauthcode.php-----ajax提交到的后台处理判断验证码是否正确的处理页面
代码如下 | 复制代码 | ||||||||||||
<?php session_start(); $authcode = $_get['authcode']; //这里的$_session['authcode']是在验证码authcode页面产生的 if(strtoupper($authcode)!= $_session['authcode']){ echo 'mis_match'; } ?> 本款程序可以获取图片大小,格式等信息,同时还可以对图片进行缩略图处理与给图片加水印功能哦。
成条形码就是必须生成图片了,在php生成图片我们就必须用到gd库来实现了,所以你得找到你的将php.ini文件找到extension=php_gd2.dll 去掉前面的;。你就可能使用些实例了。 */
//调用 方法
|
代码如下 | 复制代码 |
class upimages { var $annexfolder = "upload";//附件存放点,默认为:annex var $smallfolder = "small";//缩略图存放路径,注:必须是放在 $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("没有上传图片信息,请确认"); $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; } } |
相关文章
- 本文章来人大家介绍一个php文件上传类的使用方法,期望此实例对各位php入门者会有不小帮助哦。 简介 Class.upload.php是用于管理上传文件的php文件上传类, 它可以帮...2016-11-25
- 又码了一个周末的代码,这次在做一些关于文件上传的东西。(PHP UPLOAD)小有收获项目是一个BT种子列表,用户有权限上传自己的种子,然后配合BT TRACK服务器把种子的信息写出来...2016-11-25
- 本文实例讲述了jQuery实现文件上传进度条效果的代码。分享给大家供大家参考。具体如下: 运行效果截图如下:具体代码如下:<!DOCTYPE html><html><head><meta charset="utf-8"><title>upload</title><link rel="stylesheet...2015-11-24
- 本篇文章主要说明的是与php文件上传的相关配置的知识点。PHP文件上传功能配置主要涉及php.ini配置文件中的upload_tmp_dir、upload_max_filesize、post_max_size等选项,下面一一说明。打开php.ini配置文件找到File Upl...2015-10-21
借助FileReader实现将文件编码为Base64后通过AJAX上传
这篇文章主要介绍了借助FileReader实现将文件编码为Base64后通过AJAX上传的方法,包括后端对文件数据解码并保存的PHP代码,需要的朋友可以参考下...2015-12-25- 这篇文章主要介绍了jQuery+ajax简单实现文件上传的方法,结合实例形式简单分析了jQuery基于ajax的post方法进行文件传输及asp.net后台处理技巧,需要的朋友可以参考下...2016-06-12
- 本文实例讲述了PHP多文件上传类,分享给大家供大家参考。具体如下:<?phpclass Test_Upload{ protected $_uploaded = array(); protected $_destination; protected $_max = 1024000; protected $_messages =...2015-10-30
- 这篇文章主要介绍了c#生成高清缩略图的二个示例,需要的朋友可以参考下...2020-06-25
- 这篇文章主要为大家详细介绍了js实现上传文件添加和删除文件选择框 ,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2016-10-25
- php 大文件带进度的上传,一直是一个令php程序员很苦恼的问题。查询baidu 、Google ,大体做带进度的上传方式为:flash+php,socket,apc+php等,下面我介绍了apc +php+ajax制作的带进度的上传,并贴出源码,希望对大家有用。 Altern...2015-10-30
- 这篇文章主要为大家详细介绍了C#文件上传的简单实现,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
- 这篇文章主要介绍了js 实现文件上传样式,下面文章举例说明js 是如何实现文件上传样式的,附有代码详细解说,需要的朋友可以参考一下,希望对你有所帮助...2021-10-21
- 本文给大家介绍一个不错的需要登录的php 文件上传管理系统,功能简单有需要了解的同学可参考。 代码如下<?php$admin_pw="admin";//管理密码$uploaddir="upload";//上传目录session_start();if($_GET['action']=="g...2015-10-30
asp.net html控件的File控件实现多文件上传实例分享
asp.net中html控件的File控件实现多文件上传简单实例,开发工具vs2010使用c#语言,感兴趣的朋友可以了解下,必定是多文件上传值得学习,或许本文所提供的知识点对你有所帮助...2021-09-22- 这篇文章主要介绍了JQuery异步提交表单与文件上传功能,结合实例形式分析了jQuery表单提交及文件传输操作的相关实现技巧,需要的朋友可以参考下...2017-01-16
- 复制代码 代码如下:<?php if($_FILES['myfile']['name'] != '') { if($_FILES['myfile']['error'] > 0) { echo "错误状态:" . $_FILES['myfile']['error']; } else { move_uploaded_f...2013-10-04
- 这款批量生成缩略图代码可以生成指定大小的小图哦,并且支持文件批量上传。 这款教程会用到php文件 view.php config.php funs.php index.php 功能: -------...2016-11-25
使用jQuery.form.js/springmvc框架实现文件上传功能
这篇文章主要介绍了使用jQuery.form.jsspringmvc框架实现文件上传功能,非常具有参考借鉴价值,感兴趣的朋友一起学习吧...2016-05-14- 这篇文章主要介绍了C#实现为一张大尺寸图片创建缩略图的方法,涉及C#创建缩略图的相关图片操作技巧,需要的朋友可以参考下...2020-06-25
- if( isset($_FILES['upImg']) ) { if( $userGroup[$loginArr['group']]['upload'] == 0 ) { echo '{"error":"您所在的用户组无权上传图片!"}'; } else...2016-11-25