AWS Rekognition PHP SDK提供无效的图像编码错误

时间:2016-12-15 14:12:12

标签: php amazon-web-services amazon-rekognition

我正在使用PHP SDK上传要在AWS Rekognition中解析的本地文件(而不是S3)。但是,图像blob不起作用,我收到消息:InvalidImageFormatException: "Invalid image encoding"

我尝试了多张图片(the docs say JPEGs and PNGs are accepted),但都没有效果。

我的代码是:

$client = new RekognitionClient($credentials);

$im = file_get_contents('/app/image1.png');
$imdata = base64_encode($im);

$result = $client->detectLabels(
    [
       'Image' => [
          'Bytes' => $imdata,
       ]
    ]
);

我编码正确吗? docs非常模糊。

我发现有关“无图像内容”的问题,但没有关于无效格式的问题。

有什么想法吗?谢谢!

3 个答案:

答案 0 :(得分:1)

我最终使用Imagick而不是base64_encode路线。我怀疑这不是最好的方法,但确实很有效!

$client = new RekognitionClient($credentials);

$image = new Imagick('/app/image1.png');
$imdata = $image->getImageBlob();

$result = $client->detectLabels(
    [
       'Image' => [
          'Bytes' => $imdata,
       ]
    ]
);

答案 1 :(得分:1)

看起来您不应该应用base64编码。 SDK为blob做它。

https://github.com/aws/aws-sdk-php/blob/master/src/Api/Serializer/JsonBody.php

        case 'blob':
            return base64_encode($value);

答案 2 :(得分:0)

    $s3 = new \Aws\Rekognition\RekognitionClient([
        'version' => 'latest',
        'region'  => 'us-east-1',
        'credentials' => [
            'key'    => 'BKxxxxxxxx',
            'secret' => 'GYxxxxxxxxxxxxxxxxxx'
        ]
    ]);

    $result = $s3->detectLabels([
        'Image' => [ // REQUIRED
            'Bytes' => file_get_contents("http://img13.deviantart.net/5a3b/i/2010/249/b/a/__michelangelo__s_flying_horse___by_dark_oak_trails-d2y5iej.jpg"),
        ],
        'MaxLabels' => 10,
        'MinConfidence' => 90,
    ]);

在使用SDK时,您不需要对base64_enconde图像。

http://docs.aws.amazon.com/rekognition/latest/dg/API_Image.html

  

如果您使用的是AWS SDK,则您的代码可能不需要编码图像字节...