You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Cloud Vision产品搜索传入base64格式图片返回null问题求助

问题原因及修复方案

核心原因

你当前的问题出在对Google Cloud Vision PHP客户端的入参规则理解错误:

  • ImageAnnotatorClient的productSearch方法如果接收字符串类型的图片参数,仅会在字符串以gs:///http(s)://开头时识别为资源路径,其他情况默认将字符串当作图片原始二进制流处理。你手动对图片内容做了base64编码后传入,相当于给服务端提交了无法识别的无效图片数据,因此返回空结果。

修复方案

有两种可行的修改方式:

方案1:直接传入图片原始二进制(推荐)

删除多余的base64编码逻辑,直接传递文件读取的二进制内容即可:

// 去掉base64_encode,直接取二进制
$image = file_get_contents(public_path('gallery/test/wendy-search.png'));
$response = $imageAnnotatorClient->productSearch($image, $productSearchParams);

方案2:显式构造Image对象传入base64内容

如果业务场景必须使用base64编码格式,需要手动构造官方的Image对象指定content参数,不能直接传base64字符串:

use Google\Cloud\Vision\V1\Image;

// ...其他原有代码保持不变
$imageContent = base64_encode(file_get_contents(public_path('gallery/test/wendy-search.png')));
$image = (new Image())->setContent($imageContent);
$response = $imageAnnotatorClient->productSearch($image, $productSearchParams);

注:两种方案都不需要修改其他ProductSearch相关配置,你现有的产品集、关联配置都是正常的。

内容的提问来源于stack exchange,提问作者general666

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 05:54:04