php+js實現圖片的上傳、裁剪、預覽、提交樣本

來源:互聯網
上載者:User
首先用到的語言是php、外掛程式imgareaselect沒有太多花哨的樣式,index.php代碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta content="text/html; charset=UTF-8" http-equiv="Content-Type"> <link rel="stylesheet" type="text/css" href="css/imgareaselect-default.css" /> <script type="text/javascript" src="scripts/jquery.min.js"></script> <script type="text/javascript" src="scripts/jquery.imgareaselect.pack.js"></script> <script type="text/javascript"> function preview(img, selection) { $('#selectbanner').data('x',selection.x1); $('#selectbanner').data('y',selection.y1); $('#selectbanner').data('w',selection.width); $('#selectbanner').data('h',selection.height); var scaleX = 100 / (selection.width || 1); var scaleY = 100 / (selection.height || 1); $('#ferret > img').css({ width: Math.round(scaleX * 512) + 'px',//512、390是你上傳圖片的寬高 height: Math.round(scaleY * 390) + 'px', marginLeft: '-' + Math.round(scaleX * selection.x1) + 'px', marginTop: '-' + Math.round(scaleY * selection.y1) + 'px' }); } //這裡通過jQuery文法在原來圖片後插入預覽的小圖片 $(document).ready(function () { $('<div id="ferret"><img src="upload_pic/resized_pic.jpg" style="position: relative;" /><div>').css({ float: 'left', position: 'relative', overflow: 'hidden', width: '100px', height: '100px' }) .insertAfter($('#selectbanner')); $('#selectbanner').imgAreaSelect({ selectionColor: 'blue', x1:0, y1:0, x2: 100,//初始位置 maxWidth:500,y2:100, aspectRatio: '1:1',//縮放比例 selectionOpacity: 0.2 , onSelectEnd: preview //裁剪後執行的函數,在上面 }); //確認裁剪 $("#sliceButton").click(function() { var pic = $('#selectbanner').attr('src'); // alert(pic); var x,y,w,h; $.post( "cat.php", //(2)將附上這個頁面的代碼 { x:$('#selectbanner').data('x'), y:$('#selectbanner').data('y'), w:$('#selectbanner').data('w'), h:$('#selectbanner').data('h'), pic:pic }, function(data){ // alert(data); //把裁剪後圖片載入到#sure if(data){ $('#sure').attr('src',data); } } ); }); }) </script> <title>圖片裁剪、預覽</title> </head> <body> <?php //上傳圖片後,把圖片複製到upload檔案夾下面 if($_POST){ $photo = $_FILES['img']['name']; $tmp_addr = $_FILES['img']['tmp_name']; $path = 'upload/'; $type=array("jpg","gif","jpeg","png"); $tool = substr(strrchr($photo,'.'),1); if(!in_array(strtolower($tool),$type)){ $text=implode('.',$type); echo "您只能上傳以下類型檔案: ",$text,"<br>"; }else{ $filename = explode(".",$photo); //把上傳的檔案名稱以"."好為準做一個數組。 $time = date("m-d-H-i-s"); //取當前上傳的時間 $filename[0] = $time; //取檔案名稱 $name = implode(".",$filename); //上傳後的檔案名稱 $uploadfile = $path.$name; $_SESSION['upfile'] = $uploadfile;//上傳後的檔案名稱地址 move_uploaded_file($tmp_addr,$uploadfile); } // echo $uploadfile; } ?> <div id="s"> <!--上傳圖片--> <form action="" method="post" enctype="multipart/form-data"> <input type="file" id="img" name="img" value="" onclick=""/> <input name="submit" id="submit" type="submit" value="提交" class="submit"/> </form> <!--顯示圖片--> <? if(isset($_SESSION['upfile'])){?> <img id="selectbanner" name="selectbanner" src="<? echo $_SESSION['upfile'];?>" title="mypic"/> <? }?> </div> <!--確認裁剪--> <div><input type="submit" id="sliceButton" name="sliceButton" value="sliceButton"></div> <!--顯示裁剪後的圖片--> < div><img id="sure" src="" style="cursor:hand" /></div> </body> </html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.