83570690 2018-02-08
jqueryuploadify是一款Ajax风格的批量图片上传插件,在PHP中使用jqueryuploadify很方便,请按照本文介绍的方法和步骤,为你的PHP程序增加jqueryuploadify插件的批量上传图片功能。本文是以dilicms为基础,为其增加图片上传功能。下面我们一起来看一下吧。
1.增加数据表dili_fieldtypes新字段:k=>image,V=>图片上传区域(VACHAR);
2.修改application/libraries/dili/Field_behavior.php,在switch中增加如下代码:
viewsourceprint?1case'image':
$field=array(
'type'=>'VARCHAR',
'constraint'=>255,
'default'=>''
);
7break;
3.修改application/libraries/dili/Form.php并且增加如下代码:
viewsourceprint?1function_image($fileld,$default){
//$type='type=hidden';
//$width=($field['width']?$field['width']:'570').'px;';
//$height=($field['height']?$field['height']:'415').'px;';
return;
}
4.在content_form.php和category_content_form.php中增加判断代码:
viewsourceprint?01<?php
if($v['type']=="image"):
?>
<divid="fileQueue"></div>
<inputtype="file"id="uploadify"name="Filedata"/>
<div>
<ahref="javascript('#uploadify').uploadify('upload','*')">上传</a>
<ahref="javascript('#uploadify').uploadify('cancel')">取消上传</a>
</div>
<inputtype="text"style=""name="<?phpecho$v['name']?>"id="<?phpecho$v['name']."_id"?>"value=""/>
<inputtype="hidden"style=""name="poster"id="<?phpecho$v['name']."_id_1"?>"value=""/>
<linkhref="<?phpecho$this->config->item("base_url");?>asset/js/uploadify/uploadify.css"type="text/css"rel="stylesheet"/>
<scripttype="text/javascript"src="<?phpecho$this->config->item("base_url");?>asset/js/uploadify/jquery.uploadify-3.1.min.js"></script>
<scripttype="text/javascript">
$(document).ready(function()
{
$("#uploadify").uploadify({
'debug':false,
'swf':'<?phpecho$this->config->item("base_url");?>asset/js/uploadify/uploadify.swf',
'uploader':'<?phpecho$this->config->item("base_url");?>photo/savephoto',
'method':"post",
'height':30,
'width':120,
'queueID':'fileQueue',
'auto':false,
'multi':false,
'fileTypeDesc':'只允许上传jpg格式图片',
'fileTypeExt':'*.jpg',
'fileSizeLimit':'10MB',
'formData':{'<?phpechosession_name();?>':'<?phpecho$this->session->userdata('session_id');?>'},
'onUploadError':function(file,errorCode,errorMsg,errorString){
alert('Thefile'+file.name+'couldnotbeuploaded:'+errorString);
},
'onUploadSuccess':function(file,data,response){
alert(data);
$("#<?phpecho$v['name']."_id"?>").val(data);
$("#<?phpecho$v['name']."_id_1"?>").val(data);
}
});
})
</script>
<?phpendif;?>
6.新建photo.php文件:
viewsourceprint?01<?php
if(!defined('BASEPATH'))exit('Nodirectscriptaccessallowed');
date_default_timezone_set('Asia/Shanghai');
classPhotoextendsFront_Controller{
publicfunction__construct()
{
parent::__construct();
}
function_getFilePath()
{
$path="attachments/".date("Y")."/";
if(!file_exists($path)){
mkdir($path,'777');
}
$path.=date("m")."/";
if(!file_exists($path)){
mkdir($path,'777');
}
return$path;
}
function_creat_photothumbs($FileName,$setW){
$IMGInfo=getimagesize($FileName);
if(!$IMGInfo)returnfalse;
if($IMGInfo['mime']=="image/pjpeg"||$IMGInfo['mime']=="image/jpeg"){
$ThisPhoto=imagecreatefromjpeg($FileName);
}elseif($IMGInfo['mime']=="image/x-png"||$IMGInfo['mime']=="image/png"){
$ThisPhoto=imagecreatefrompng($FileName);
}elseif($IMGInfo['mime']=="image/gif"){
$ThisPhoto=imagecreatefromgif($FileName);
}
$width=$IMGInfo['0'];
$height=$IMGInfo['1'];
$scale=$height/$width;
$nw=$setW;
$nh=$nw*$scale;
$NewPhoto=imagecreatetruecolor($nw,$nh);
imagecopyresampled($NewPhoto,$ThisPhoto,0,0,0,0,$nw,$nh,$width,$height);
ImageJpeg($NewPhoto,$FileName);
returntrue;
}
function_savephoto_post(){
$dest_dir=$this->_getFilePath();
if(!empty($_FILES)){
$date=date('Ymd');
//$dest_dir=$this->_getFilePath();
$photoname=$_FILES["Filedata"]['name'];
$phototype=$_FILES['Filedata']['type'];
$photosize=$_FILES['Filedata']['size'];
$fileInfo=pathinfo($photoname);
$extension=$fileInfo['extension'];
$newphotoname=date("YmdHis").mt_rand(10000,99999).'.'.$extension;
$dest=$dest_dir.$newphotoname;
//move_uploaded_file($_FILES['Filedata']['tmp_name'],iconv("UTF-8","gb2312",$dest));
move_uploaded_file($_FILES['Filedata']['tmp_name'],$dest);
//chmod($dest,0755);
//如果宽度大于600则要进行裁剪
$arr=getimagesize($dest);
if($arr[0]>600){
$thumb_w=600;
$this->_creat_photothumbs($dest,$thumb_w);
}
//生成缩略图
$config2['image_library']='gd2';
$config2['source_image']=$dest;
$config2['create_thumb']=TRUE;
$config2['maintain_ratio']=TRUE;
$config2['width']=170;
$config2['height']=150;
$config2['master_dim']="width";
$config2['thumb_marker']="_1";
$this->load->library('image_lib',$config2);
$this->image_lib->resize();
echo$dest;
}
}
}
?>