FlashとjQueryを組み合わせたプラグインでWebカメラで撮影した画像をアップロード
2024/01/13
FlashとjQueryを組み合わせたプラグインを用いてWebカメラで撮影した画像をフォーム等でアップロードする方法のメモ。jQuery webcam pluginというのがswfも同梱されてて非常に便利だった。
jQuery webcam plugin
http://www.xarg.org/project/jquery-webcam-plugin/
使い方
撮影ページ
<?php
$id = md5(uniqid(rand(), true));
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div id="jquery_camera"></div>
<button id="btn">キャプチャ</button>
</div>
<?php if($_GET['id']){ ?>
<img src="<?php echo $_GET['id']?>.jpg">
<?php } ?>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="./jquery.webcam.min.js"></script>
<script type="text/javascript">
$(function(){
var width = 320;
var height = 240;
$("#jquery_camera").webcam({
width: width,
height: height,
mode: "save",
swffile: "./jscam.swf",
onTick: function() {},
onSave : function () {
window.location = "./index.php?id=<?php echo $id;?>";
},
// Capture時に呼び出される
onCapture: function() {
webcam.save('./upload.php?id=<?php echo $id;?>');
},
debug: function() {},
onLoad: function() {
$("#btn").click(function(){
webcam.capture();
});
}
});
})
</script>
</body>
</html>
保存ページ
<?php
$name = $_REQUEST['id'];
$str = file_get_contents("php://input");
file_put_contents("/hoge/webcam/{$name}.jpg", pack("H*", $str));
?>
その他
これならIEでも撮影できる。ただスマホだとflash使えない場合が多いので、他の方法も探しておいたほうが良さそう。
関連記事
-
-
jQueryで偶数or奇数行に任意のClassを付与する方法
tableタグの1行目を除く偶数もしくは奇数行のtrタグに任意にClassを指定 ...
-
-
Lightboxで画像拡大時に文字タイトルとリンクをつける
lightboxで画像をクリックして拡大した際に、文字タイトルとその文字にリンク ...
-
-
CodeIgniter4 & jQueryを用いて同一サイト内でajax通信する方法
CodeIgniter4.4.4 & jQueryを用いて同一サイト内で ...
-
-
CodeIgniter4&Bootstrap&jQueryで簡易版お問い合わせページの作成
CodeIgniter4.4.4&Bootstrap&jQuer ...
-
-
tableのフィルター機能
jQueryのプラグインで簡単にフィルター機能が つけられたのでメモ。 ■必要な ...