将图像 URL 转换为 base64
Converting image URL to base64
我正在尝试将图像 URL 转换为 base 64(图像本身)以在 AWS FacialRekognition 中使用,但代码中存在一些错误,它没有超过 indexFaces 所在的位置用作这里制作的错误日志是永远不会打印的。
有人可以告诉我我做错了什么吗?
$url= "https://www.thesprucepets.com/thmb/KYaXBSM013GnZ2jEZJnX4a9oIsU=/3865x2174/smart/filters:no_upscale()/horse-galloping-in-grass-688899769-587673275f9b584db3a44cdf.jpg";
$type = pathinfo($url, PATHINFO_EXTENSION);
$data = file_get_contents($url);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
$result = $client->indexFaces([
'CollectionId' => $id . "blablabla",
'DetectionAttributes' => ['DEFAULT'],
'MaxFaces' => 1,
'QualityFilter' => "NONE",
'Image' => [ // REQUIRED
'Bytes' => $base64
]
]);
error_log("MADE IT HERE");
如 Amazon Rekognition documentation, 中所述,如果您使用的是 AWS PHP SDK,则不需要对内容进行 base64。您只需要传递 file_get_contents.
的结果
所以你的代码会更简单:
$url= "https://www.thesprucepets.com/thmb/KYaXBSM013GnZ2jEZJnX4a9oIsU=/3865x2174/smart/filters:no_upscale()/horse-galloping-in-grass-688899769-587673275f9b584db3a44cdf.jpg";
$result = $client->indexFaces([
'CollectionId' => $id . "blablabla",
'DetectionAttributes' => ['DEFAULT'],
'MaxFaces' => 1,
'QualityFilter' => "NONE",
'Image' => [ // REQUIRED
'Bytes' => file_get_contents($url);
]
]);
error_log("MADE IT HERE");
如果还是报错,可能不是Base64编码的问题。请用 Try/Catch 和 post 异常
包围您的代码
我正在尝试将图像 URL 转换为 base 64(图像本身)以在 AWS FacialRekognition 中使用,但代码中存在一些错误,它没有超过 indexFaces 所在的位置用作这里制作的错误日志是永远不会打印的。
有人可以告诉我我做错了什么吗?
$url= "https://www.thesprucepets.com/thmb/KYaXBSM013GnZ2jEZJnX4a9oIsU=/3865x2174/smart/filters:no_upscale()/horse-galloping-in-grass-688899769-587673275f9b584db3a44cdf.jpg";
$type = pathinfo($url, PATHINFO_EXTENSION);
$data = file_get_contents($url);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
$result = $client->indexFaces([
'CollectionId' => $id . "blablabla",
'DetectionAttributes' => ['DEFAULT'],
'MaxFaces' => 1,
'QualityFilter' => "NONE",
'Image' => [ // REQUIRED
'Bytes' => $base64
]
]);
error_log("MADE IT HERE");
如 Amazon Rekognition documentation, 中所述,如果您使用的是 AWS PHP SDK,则不需要对内容进行 base64。您只需要传递 file_get_contents.
的结果所以你的代码会更简单:
$url= "https://www.thesprucepets.com/thmb/KYaXBSM013GnZ2jEZJnX4a9oIsU=/3865x2174/smart/filters:no_upscale()/horse-galloping-in-grass-688899769-587673275f9b584db3a44cdf.jpg";
$result = $client->indexFaces([
'CollectionId' => $id . "blablabla",
'DetectionAttributes' => ['DEFAULT'],
'MaxFaces' => 1,
'QualityFilter' => "NONE",
'Image' => [ // REQUIRED
'Bytes' => file_get_contents($url);
]
]);
error_log("MADE IT HERE");
如果还是报错,可能不是Base64编码的问题。请用 Try/Catch 和 post 异常
包围您的代码