使用jquery的imagecropper插件做用户头像上传 兼容移动端 - 图1

    相信大多的用户系统里都有用户自定义头像上传这个功能,我这今天也在写这块,我将代码贴出来,方便以后大家借鉴。

    实现方案:使用jquery的imagecropper.js插件中裁剪功能来实现

    imagecropper.js这个源码我找了很久才找到http://pan.baidu.com/s/1oDqRO

    代码量很少就没有写注释了

    css

    1. <style>
    2. body
    3. {
    4. padding: 0;
    5. margin: 0;
    6. height: 100%;
    7. background-color: #eee;
    8. font-size: 12px;
    9. color: #666;
    10. }
    11. a
    12. {
    13. text-decoration: none;
    14. color: #333;
    15. }
    16. a:hover
    17. {
    18. text-decoration: none;
    19. color: #f00;
    20. }
    21. #container
    22. {
    23. position: absolute;
    24. left: 20px;
    25. top: 20px;
    26. }
    27. #wrapper
    28. {
    29. position: absolute;
    30. left: 0px;
    31. top: 40px;
    32. }
    33. #cropper
    34. {
    35. position: absolute;
    36. left: 0px;
    37. top: 0px;
    38. border: 1px solid #ccc;
    39. }
    40. #previewContainer
    41. {
    42. position: absolute;
    43. left: 350px;
    44. top: 60px;
    45. }
    46. .preview
    47. {
    48. border: 1px solid #ccc;
    49. }
    50. #selectBtn
    51. {
    52. position: absolute;
    53. left: 0px;
    54. top: 0px;
    55. width: 119px;
    56. height: 27px;
    57. }
    58. #saveBtn
    59. {
    60. position: absolute;
    61. left: 150px;
    62. top: 0px;
    63. width: 67px;
    64. height: 27px;
    65. }
    66. #rotateLeftBtn
    67. {
    68. position: absolute;
    69. left: 0px;
    70. top: 315px;
    71. width: 100px;
    72. height: 22px;
    73. padding-left: 25px;
    74. padding-top: 2px;
    75. }
    76. #rotateRightBtn
    77. {
    78. position: absolute;
    79. left: 225px;
    80. top: 315px;
    81. width: 50px;
    82. height: 22px;
    83. padding-right: 25px;
    84. padding-top: 2px;
    85. }
    86. </style>

    html

    1. <body onload="init();">
    2. <div id="container">
    3. <a id="selectBtn" href="javascript:void(0);" onclick="document.getElementById('input').click();">选择</a>
    4. <a id="saveBtn" href="javascript:void(0);" onclick="saveImage();">保存</a>
    5. <input type="file" id="input" size="10" style="visibility:hidden;" onchange="selectImage(this.files)" />
    6. <div id="wrapper">
    7. <canvas id="cropper"></canvas>
    8. <a id="rotateLeftBtn" href="javascript:void(0);" onclick="rotateImage(event);">向左旋转</a>
    9. <a id="rotateRightBtn" href="javascript:void(0);" onclick="rotateImage(event);">向右旋转</a>
    10. <span id="status" style="position:absolute;left:350px;top:10px;width:100px;"></span>
    11. <div id="previewContainer">
    12. <canvas id="preview180" width="180" height="180" class="preview"></canvas>
    13. <span style="display:block;width:100%;padding-top:5px;text-align:center;">大尺寸图片,180x180像素</span>
    14. <canvas id="preview100" width="100" height="100" style="position:absolute;left:230px;top:0px;" class="preview"></canvas>
    15. <span style="position:absolute;left:230px;top:110px;width:100px;text-align:center;">中尺寸图片 100x100像素</span>
    16. <canvas id="preview50" width="50" height="50" style="position:absolute;left:255px;top:150px;" class="preview"></canvas>
    17. <span style="position:absolute;left:245px;top:210px;width:70px;text-align:center;">小尺寸图片 50x50像素</span>
    18. </div>
    19. </div>
    20. </div>
    21. </body>

    最后是js部分了

    1. var cropper;
    2. function init()
    3. {
    4. //绑定
    5. cropper = new ImageCropper(300, 300, 180, 180);
    6. cropper.setCanvas("cropper");
    7. cropper.addPreview("preview180");
    8. cropper.addPreview("preview100");
    9. cropper.addPreview("preview50");
    10. //检测用户浏览器是否支持imagecropper插件
    11. if(!cropper.isAvaiable())
    12. {
    13. alert("Sorry, your browser doesn't support FileReader, please use Firefox3.6+ or Chrome10+ to run it.");
    14. }
    15. }
    16. //打开本地图片
    17. function selectImage(fileList)
    18. {
    19. cropper.loadImage(fileList[0]);
    20. }
    21. //旋转图片
    22. function rotateImage(e)
    23. {
    24. switch(e.target.id)
    25. {
    26. case "rotateLeftBtn":
    27. cropper.rotate(-90);
    28. break;
    29. case "rotateRightBtn":
    30. cropper.rotate(90);
    31. break;
    32. }
    33. }
    34. //上传图片
    35. function saveImage()
    36. {
    37. //选个你需要的大小
    38. var imgData = cropper.getCroppedImageData(180, 180);
    39. console.log("上传了:"+imgData);
    40. //在这里写你的上传代码
    41. }