php+js實現(xiàn)圖片的上傳、裁剪、預(yù)覽、提交示例
更新時間:2013年08月27日 17:10:29 作者:
首先用到的語言是php、插件imgareaselect附下載地址,沒有太多花哨的樣式,喜歡的朋友可以學(xué)習(xí)下
首先用到的語言是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語法在原來圖片后插入預(yù)覽的小圖片
$(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 //裁剪后執(zhí)行的函數(shù),在上面
});
//確認(rèn)裁剪
$("#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>圖片裁剪、預(yù)覽</title>
</head>
<body>
<?php
//上傳圖片后,把圖片復(fù)制到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); //把上傳的文件名以"."好為準(zhǔn)做一個數(shù)組。
$time = date("m-d-H-i-s"); //取當(dāng)前上傳的時間
$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>
<!--確認(rèn)裁剪-->
<div><input type="submit" id="sliceButton" name="sliceButton" value="sliceButton"></div>
<!--顯示裁剪后的圖片-->
< div><img id="sure" src="" style="cursor:hand" /></div>
</body>
</html>
復(fù)制代碼 代碼如下:
<!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語法在原來圖片后插入預(yù)覽的小圖片
$(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 //裁剪后執(zhí)行的函數(shù),在上面
});
//確認(rèn)裁剪
$("#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>圖片裁剪、預(yù)覽</title>
</head>
<body>
<?php
//上傳圖片后,把圖片復(fù)制到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); //把上傳的文件名以"."好為準(zhǔn)做一個數(shù)組。
$time = date("m-d-H-i-s"); //取當(dāng)前上傳的時間
$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>
<!--確認(rèn)裁剪-->
<div><input type="submit" id="sliceButton" name="sliceButton" value="sliceButton"></div>
<!--顯示裁剪后的圖片-->
< div><img id="sure" src="" style="cursor:hand" /></div>
</body>
</html>
您可能感興趣的文章:
- 基于原生JS實現(xiàn)圖片裁剪
- js+jquery實現(xiàn)圖片裁剪功能
- 基于HTML5+JS實現(xiàn)本地圖片裁剪并上傳功能
- Cropper.js 實現(xiàn)裁剪圖片并上傳(PC端)
- 使用ImageMagick進(jìn)行圖片縮放、合成與裁剪(js+python)
- 使用JavaScript+canvas實現(xiàn)圖片裁剪
- vue-cli結(jié)合Element-ui基于cropper.js封裝vue實現(xiàn)圖片裁剪組件功能
- Nodejs下使用gm圓形裁剪并合成圖片的示例
- cropper js基于vue的圖片裁剪上傳功能的實現(xiàn)代碼
- cropperjs實現(xiàn)裁剪圖片功能
相關(guān)文章
Aliyun Linux 編譯安裝 php7.3 tengine2.3.2 mysql8.0 redis5的過程詳解
這篇文章主要介紹了Aliyun Linux 編譯安裝 php7.3 tengine2.3.2 mysql8.0 redis5,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10為你的 Laravel 驗證器加上多驗證場景的實現(xiàn)
這篇文章主要介紹了為你的 Laravel 驗證器加上多驗證場景的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04ThinkPHP5.1框架頁面跳轉(zhuǎn)及修改跳轉(zhuǎn)頁面模版示例
這篇文章主要介紹了ThinkPHP5.1框架頁面跳轉(zhuǎn)及修改跳轉(zhuǎn)頁面模版,結(jié)合實例形式分析了thinkPHP5.1框架進(jìn)行頁面跳轉(zhuǎn)及修改跳轉(zhuǎn)模板相關(guān)操作技巧,需要的朋友可以參考下2019-05-05Django中通過定時任務(wù)觸發(fā)頁面靜態(tài)化的處理方式
Django是一個開放源代碼的Web應(yīng)用框架,由Python寫成。這篇文章主要介紹了Django中通過定時任務(wù)觸發(fā)頁面靜態(tài)化的方式,需要的朋友可以參考下2018-08-08