
点击此处下载源码
HTML
首先,我们要建立一个主页面index.html来展示最新上传的照片,我们使用jQuery来获取最新的照片,所以这是一个HTML页面,不需要PHP标签,当然还要建立一个包括用来拍照和上传交互所需的HTML结构。
<div id="main" style="width:90%"><div id="photos"></div><div id="camera"><div id="cam"></div><div id="webcam"></div><div id="buttons"> <div class="button_pane" id="shoot"> <a id="btn_shoot" href="" class="btn_blue">拍照</a> </div> <div class="button_pane hidden" id="upload"> <a id="btn_cancel" href="" class="btn_blue">取消</a> <a id="btn_upload" href="" class="btn_green">上传</a> </div></div></div> </div>我们在body间加入了以上html代码,其中#photos用来加载展示最新上传的照片;#camera用于加载摄像模块,包括调用摄像flash组件webcam,以及拍照和上传等按钮。
<link rel="stylesheet" type="text/css" href="fancybox/jquery.fancybox-1.3.4.css" /> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/ jquery.min.js"></script> <script type="text/javascript" src="fancybox/jquery.fancybox-1.3.4.pack.js"></script> <script type="text/javascript" src="js/webcam.js"></script> <script type="text/javascript" src="js/script.js"></script>CSS
#photos{width:80%; margin:40px auto} #photos:hover a{opacity:0.5} #photos a:hover{opacity:1} #camera{width:598px; height:525px; position:fixed; bottom:-466px; left:50%; margin-left:-300px; border:1px solid #f0f0f0; background:url(images/cam_bg.jpg) repeat-y; -moz-border-radius: 4px 4px 0 0; -webkit-border-radius:4px 4px 0 0; border-radius:4px 4px 0 0; -moz-box-shadow: 0 0 4px rgba(0,0,0,0.6); -webkit-box-shadow:0 0 4px rgba(0,0,0,0.6); box-shadow: 0 0 4px rgba(0,0,0,0.6);} #cam{width:100%; height:66px; display:block; position:absolute; top:0; left:0; background: url(images/cam.png) no-repeat center center; cursor:pointer} #webcam{width:520px; height:370px; margin:66px auto 22px; line-height:360px; background:#ccc; color:#666; text-align:center} .button_pane{text-align:center;} .btn_blue,.btn_green{width:99px; height:38px; line-height:32px; margin:0 4px; border:none; display:inline-block; text-align:center; font-size:14px; color:#fff !important; text-shadow:1px 1px 1px #277c9b; background:url(images/buttons.png) no-repeat} .btn_green{background:url(images/buttons.png) no-repeat right top; text-shadow:1px 1px 1px #498917;} .hidden{display:none} 这样你在预览index.html时会发现在页面的正下方有一个摄像头按钮,默认是折叠的。script.js-Part 1$(function(){webcam.set_swf_url("js/webcam.swf"); //载入flash摄像组件的路径webcam.set_api_url("upload.php"); // 上传照片的PHP后端处理文件webcam.set_quality(80);// 设置图像质量webcam.set_shutter_sound(true, "js/shutter.mp3"); //设置拍照声音,拍照时会发出“咔嚓”声var cam = $("#webcam");cam.html(webcam.get_html(cam.width(), cam.height()) //在#webcam中载入摄像组件); script.js-Part 2var camera = $("#camera"); var shown = false; $("#cam").click(function(){if(shown){camera.animate({ bottom:-466});}else {camera.animate({ bottom:-5});}shown = !shown; }); script.js-Part 3//拍照 $("#btn_shoot").click(function(){webcam.freeze(); //冻结webcam,摄像头停止工作$("#shoot").hide(); //隐藏拍照按钮$("#upload").show(); //显示取消和上传按钮return false; }); //取消拍照 $("#btn_cancel").click(function(){webcam.reset(); //重置webcam,摄像头重新工作$("#shoot").show(); //显示拍照按钮$("#upload").hide(); //隐藏取消和上传按钮return false; }); //上传照片 $("#btn_upload").click(function(){webcam.upload(); //上传webcam.reset();//重置webcam,摄像头重新工作$("#shoot").show();//显示拍照按钮$("#upload").hide(); //隐藏取消和上传按钮return false; }); $folder = "uploads/"; //上传照片保存路径 $filename = date("YmdHis").rand().".jpg"; //命名照片名称 $original = $folder.$filename; $input = file_get_contents("php://input"); if(md5($input) == "7d4df9cc423720b7f1f3d672b89362be"){ exit; //如果上传的是空白的照片,则终止程序 } $result = file_put_contents($original, $input); if (!$result) { echo "{"error":1,"message":"文件目录不可写";}"; exit; } $info = getimagesize($original); if($info["mime"] != "image/jpeg"){ //如果类型不是图片,则删除 unlink($original); exit; } //生成缩略图 $origImage = imagecreatefromjpeg($original); $newImage = imagecreatetruecolor(154,110); //缩略图尺寸154x110 imagecopyresampled($newImage,$origImage,0,0,0,0,154,110,520,370); imagejpeg($newImage,"uploads/small_".$filename); //写入数据库 include_once("connect.php"); $time = mktime(); $sql = "insert into photobooth (pic,uploadtime)values("$filename","$time")"; $res = mysql_query($sql); if($res){ //输出JSON信息 echo "{"status":1,"message":"Success!","filename":"".$filename.""}"; }else{ echo "{"error":1,"message":"Sorry,something goes wrong.";}"; } webcam.set_hook("onComplete", function(msg){ msg = $.parseJSON(msg); //解析json if(msg.error){ alert(msg.message); } else { var pic = "<a rel="group" href="uploads/"+msg.filename+""><img src="uploads/small_"+ msg.filename+""></a>"; $("#photos").prepend(pic); //将获取的照片信息插入到index.html的#photo里 initFancyBox(); //调用fancybox插件 } }); webcam.set_hook("onError",function(e){ cam.html(e); }); //调用fancybox function initFancyBox(){ $("a[rel=group]").fancybox({ "transitionIn" : "elastic", "transitionOut" : "elastic", "cyclic" : true }); } function loadpic(){$.getJSON("getpic.php",function(json){if(json){ $.each(json,function(index,array){ //循环json数据 var pic = "<a rel="group" href="uploads/"+array["pic"]+""> <img src="uploads/small_"+array["pic"]+""></a>"; $("#photos").prepend(pic); });}initFancyBox(); //调用fancybox插件}); } include_once("connect.php"); //连接数据库 //查询数据表中最新的50条记录 $query = mysql_query("select pic from photobooth order by id desc limit 50"); while($row=mysql_fetch_array($query)){$arr[] = array("pic" => $row["pic"]); } //输出json数据 echo json_encode($arr); 最后,附上数据photobooth结构:CREATE TABLE `photobooth` (`id` int(11) NOT NULL auto_increment,`pic` varchar(50) NOT NULL,`uploadtime` int(10) NOT NULL,PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;