thinkphp jquery实现图片上传和预览效果

yipeiwu_com7年前PHP代码库

先上效果图:

那个file按钮样式先忽略

点击选择图片(浏览),随便选一张图片

js代码如下

//上传图片立即预览
 function PreviewImage(imgFile) {
  var filextension = imgFile.value.substring(imgFile.value
    .lastIndexOf("."), imgFile.value.length);
  filextension = filextension.toLowerCase();
  if ((filextension != '.jpg') && (filextension != '.gif')
    && (filextension != '.jpeg') && (filextension != '.png')
    && (filextension != '.bmp')) {
   alert("对不起,系统仅支持标准格式的照片,请您调整格式后重新上传,谢谢 !");
   imgFile.focus();
  } else {
   var path;
   if (document.all)//IE
   {
    imgFile.select();
    path = document.selection.createRange().text;
    document.getElementById("photo_info").innerHTML = "";
    document.getElementById("photo_info").style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true',sizingMethod='scale',src=\""
      + path + "\")";//使用滤镜效果  
   } else//FF
   {
    path = window.URL.createObjectURL(imgFile.files[0]);// FF 7.0以上
    //path = imgFile.files[0].getAsDataURL();// FF 3.0
    document.getElementById("photo_info").innerHTML = "<img id='img1' width='120px' height='100px' src='"+path+"'/>";
    //document.getElementById("img1").src = path;
   }
  }
 }

html代码,其实不重要啦

<div class="form-group has-success has-feedback">
       <label for="info_photo" class="col-sm-2 control-label">物品图片</label>
       <div class="col-sm-10">

        <span class="btn btn-success btn-file"> 选择图片 <span
         class="glyphicon glyphicon-picture" aria-hidden="true"></span>
         <input type="file" name="info_photo" value="" id="info_photo"
         onchange='PreviewImage(this)' />
        </span>
       </div>

      </div>

      <div class="form-group has-success has-feedback">
       <label for="info_desc" class="col-sm-2 control-label"><span
        style="color: red;">* </span> 详细描述</label>
       <div class="col-sm-10">
        <textarea class="form-control" rows="5" id="info_desc"
         name="info_desc" placeholder="如需补充,请填写..." title="可包含中文数字和常用字符"></textarea>
        <div id="photo_info" class="photo_info"></div>
       </div>
      </div>

css代码就不上了啊
顺便补充一下thinkphp上传图片代码,把整个发布消息都拷过了算了

public function loseThing(){
   $m=M('info');
   $m->create();
   $upload = new \Think\Upload();// 实例化上传类
   $upload->maxSize =  3145728 ;// 设置附件上传大小
   $upload->exts  =  array('jpg', 'gif', 'png', 'jpeg','bmp');// 设置附件上传类型
   $upload->rootPath = './Public/';
   $upload->savePath = '/Uploads/'; // 设置附件上传目录
   $upload->autoSub = false;
   // 上传文件
   $info = $upload->upload();
   if($info) {
  // 上传成功
   foreach($info as $file){
    $m->info_photo=$file['savename'];
   }
  }
   $m->create_time=date('Y-m-d H:i:s',time()); 
   $m->uid=$_SESSION['id'];
   $m->username=$_SESSION['username'];
  /* $m->user_img=$_SESSION['filename']; */

   $m->info_type="寻物启事";//信息类型为失物
   $lastId=$m->add();
   if($lastId){
    $this->success('发布成功');
   }else{
    $this->error('发布失败,请先登录');
   } 
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【宜配屋www.yipeiwu.com】。

相关文章

PHP生成Flash动画的实现代码

PHP生成Flash动画的实现代码

其中有一组映射到 SWF 动画中的数据类型的对象:子图形、图形、文本、位图等等。在本文中,我使用了预编译的扩展 php_ming.dll 库用于 Windows 版本的 PHP。 清单...

php事务处理实例详解

一、php事务处理概述: 事务:是若干事件的集合 事务处理:当所有事件执行成功,事务才执行;若有任何一个事件不能成功执行,事务的其它事件也不被执行。 只要你的MySQL版本支持BDB或I...

php中使用in_array() foreach array_search() 查找数组是否包含时的性能对比

php中使用in_array() foreach array_search() 查找数组是否包含时的性能对比

判断某字符是否包含与某于数组中,方法有很多,刚学习php的新手们估计偏向于使用循环来解决,对于一般的小网站来说,这种解决方案是不会出现什么大问题的。但就性能来说,这种方法不是最好的方法,...

PHP GD库添加freetype拓展的方法

背景:业务需求要用到 imagefttext 函数,发现GD库一开始安装时没有添加 FreeType linux版本 centos 6.6 安装流程(由于服务器为分布式内网服务器,无法使...

PHP的explode和implode的使用说明

说到php,函数便长短常主要的,也是php爱好者,和喜好php进阶的朋友们必须控制的东东,学习php的友朋们也晓得,数组也是必需把握的,能够那样道,进阶php,也便是学习php中的数组而...