【问题标题】:converting blob data into image using php使用php将blob数据转换为图像
【发布时间】:2016-11-01 08:39:28
【问题描述】:

我有一个页面,我在其中使用输入类型 = 文件将图像上传到使用此代码的图像标签

$('#profile-image-upload').change( function(event) {
var path = URL.createObjectURL(event.target.files[0]);

$("#image").attr('src',URL.createObjectURL(event.target.files[0]));

这给了我一个像这样 blob:http://localhost:8080/467d02c9-0af0-448a-a239-69e8d4037dd1 的路径的 bolb 数据。我的图像在路径变量中保存的这个 blob 数据中

现在我还希望将此图像文件保存在我的服务器目录中,并使用 ajax 将其发送到我的服务器

var ajax;

    if (window.XMLHttpRequest)  // For modren Browsers
      ajax=new XMLHttpRequest();

    else
      ajax=new ActiveXObject("Microsoft.XMLHTTP"); // For IE5 & IE8

      ajax.open("POST","../UploadImg.php",true);

      ajax.setRequestHeader("Content-type","application/x-www-form-urlencoded");
     var data=  "path="+path+"&type="+user+"&id="+id+"&extent="+extent;


     //path = blob data       
     //user = user name
     //id = user id
     //extent = extension of image

      ajax.onreadystatechange=function()
      {
        if (ajax.readyState==4 && ajax.status==200){
            alert(ajax.responseText);
        }
      }

     ajax.send(data);

});

我的服务器端编码是

$img_ff = $_POST["path"]; // blob path
$user = $_POST["type"];
$id = $_POST["id"];
$extension = $_POST["extent"];

$dst_img= $user.$id.".".$extention;  // for example user17.png
$dst_path= "localhost:8080/live4others/images/";

$dst_cpl = $dst_path . $dst_img;   

if(move_uploaded_file($dst_cpl, $img_ff)){
        echo "yes";
    }else{
        echo "error";
    }

    if( file_put_contents( $img_ff, $dst_cpl)){
        echo "yes";
    }else{
        echo "error";
    }

这两种方法都不起作用。请告诉我我在哪里做错了。他们给出错误并说给定的路径无效。

【问题讨论】:

    标签: javascript php image blob


    【解决方案1】:

    你可以试试这个。我使用的是相同的,但使用 blob 在提交之前简单地预览图像/视频。

    jQuery

    $('#<formId>').on('submit', function(e) {
        e.preventDefault();
        $.ajax({
            url: "../UploadImg.php",
            type: "POST",
            data:  new FormData(this),
            dataType: "json",
            contentType: false,
            cache: false,
            processData:false,
            success: function(response){
                console.log(response);
            }, 
            error : function (jqXHR, textStatus, errorThrown) {
            }
        });
    });
    

    PHP - UploadImg.php

    $tmpFile  = $_FILES['<fileElementName>'];
    
    $absolutePathOfdestinationFolder = '/var/www/<projectDocRoot>/images/'.$user.$id.".".$extention;
    
    if(move_uploaded_file($tmpFile['tmp_name'], $absolutePathOfdestinationFolder)){
        echo "yes";
    }else{
        echo "error";
    }
    

    【讨论】:

    • 我在 textstatus 和 errorThrown Unexpected token
    • $_FILES[''];没有在 $tmpFile 中保存 blob 数据,并给出“解析错误:语法错误,意外 '$tmpFile' (T_VARIABLE)”的错误
    猜你喜欢
    • 2020-07-20
    • 2011-08-31
    • 1970-01-01
    • 1970-01-01
    • 2017-07-17
    • 2016-05-29
    • 1970-01-01
    • 2017-11-13
    相关资源
    最近更新 更多