前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >thinkphp ajaxfileupload实现异步上传图片的示例

thinkphp ajaxfileupload实现异步上传图片的示例

作者头像
用户2323866
修改2021-07-02 10:36:07
1K0
修改2021-07-02 10:36:07
举报
文章被收录于专栏:技术派

thinkphp开发图片上传,图片异步上传是目前比较方便的功能,这里我就不写css文件了,将代码写出来。引入核心文件下载https://github.com/carlcarl/A...

HTML

下面首先在html页面引入相关js资源

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>图片上传</title> <script type="text/javascript" src="js/jquery-1.8.2.min.js"></script> <script type="text/javascript" src="js/ajaxfileupload.js"></script> </head> <body> </body> </html>

接下来在body中创建相关div

<label >封面图片:</label> <div > <label onclick="upd_file(this,'image_file');"> <input type="file" name="image_file" id="image_file"/> <!--上传成功后图片会给value赋值图片路径,以便于form表单提交数据--> <input type="hidden" name="image" value=""> </label> <label ></label> </div> <div ></div> <!--上传成功后图片会在这里显示否则是默认图片--> <img id="image" src="/Public/images/empty_thumb.gif" />

解释一下:

其中upd_file(this,'image_file')不可缺少

其中隐藏的input 是用于上传成功后赋值图片路径,以便于form表单提交数据

接下来在html中编辑javascript脚本以便于传递和提交图片功能

<script> function upd_file(obj,file_id){ $("input[name='"+file_id+"']").bind("change",function(){ $(obj).hide(); $(obj).parent().find(".fileuploading").removeClass("hide"); $(obj).parent().find(".fileuploading").removeClass("show"); $(obj).parent().find(".fileuploading").addClass("show"); $.ajaxFileUpload ( { url:'/index.php/home/avatar/app_upload_image',//上传图片处理文件 secureuri:false, fileElementId:file_id, dataType: 'json', success: function (data, status) { $(obj).show(); $(obj).parent().find(".fileuploading").removeClass("hide"); $(obj).parent().find(".fileuploading").removeClass("show"); $(obj).parent().find(".fileuploading").addClass("hide"); if(data.status==1) { $("#image").attr("src",data.thumb_url+"/【一个开发人员,能懂服务器量好,反之一个服务器维护人员,也应该懂开发】/3;r="+Math.random()); $("input[name='image']").val(data.url);//返回json后将隐藏input赋值 //$("#img_url").html('<input type="hidden" name="img_url" value="'+ path.path +'" />'); } else { $.showErr(data.msg); } }, error: function (data, status, e) { $.showErr(data.responseText);; $(obj).show(); $(obj).parent().find(".fileuploading").removeClass("hide"); $(obj).parent().find(".fileuploading").removeClass("show"); $(obj).parent().find(".fileuploading").addClass("hide"); } } ); $("input[name='"+file_id+"']").unbind("change"); }); } <script>

thikphp 中创建方法 app_upload_image()

function app_upload_image($maxSize=52428800){ $id=session('id'); $config=array( 'rootPath' =>'Upload', //文件上传/【本文中一些PHP版本可能是以前的,如果不是一定要,建议PHP尽量使用7.2以上的版本】/保存的根路径 'savePath' =>'/avatar/', 'exts' => array('jpg', 'gif', 'png', 'jpeg','bmp'), 'maxSize' => $maxSize, 'autoSub' => true, ); $upload = new \Think\Upload($config);// 实例化上传类 $z = $upload->uploadOne($_FILES['image_file']); if($z) { //拼接图片的路径名 $img='/Upload'.$z['savepath'].$z['savename']; $_POST['image_file']=$img; //获取上传图片绝对路径 $imgsrc=$_SERVER['DOCUMENT_ROOT'].__ROOT__.$_POST['image_file']; $image = new ThinkImage(); $image-&gt;open($imgsrc); //将图片裁剪为400x400并保存为corp.jpg $image-&gt;thumb(205, 160,\Think\Image::IMAGE_THUMB_CENTER)-&gt;save($imgsrc);

$this-&gt;ajaxReturn(array("thumb_url"=&gt;$img,"url"=>$img,"status"=>1)); } }

OK这样就好了,首先和大家说一下,如果ajaxfileupload.js报错程序是不会跑通的,如果你的程序报错就检查你的ajaxfileupload文件是不是版本的问题

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
图片处理
图片处理(Image Processing,IP)是由腾讯云数据万象提供的丰富的图片处理服务,广泛应用于腾讯内部各产品。支持对腾讯云对象存储 COS 或第三方源的图片进行处理,提供基础处理能力(图片裁剪、转格式、缩放、打水印等)、图片瘦身能力(Guetzli 压缩、AVIF 转码压缩)、盲水印版权保护能力,同时支持先进的图像 AI 功能(图像增强、图像标签、图像评分、图像修复、商品抠图等),满足多种业务场景下的图片处理需求。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档