首先用到的語言是php、外掛imgareaselect(下載位址),沒有太多花俏的樣式,index.php程式碼如下: 複製程式碼 程式碼如下: <BR>function preview(img, selection) { <BR>$('#selectbanner').data('x',selection.x1); <BR>$( '#selectbanner').data('y',selection.y1); <BR>$('#selectbanner').data('w',selection.width); <BR>$('#selectbanner'). data('h',selection.height); <br><br>var scaleX = 100 / (selection.width || 1); <BR>var scaleY = 100 / (selection.height || 1); <BR>$('#ferret > img').css({ <BR>width: Math.round(scaleX * 512) 'px',//512、390是你上傳圖片的寬高<BR>height: Math. round(scaleY * 390) 'px', <BR>marginLeft: '-' Math.round(scaleX * selection.x1) 'px', <BR>marginTop: '-' Math.round(scaleY * selection.y1) 'px' <BR>}); <BR>} <br><br>//這裡透過jQuery語法在原來圖片後插入預覽的小圖片<BR>$(document).ready(function () { <BR>$('<div id="ferret"><img src="upload_pic/resized_pic.jpg" style="max-width:90%" / alt="php js實作圖片的上傳、裁切、預覽、提交範例_PHP教學" ><div>').css({ <BR>float: 'left' , <BR>position: 'relative', <BR>overflow: 'hidden', <BR>width: '100px', <BR>height: '100px' <BR>}) <br><br>.insertAfter $('#selectbanner')); <br><br>$('#selectbanner').imgAreaSelect({ <BR>selectionColor: 'blue', x1:0, y1:0, x2: 100,//初始位置<BR>maxWidth:500,y2:100, <BR>aspectRatio: '1:1',//縮放比例<BR>selectionOpacity: 0.2 , <BR>onSelectEnd: preview //裁剪後執行的函數,在上面裁剪後執行的函數,在上面裁剪後執行的函數,在上面裁剪後執行的函數,在上面裁剪<BR>}); <BR>//確認裁切<BR>$("#sliceButton").click(function() { <BR>var pic = $('#selectbanner').attr('src') ; <BR>// alert(pic); <BR>var x,y,w,h; <BR>$.post( <BR>"cat.php", //(2)將附上此頁的程式碼<BR>{ <BR>x:$('#selectbanner').data('x'), <BR>y:$('#selectbanner').data('y'), <BR>w: $('#selectbanner').data('w'), <BR>h:$('#selectbanner').data('h'), <BR>pic:pic <BR>}, <BR>function (data){ <BR>// alert(data); <BR>//把裁切後圖片載入到#sure <BR>if(data){ <BR>$('#sure').attr('src ',data); <BR>} <BR>} <BR>); <BR>}); <BR>}) <BR> 圖片裁切、預覽 //上傳圖片後,把圖片複製到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," "; }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; } ?> if(isset($_SESSION['upfile'])){?> }?> http://www.bkjia.com/PHPjc/313687.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/313687.htmlTechArticle首先使用的語言是php、插件imgareaselect(下載位址),沒有太多花俏的樣式,index. php程式碼如下: 複製程式碼如下: !DOCTYPE html PUBLIC "-//W3C//DTD...