将Base64上传到S3

时间:2015-04-16 16:14:50

标签: javascript php html canvas amazon-s3

我试图在不使用输入或html格式的情况下将图像上传到S3。

我想要的流程如下:合并两个HTML画布,转换为base64,下载为图像,通过电子邮件发送图像,上传到S3。我目前的代码如下:

function convertImage() {
    var bottleCanvas = document.getElementById('bottleCanvas');
    var designCanvas = document.getElementById('editorCanvas');
    var bottleContext = bottleCanvas.getContext('2d');

    if($('.colourCanvas500').is(':visible')) {
        bottleContext.drawImage(designCanvas, 153, 250);
    }
    else if($('.colourCanvas600').is(':visible')) {
        bottleContext.drawImage(designCanvas, 155, 238);
    }
    else if($('.whiteCanvas500').is(':visible')) { 
        bottleContext.drawImage(designCanvas, 132, 235);
    }
    else if($('.whiteCanvas600').is(':visible')) {
        bottleContext.drawImage(designCanvas, 132, 235);
    }

    var data = bottleCanvas.toDataURL("image/png");
    var link = document.createElement('a');
    link.download = "bottle-design.png";
    var dataLink = bottleCanvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
    //document.write(dataLink);
    link.href = dataLink;
    link.click();

    $('#imageInput').val(data);

    $('form[name="bottleDetails"]').submit();
}

我有以下代码用于将徽标从文件上传到S3:

<?php
include('php/image_check.php');
$msg='';
if($_SERVER['REQUEST_METHOD'] == "POST")
{

$name = $_FILES['file']['name'];
$size = $_FILES['file']['size'];
$tmp = $_FILES['file']['tmp_name'];
$ext = getExtension($name);

if(strlen($name) > 0)
{

if(in_array($ext,$valid_formats))
{

if($size<(2048*2048))
{
include('php/s3_config.php');
//Rename image name. 
$actual_image_name = time().".".$ext;
if($s3->putObjectFile($tmp, $bucket , $actual_image_name, S3::ACL_PUBLIC_READ) )
{
$s3file='http://'.$bucket.'.s3.amazonaws.com/'.$actual_image_name;

session_start();
$_SESSION['uploadedImage'] = $s3file;
header('Location: builder.php');


}
else
$msg = "S3 Upload Fail.";


}
else
$msg = "Image size Max 2 MB";

}
else
$msg = "Invalid file, please upload image file.";

}
else
$msg = "Please select image file.";

}

?>

如何使用没有表单的PHP将我的base64 / canvas图像上传到S3?

1 个答案:

答案 0 :(得分:0)

您可以通过Ajax将base64字符串发送到您的服务器(到php文件)

来完成此操作

jQuery具有以下功能:https://api.jquery.com/jquery.ajax/