Base 64 编码图像。上传推特头像

发布于 2024-11-27 02:00:39 字数 966 浏览 0 评论 0原文

我正在尝试为 Twitter 制作一个图像上传表单来更改头像。 Twitter 说在我上传图片之前必须对其进行 Base 64 编码。这是一个示例

https://dev.twitter.com/docs/api /1/post/account/update_profile_image

我该怎么做到目前为止我一直收到错误

PHP 代码:

<?php // process form data 
    if ($_POST['image']){ 
        // post profile update 
        $post_data = array( 
            "image" => $_POST['image'] 
        ); 

         $post_data = base64_encode($post_data); 

        echo $post_data; 
        } 
        else{ 
        echo     
        "  <form action='post.php' method='POST' enctype='multipart/form-data'> 
<br><input type='hidden' name='image' value='update_profile_image'> 

    <input type='file' name='image' size='30'/><br> 

    <input type='submit' value='Upload' class='button'/> 


    </form>";} 

    ?>

请帮忙

I'm trying to make an image upload form for twitter to change avatar. Twitter says the image has to be base 64 encoded before I upload it. So here's a sample

https://dev.twitter.com/docs/api/1/post/account/update_profile_image

How can I do this so far I've been getting errors

PHP Code:

<?php // process form data 
    if ($_POST['image']){ 
        // post profile update 
        $post_data = array( 
            "image" => $_POST['image'] 
        ); 

         $post_data = base64_encode($post_data); 

        echo $post_data; 
        } 
        else{ 
        echo     
        "  <form action='post.php' method='POST' enctype='multipart/form-data'> 
<br><input type='hidden' name='image' value='update_profile_image'> 

    <input type='file' name='image' size='30'/><br> 

    <input type='submit' value='Upload' class='button'/> 


    </form>";} 

    ?>

Please help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

牵你手 2024-12-04 02:00:39

从表面上看,您根本没有从 POST 中检索图像。图像本身将在 $_FILES 中提供

// See what's in $_FILES
var_dump($_FILES);

// You need the temporary name of your image from $_FILES
$filedata = file_get_contents($_FILES['image']['tmp_name']);
$post_data = base64_encode($filedata);

// Now you should have a base64 ascii string
echo $post_data;

From the looks of this, you aren't retrieving the image from the POST at all. The image itself will be available in $_FILES

// See what's in $_FILES
var_dump($_FILES);

// You need the temporary name of your image from $_FILES
$filedata = file_get_contents($_FILES['image']['tmp_name']);
$post_data = base64_encode($filedata);

// Now you should have a base64 ascii string
echo $post_data;
比忠 2024-12-04 02:00:39

这应该可以做到。
将上传的文件移动到某个目录,因为通过加密[tmp_name]无法得到所需的结果

if ($_FILES['image']){

move_uploaded_file($_FILES["image"]["tmp_name"], "path" .basename($_FILES["image"]["name"]));
$meh = file_get_contents("path/" .basename($_FILES["image"]["name"]));
$results = base64_encode($meh);
}

This should do it.
Move the uploaded file to a directory because you cannot get required results by encrypting the [tmp_name]

if ($_FILES['image']){

move_uploaded_file($_FILES["image"]["tmp_name"], "path" .basename($_FILES["image"]["name"]));
$meh = file_get_contents("path/" .basename($_FILES["image"]["name"]));
$results = base64_encode($meh);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文