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

首先放置一个上传按钮及相关预览信息等

 1 <form id="upload_form" enctype="multipart/form-data" method="post" action="upload.php" onsubmit="return checkform()">  
 2    <!-- hidden crop params -->  
 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 }

 

本文转自: 转载请注明出处!