保存到目录的Base64_decoded图像字符串不显示图像

时间:2018-06-05 15:36:04

标签: php image file-put-contents

我正在尝试将我的Android应用程序中的图像上传到我服务器上的php脚本。在我的脚本中,我试图解码图像(使用base64_decode),然后使用file_put_contents()将图像保存为我的目录中的文件。我的问题是该文件出现了'当我在文件名末尾有.jpg时为空。当我删除它以查看为图像编码添加的内容时,我看到一个很长的字符串,(特别是65214字节写入文件)。当我再次运行代码时,只有这次上传$ _POST [' sent_image']而没有解码,我得到完全相同的文本字符串。

我不确定我做错了什么...最终目标是将图像保存在服务器上,因此可以在其他地方在线查看,也可以检索它并返回到另一个活动中我的android应用程序。

所有建议都表示赞赏!

注意:我也尝试过imagecreatefromstring(),但这会导致写入0个字节。

我的代码:PHP获取编码的android映像并尝试保存到服务器目录:

<?php

include('inc.php');

if ((isset($_POST['searchinput'])) && (isset($_POST['newUnitStatus'])) && (isset($_POST['generalCause'])) && (isset($_POST['newUnitStatusComment'])) && (isset($_POST['newUnitStatusPhoto'])) && (isset($_POST['lexauser'])) && (isset($_POST['password']))) {

    $sgref = "";
    $searchinput = $_POST['searchinput'];
    $newUnitStatus = $_POST['newUnitStatus'];
    $generalCause = $_POST['generalCause'];
    $newUnitStatusComment = $_POST['newUnitStatusComment'];
    $lexauser = $_POST['lexauser'];
    $pass = $_POST['password'];

    if ((strpos($searchinput, "/") !== false)) {

        $barcodesplit = preg_split('/\D/im', $searchinput, 4);
        $sgref = $barcodesplit[0];
        $lineitem = $barcodesplit[1];
        $unitnumber = $barcodesplit[2];
        $totalunits = $barcodesplit[3];
        $unitname = $sgref."-".$lineitem."-".$unitnumber."_of_".$totalunits;

        $photo = $_POST['newUnitStatusPhoto'];
        $decodedPhoto = str_replace('data:image/jpg;base64,', '', $photo);
        $decodedPhoto = str_replace(' ', '+', $decodedPhoto);
        $newUnitStatusPhoto = base64_decode($decodedPhoto);
        //$newUnitStatusPhoto = imagecreatefromstring($decodedPhoto);

        $fileName = "".$unitname."_rej";
        $target = '../LEXA/modules/bms/uploads/';
        $newFile = $target.$fileName;
        $docType = "Reject";

        $success = file_put_contents($newFile, $newUnitStatusPhoto);

        if($success === false) {
            $response['message'] = "Couldn not write file.";
            echo json_encode($response);
        } else {
            $response['message'] = "Wrote $success bytes. ";
            echo json_encode($response);
        }

    } else {
        $sgref = $searchinput;
        $response['message'] = "I'm sorry, but you must enter a unit's uniqueid value to add a unit exception. Please view the siblings for this SG and pick the unit you need. Then you can add the new status.";
        echo json_encode($response);
    }

} else {

    $response['message'] = "Your search value did not get sent. Please try     again.";
    echo json_encode($response);
}//End logic for post values.
?>

谢谢!

1 个答案:

答案 0 :(得分:0)

例如,如果图像格式不是jpg,则使用str_replace可能会出现问题。

示例代码:

<?php
$photo = $_POST['newUnitStatusPhoto'];



if(substr($photo, 0,5) !== "data:"){
    //do error treatment as it's not datauri
    die("Error: no data: scheme");
};
$decodedPhoto = substr($photo, 5);
$mimeTerminator = stripos($decodedPhoto,";");
if($mimeTerminator === false){
    die("Error: no mimetype found");
};
$decodedPhoto = substr($decodedPhoto, $mimeTerminator+8); //1<;>+4<base>+2<64>+1<,>

// $decodedPhoto = str_replace('data:image/jpg;base64,', '', $photo);
// $decodedPhoto = str_replace(' ', '+', $decodedPhoto);
$newUnitStatusPhoto = base64_decode($decodedPhoto);
//$newUnitStatusPhoto = imagecreatefromstring($decodedPhoto);
$unitname = "testando";
$fileName = "".$unitname."_rej.jpg";
$target = 'img/';
$newFile = $target.$fileName;
if(file_exists($newFile))
    unlink($newFile);

$success = file_put_contents($newFile, $newUnitStatusPhoto);
echo $success;