从angularJS抓取一个php base64图像

时间:2015-12-29 09:40:29

标签: php angularjs base64

我正试图从angularJS中获取一个64base编码的字符串,我不太确定我做错了什么。我编写了一个由php运行的makegray.exe写入服务器的映像。现在,我正试图抓取该图像并将其返回到angularJS,然后为用户显示它。这似乎是一个简单的问题,但我对如何做到这一点感到难过。

$scope.MakeGray_Button = function(){
    if ($scope.imageUrl) {
        var MakeGray_Form = new FormData();
        MakeGray_Form.append("FileName", $scope.imageUrl);
        $http({
        method : "POST",
        url    : "../opencv/MakeGray/MakeGray.php",
        data   : MakeGray_Form,
        transformRequest: angular.identity,
        headers: {'Content-Type': undefined}
        }).
        success(function(){                     
           // some magic code here that grabs the $base64 variable from php
        })
        .error(function(){});
    }
    else{
        alert("Please upload an image");
    }
}

PHP

<?php

$imgname = $_POST["FileName"];
$inputDir = "../../uploads/" . $imgname;
$outputDir = "../../processed/" . $imgname;

$MakeGray = "./makegray " . $inputDir . " " . $outputDir;
$runExec  = exec($MakeGray, $out);

$type = pathinfo($outputDir, PATHINFO_EXTENSION);

$data = file_get_contents($outputDir);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);

echo "$base64";

?>

2 个答案:

答案 0 :(得分:1)

$scope.MakeGray_Button = function(){
    if ($scope.imageUrl) {
        var MakeGray_Form = new FormData();
        MakeGray_Form.append("FileName", $scope.imageUrl);
        $http({
        method : "POST",
        url    : "../opencv/MakeGray/MakeGray.php",
        data   : MakeGray_Form,
        transformRequest: angular.identity,
        headers: {'Content-Type': undefined}
        }).then(function(response){                     
           //response object should hold your base64 image data.
           //you can change src of an img tag with ng-src and let the browser render your image.
        },function(response){});
    }
    else{
        alert("Please upload an image");
    }
}

首先使用.then()代替已弃用的函数.success().error()

然后也像上面的代码一样捕获response

您可以通过将src标记设为img来轻松渲染base64图像。 ng-src应该为此提供帮助。

如果您希望图像为字符串,则应该检查response对象。

答案 1 :(得分:0)

好的,我让它上班......

=IIf(Fields!cdr_charge_amount.Value < 0.01, "",("Php " &  FORMAT(...)))

希望这有助于其他人尝试使用base64编码的字符串从php向JS显示图像!