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

如何改进脚本通过Google自定义搜索API获取高质量图片

如何通过Google自定义搜索API获取高质量/指定规格图片

我编写了一个脚本,可基于任意查询词通过Google自定义搜索API获取搜索到的第一张图片,功能正常但获取的图片质量偏低。请问如何修改代码,使其能获取特定像素规格或更高质量的图片?相关代码片段如下:

function getImageUrl(searchTerm, callback, errorCallback) {
    // Google image search - 100 searches per day.
    // https://developers.google.com/image-search/
    // var searchUrl = 'https://ajax.googleapis.com/ajax/services/search/images' +
    //   '?v=1.0&q=' + encodeURIComponent(searchTerm);
    var searchUrl = 'https://www.googleapis.com/customsearch/v1' +
      '?key=' + key + '&cx=' + cx + '&searchType=image&q=' + encodeURIComponent(searchTerm);
  
    var x = new XMLHttpRequest();
    x.open('GET', searchUrl);
    // The Google image search API responds with JSON, so let Chrome parse it.
    x.responseType = 'json';
    x.onload = function() {
      // Parse and process the response from Google Image Search.
      var response = x.response;
      if (!response || !response.items || response.items.length === 0) {
        errorCallback('No response from Google Image search!');
        return;
      }
      var firstResult = response.items[0];
      // Take the thumbnail instead of the full image to get an approximately
      // consistent image size.
      var imageUrl = firstResult.image.thumbnailLink;
      var width = parseInt(firstResult.image.thumbnailWidth);
      var height = parseInt(firstResult.image.thumbnailHeight);
      console.assert(
          typeof imageUrl == 'string' && !isNaN(width) && !isNaN(height),
          'Unexpected respose from the Google Image Search API!');
      callback(imageUrl, width, height, searchTerm);
    };
    x.onerror = function() {
      errorCallback('Network error.');
    };
    x.send();
  }

修改方案

要获取高质量或指定规格的图片,需要从API请求参数和结果链接选择两部分调整:

1. 给API请求添加图片筛选参数

Google自定义搜索API支持多个图片过滤参数,可根据需求添加到请求URL中:

  • imgSize=large:筛选大尺寸图片;imgSize=xlarge可选特大尺寸
  • imgWidth>=1920&imgHeight>=1080:指定最小像素规格(需确保参数格式正确)
  • imgType=photo:优先返回实拍照片类的高质量图片

修改后的请求URL示例:

var searchUrl = 'https://www.googleapis.com/customsearch/v1' +
  '?key=' + key + '&cx=' + cx + '&searchType=image&q=' + encodeURIComponent(searchTerm) +
  '&imgSize=large&imgType=photo';

2. 改用原图链接而非缩略图

原代码里使用的thumbnailLink是低质量缩略图,直接替换为原图的链接字段即可:

  • 用firstResult.link或firstResult.image.contentLink获取原图地址
  • 同时替换宽高字段为原图的width和height

修改后的响应处理部分代码:

var firstResult = response.items[0];
// 改用原图链接和原图尺寸
var imageUrl = firstResult.image.contentLink; // 或直接用firstResult.link
var width = parseInt(firstResult.image.width);
var height = parseInt(firstResult.image.height);

修改后的完整函数

function getImageUrl(searchTerm, callback, errorCallback) {
    var searchUrl = 'https://www.googleapis.com/customsearch/v1' +
      '?key=' + key + '&cx=' + cx + '&searchType=image&q=' + encodeURIComponent(searchTerm) +
      '&imgSize=large&imgType=photo'; // 添加筛选参数
  
    var x = new XMLHttpRequest();
    x.open('GET', searchUrl);
    x.responseType = 'json';
    x.onload = function() {
      var response = x.response;
      if (!response || !response.items || response.items.length === 0) {
        errorCallback('No response from Google Image search!');
        return;
      }
      var firstResult = response.items[0];
      // 使用原图链接和尺寸
      var imageUrl = firstResult.image.contentLink;
      var width = parseInt(firstResult.image.width);
      var height = parseInt(firstResult.image.height);
      console.assert(
          typeof imageUrl == 'string' && !isNaN(width) && !isNaN(height),
          'Unexpected respose from the Google Image Search API!');
      callback(imageUrl, width, height, searchTerm);
    };
    x.onerror = function() {
      errorCallback('Network error.');
    };
    x.send();
  }

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 07:34:55