简体   繁体   English

如何从webcam.js上传Word64中的base64图像

[英]How to upload a base64 image inside Wordpress from webcam.js

I am running into some trouble with a code and I was hoping somebody could bring some light :) 我在编写代码时遇到了麻烦,我希望有人能带来一些启发:)

I am using webcamjs on a project that is like a small CRM inside a Wordpress website. 我在像Wordpress网站中的小型CRM的项目中使用webcamjs。 I've created a form using ACF and we fill the forms from the frontend of the website, which is only visible to logged in users. 我已经使用ACF创建了一个表单,我们从网站的前端填充了表单,只有登录的用户才能看到该表单。

In the members page, I need to be able to take a picture from the webcam, and so far I have the webcam working and producing a base64 image. 在会员页面中,我需要能够从网络摄像头拍照,到目前为止,我已经使网络摄像头工作并生成base64图像。

What I need to do now, is be able to upload this image to the upload directory within WordPress and attach the link of the image to the field "photo" using the ACF plugin, so that all the information goes together. 我现在需要做的是,能够将该图像上传到WordPress中的上传目录,并使用ACF插件将图像的链接附加到“照片”字段,以便所有信息一起存储。 But this part is not working. 但是这部分不起作用。

I wonder if I am calling the PHP file correctly from within the JS code and if the information from the JS is going to the PHP file. 我想知道我是否从JS代码中正确调用了PHP文件,以及来自JS的信息是否进入了PHP文件。

Any help is highly appreciated! 任何帮助深表感谢!

<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>

This is the "uploader.php" file 这是“ 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;

I got it! 我知道了!

The problem was with my "uploader.php" file. 问题出在我的“ uploader.php”文件上。 It now looks like this: 现在看起来像这样:

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);

I also had to go and create the directory manually. 我还必须手动创建目录。 So if anyone has a better solution, I am open to it :) 因此,如果有人有更好的解决方案,我会开放的:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM