当前位置:Gxlcms > PHP教程 > php+js实现图片的上传、裁剪、预览、提交示例

php+js实现图片的上传、裁剪、预览、提交示例

时间:2021-07-01 10:21:17 帮助过:18人阅读

首先用到的语言是php、插件imgareaselect没有太多花哨的样式,index.php代码如下:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
  5. <link rel="stylesheet" type="text/css" href="css/imgareaselect-default.css" />
  6. <script type="text/javascript" src="scripts/jquery.min.js"></script>
  7. <script type="text/javascript" src="scripts/jquery.imgareaselect.pack.js"></script>
  8. <script type="text/javascript">
  9. function preview(img, selection) {
  10. $('#selectbanner').data('x',selection.x1);
  11. $('#selectbanner').data('y',selection.y1);
  12. $('#selectbanner').data('w',selection.width);
  13. $('#selectbanner').data('h',selection.height);
  14. var scaleX = 100 / (selection.width || 1);
  15. var scaleY = 100 / (selection.height || 1);
  16. $('#ferret > img').css({
  17. width: Math.round(scaleX * 512) + 'px',//512、390是你上传图片的宽高
  18. height: Math.round(scaleY * 390) + 'px',
  19. marginLeft: '-' + Math.round(scaleX * selection.x1) + 'px',
  20. marginTop: '-' + Math.round(scaleY * selection.y1) + 'px'
  21. });
  22. }
  23. //这里通过jQuery语法在原来图片后插入预览的小图片
  24. $(document).ready(function () {
  25. $('<div id="ferret"><img src="upload_pic/resized_pic.jpg" style="position: relative;" /><div>').css({
  26. float: 'left',
  27. position: 'relative',
  28. overflow: 'hidden',
  29. width: '100px',
  30. height: '100px'
  31. })
  32. .insertAfter($('#selectbanner'));
  33. $('#selectbanner').imgAreaSelect({
  34. selectionColor: 'blue', x1:0, y1:0, x2: 100,//初始位置
  35. maxWidth:500,y2:100,
  36. aspectRatio: '1:1',//缩放比例
  37. selectionOpacity: 0.2 ,
  38. onSelectEnd: preview //裁剪后执行的函数,在上面
  39. });
  40. //确认裁剪
  41. $("#sliceButton").click(function() {
  42. var pic = $('#selectbanner').attr('src');
  43. // alert(pic);
  44. var x,y,w,h;
  45. $.post(
  46. "cat.php", //(2)将附上这个页面的代码
  47. {
  48. x:$('#selectbanner').data('x'),
  49. y:$('#selectbanner').data('y'),
  50. w:$('#selectbanner').data('w'),
  51. h:$('#selectbanner').data('h'),
  52. pic:pic
  53. },
  54. function(data){
  55. // alert(data);
  56. //把裁剪后图片加载到#sure
  57. if(data){
  58. $('#sure').attr('src',data);
  59. }
  60. }
  61. );
  62. });
  63. })
  64. </script>
  65. <title>图片裁剪、预览</title>
  66. </head>
  67. <body>
  68. <?php
  69. //上传图片后,把图片复制到upload文件夹下面
  70. if($_POST){
  71. $photo = $_FILES['img']['name'];
  72. $tmp_addr = $_FILES['img']['tmp_name'];
  73. $path = 'upload/';
  74. $type=array("jpg","gif","jpeg","png");
  75. $tool = substr(strrchr($photo,'.'),1);
  76. if(!in_array(strtolower($tool),$type)){
  77. $text=implode('.',$type);
  78. echo "您只能上传以下类型文件: ",$text,"<br>";
  79. }else{
  80. $filename = explode(".",$photo); //把上传的文件名以"."好为准做一个数组。
  81. $time = date("m-d-H-i-s"); //取当前上传的时间
  82. $filename[0] = $time; //取文件名
  83. $name = implode(".",$filename); //上传后的文件名
  84. $uploadfile = $path.$name;
  85. $_SESSION['upfile'] = $uploadfile;//上传后的文件名地址
  86. move_uploaded_file($tmp_addr,$uploadfile);
  87. }
  88. // echo $uploadfile;
  89. }
  90. ?>
  91. <div id="s">
  92. <!--上传图片-->
  93. <form action="" method="post" enctype="multipart/form-data">
  94. <input type="file" id="img" name="img" value="" onclick=""/>
  95. <input name="submit" id="submit" type="submit" value="提交" class="submit"/>
  96. </form>
  97. <!--显示图片-->
  98. <? if(isset($_SESSION['upfile'])){?>
  99. <img id="selectbanner" name="selectbanner" src="<? echo $_SESSION['upfile'];?>" title="mypic"/>
  100. <? }?>
  101. </div>
  102. <!--确认裁剪-->
  103. <div><input type="submit" id="sliceButton" name="sliceButton" value="sliceButton"></div>
  104. <!--显示裁剪后的图片-->
  105. < div><img id="sure" src="" style="cursor:hand" /></div>
  106. </body>
  107. </html>

更多php+js实现图片的上传、裁剪、预览、提交示例相关文章请关注PHP中文网!

人气教程排行