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

  首先用到的语言是php、插件imgareaselect没有太多花哨的样式,index.php代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<!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 () {
$('< p  id = "ferret" >< img  src = "upload_pic/resized_pic.jpg"  style = "position: relative;"  />< p >').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;
}
?>
< p  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"/>
<? }?>
</ p >
<!--确认裁剪-->
< p >< input  type = "submit"  id = "sliceButton"  name = "sliceButton"  value = "sliceButton" ></ p >
<!--显示裁剪后的图片-->
p >< img  id = "sure"  src = ""  style = "cursor:hand"  /></ p >
</ body >
</ html >

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值