Chinaunix首页 | 论坛 | 博客
  • 博客访问: 393473
  • 博文数量: 25
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 125
  • 用 户 组: 普通用户
  • 注册时间: 2018-07-07 22:36
文章分类
文章存档

2020年(9)

2019年(14)

2018年(2)

我的朋友

分类: PHP

2020-03-10 09:19:51

网上大多图片上传插件都不带裁剪功能,这个是php+html5实现的兼容手机端的图片选取裁剪上传实例,分享出来希望能帮到大家。



首先放置一个上传按钮及相关预览信息等
  1. <form id="upload_form" enctype="multipart/form-data" method="post" action="upload.php" onsubmit="return checkForm()">
  2.    
  3.    <input type="hidden" id="x1" name="x1" autocomplete="off" />
  4.    <input type="hidden" id="y1" name="y1" autocomplete="off" />
  5.    <input type="hidden" id="x2" name="x2" autocomplete="off" />
  6.    <input type="hidden" id="y2" name="y2" autocomplete="off" />
  7.    <input type="file" name="image_file" id="image_file" onchange="fileSelectHandler()" />
  8.    <div class="error">
  9.     注意:上传前,先截图
  10.    </div>
  11.    <div class="step2">
  12.     <img id="preview" />
  13.     <div class="info">
  14.      <ul>
  15.       <li><label>文件大小</label> <input type="text" id="filesize" name="filesize" class="input" autocomplete="off" /></li>
  16.       <li><label>类型</label> <input type="text" id="filetype" name="filetype" class="input" autocomplete="off" /></li>
  17.       <li><label>图像尺寸</label> <input type="text" id="filedim" name="filedim" class="input" autocomplete="off" /></li>
  18.       <li><label>宽度</label> <input type="text" id="w" name="w" class="input" autocomplete="off" /></li>
  19.       <li><label>高度</label> <input type="text" id="h" name="h" class="input" autocomplete="off" /></li>
  20.      </ul>
  21.     </div>
  22.     <input type="submit" value="上传" class="btn" />
  23.    </div>
  24. </form>

接着引用js和css样式
  1. <link href="css/jquery.Jcrop.min.css" rel="stylesheet" type="text/css" />
  2. <script type="text/javascript" src="jquery.js"></script>
  3. <script src="js/jquery.Jcrop.min.js"></script>

接着看下表单的检查
  1. function checkForm() {
  2.     if (parseInt($('#w').val())) //若是没有截屏
  3.         return true;
  4.     $('.error').html('请先选择图片,并且截图').show();
  5.     return false;
  6. }

上传文件条件设置
  1. var rFilter = /^(image\/jpeg|image\/png|image\/jpg)$/i;
  2. if (!rFilter.test(oFile.type)) {
  3.     $('.error').html('请选择jpg、jpeg或png格式的图片').show();
  4.     return;
  5. }
  6.  
  7. // check for file size
  8. if (oFile.size > 1000 * 1024) {
  9.     $('.error').html('请上传小于1M的图片').show();
  10.     return;
  11. }

本文转自: 转载请注明出处!
阅读(1914) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~