![](/img/trans.png)
[英]I'm getting corrupted image when converting base64 image using webcam.js
[英]How to upload a base64 image inside Wordpress from webcam.js
我在编写代码时遇到了麻烦,我希望有人能带来一些启发:)
我在像Wordpress网站中的小型CRM的项目中使用webcamjs。 我已经使用ACF创建了一个表单,我们从网站的前端填充了表单,只有登录的用户才能看到该表单。
在会员页面中,我需要能够从网络摄像头拍照,到目前为止,我已经使网络摄像头工作并生成base64图像。
我现在需要做的是,能够将该图像上传到WordPress中的上传目录,并使用ACF插件将图像的链接附加到“照片”字段,以便所有信息一起存储。 但是这部分不起作用。
我想知道我是否从JS代码中正确调用了PHP文件,以及来自JS的信息是否进入了PHP文件。
任何帮助深表感谢!
<script language="JavaScript">
Webcam.set({
width: 640,
height: 480,
image_format: 'jpeg',
jpeg_quality: 90
});
Webcam.attach( '#members_camera' );
function take_snapshot() {
// take snapshot and acquire image data
Webcam.snap( function(data_uri) {
// snap complete, image data is in 'data_uri'
Webcam.on( 'uploadProgress', function(progress) {
// Upload in progress
// 'progress' will be between 0.0 and 1.0
} );
Webcam.on( 'uploadComplete', function(code, text) {
location.reload();
// Upload complete!
// 'code' will be the HTTP response code from the server, e.g. 200
// 'text' will be the raw response content
} );
var postid = '<?php echo $postid ?>';
var url = '<?php echo get_stylesheet_directory_uri() . '/inc/webcam/uploader.php?postid='; ?>' + postid;
Webcam.upload( data_uri, url, function(code, text) {
// Upload complete!
// 'code' will be the HTTP response code from the server, e.g. 200
// 'text' will be the raw response content
} );
} );
}
</script>
这是“ uploader.php”文件
require_once("../../../../../wp-load.php");
$name = date('YmdHis');
$imagename = $_SERVER['DOCUMENT_ROOT']."/wp-content/uploads/members_photos/".$name.".jpg";
move_uploaded_file($_FILES['webcam']['tmp_name'], $imagename);
$postid = $_GET['postid'];
update_field('foto', $imagename, $postid);
echo $postid;
echo $imagename;
我知道了!
问题出在我的“ uploader.php”文件上。 现在看起来像这样:
require_once('../../../../../wp-load.php');
$name = date('YmdHis');
$upload_dir = wp_upload_dir();
$imagename = $upload_dir['basedir'] . '/members_photos/' . $name . '.jpg';
move_uploaded_file($_FILES['webcam']['tmp_name'], $imagename);
$home_url = home_url();
$member_pic_url = $home_url . '/wp-content/uploads/members_photos/' . $name . '.jpg';
$postid = $_GET['postid'];
update_field('foto', $member_pic_url, $postid);
我还必须手动创建目录。 因此,如果有人有更好的解决方案,我会开放的:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.