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
if($v['type']=="image"):
上传
取消上传
"id=""value=""/>
"value=""/>
config->item("base_url");?>asset/js/uploadify/uploadify.css"type="text/css"rel="stylesheet"/>
6.新建photo.php文件:
viewsourceprint?01
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;
}
}
}
领取专属 10元无门槛券
私享最新 技术干货