ginsen / img-finder
在多个公共仓库中查找图片的应用
v4.0.1
2023-02-17 13:00 UTC
Requires
- php: >=8.0
- ext-json: *
- cocur/slugify: ^4.0
- nyholm/psr7: ^1.5
- psr/cache: ^1.0
- psr/http-client: ^1.0
- symfony/http-client: 5.4.*
- symfony/translation: 5.4.*
- symfony/yaml: 5.4.*
Requires (Dev)
- friendsofphp/php-cs-fixer: 3.8.*
- mockery/mockery: 1.5.*
- phpunit/phpunit: 9.5.*
- symfony/var-dumper: 5.4.*
Suggests
- symfony/cache: Recommened to implement psr/cache
README
PHP 库,用于在多个公共仓库中查找图片
安装
$ composer require ginsen/img-finder
配置文件
使用 YAML 格式
创建一个 YAML 格式的配置文件 img-finder.yml
,内容如下:
img-finder: repositories: ImgFinder\Repository\PexelsRepository: params: authorization: your-authorization # Visit https://www.pexels.com/es-es/api/new/ ImgFinder\Repository\UnsplashRepository: params: authorization: your-authorization # Visit https://unsplash.com/developers # Optional translators: ImgFinder\Translator\DictionaryYaml: no_cache: true params: filename: img-finder/doc/examples/yaml_dictionary.yml ImgFinder\Translator\GoogleTranslate: params: apikey: your-credentials from: es to: en
使用 PHP 格式
创建配置文件与 YAML 非常相似,创建 PHP 文件格式如下。
<?php use ImgFinder\Repository\PexelsRepository; use ImgFinder\Repository\UnsplashRepository; use ImgFinder\Translator\DictionaryYaml; use ImgFinder\Translator\GoogleTranslate; $settings = [ 'img-finder' => [ 'repositories' => [ PexelsRepository::class => [ 'params' => [ 'authorization' => 'your-authorization' # Visit https://www.pexels.com/es-es/api/new/ ] ], UnsplashRepository::class => [ 'params' => [ 'authorization' => 'your-authorization' # Visit https://unsplash.com/developers ] ] ], 'translators' => [ DictionaryYaml::class => [ 'no_cache' => true, 'params' => [ 'filename' => 'img-finder/doc/examples/yaml_dictionary.yml' ] ], GoogleTranslate::class => [ 'params' => [ 'apikey' => 'your-credentials', 'from' => 'es', 'to' => 'en' ] ] ] ] ];
并替换您用于合约服务的凭据,并删除没有凭据的仓库。
如果您没有使用 GoogleTranslate 的凭据,请移除此翻译服务,字典不需要凭据,您可以将其配置为将您想要翻译的搜索词翻译成英语,例如标签云。请注意,字典对免费文本搜索没有用。
使用
使用 YAML 格式
使用创建的 yml 文件创建一个配置实例,并将其注入到 主 ImgFinder 类 中。
$file = '/your/path/img-finder.yml'; $config = ImgFinder\Config::fromYaml($file); $finder = new ImgFinder\ImgFinder($config);
使用 PHP 格式
使用 PHP 配置文件,操作如下。
use ImgFinder\Repository\PexelsRepository; $settings = [ 'img-finder' => [ 'repositories' => [ PexelsRepository::class => [ 'params' => [ 'authorization' => 'your-authorization' # Visit https://www.pexels.com/es-es/api/new/ ] ], ], // ... ] //... ]; $config = ImgFinder\Config::fromArray($settings); $finder = new ImgFinder\ImgFinder($config);
完成
ImgFinder 现在可用于查询图片仓库,只需创建一个请求即可。
Pexels & Unsplash 仓库中的请求
// Search in pexels ad unsplash repositories $request = ImgFinder\Request::set('nature', ['pexels', 'unsplash']); // same as: /** * @param string $words The search term * @param array $repositories The used repositories * @param int $page Page number * @param int $perPage Items per page * @param string $orientation Orientation: 'landscape' or 'portrait', default: 'landscape' * @param int $width Width of photos, default 1200 pixels * @param int $widthSmall Width of small photos, default 320 pixels */ $request = ImgFinder\Request::set('nature', ['pexels', 'unsplash'], 1, 10, 'landscape', 1200, 320);
这两个请求相同,"nature" 是搜索词,1 是默认页,10 是每个仓库的响应图片数量,"landscape" 是方向('landscape' 或 'portrait'),宽度为 1200px,最后 320 是缩略图的宽度。
仅在一个仓库中搜索的请求
// Search in pexels repository $request = ImgFinder\Request::set('nature', ['pexels']); // same as: $request = ImgFinder\Request::set('nature', ['pexels'], 1, 10, 'landscape', 1200, 320);
搜索
最后,您只需执行搜索即可。
$response = $finder->search($request);
这是整个操作的全部代码。
use ImgFinder\Config; use ImgFinder\ImgFinder; use ImgFinder\Request; $file = '/your/path/img-finder.yml'; $config = Config::fromYaml($file); $finder = new ImgFinder($config); $request = Request::set('nature', ['pexels', 'unsplash']); $response = $finder->search($request); $imagesUrls = $response->toArray(); echo json_encode($imagesUrls); /** * [ * { * "author": "Rodolfo Quirós", * "url_author": "https://www.pexels.com/@rquiros", * "media": "https://images.pexels.com/photos/2219118/pexels-photo-2219118.jpeg?auto=compress&cs=tinysrgb&fit=crop&h=627&w=1200", * "thumbnail": "https://images.pexels.com/photos/2219118/pexels-photo-2219118.jpeg?auto=compress&cs=tinysrgb&h=350&w=320", * "repository": "pexels" * },{ * "author": "Igor Starkov", * "url_author": "https://unsplash.com/@igorstarkoff", * "media": "https://images.unsplash.com/photo-1595706480968-ca87913ee9c7?ixid=MXwxODk0OTN8MHwxfHNlYXJjaHwxMHx8YmVhdXR5JTIwZmFjZXxlbnwwfDB8fA&ixlib=rb-1.2.1", * "thumbnail": "https://images.unsplash.com/photo-1595706480968-ca87913ee9c7?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=MXwxODk0OTN8MHwxfHNlYXJjaHwxMHx8YmVhdXR5JTIwZmFjZXxlbnwwfDB8fA&ixlib=rb-1.2.1&q=80&w=320", * "repository": "unsplash" * }, * .... * ] */
显示所有公共仓库
要显示所有可用仓库。
... $finder = new ImgFinder($config); $finder->repositories(); /** array:10 [ 0 => "pexels" 1 => "unsplash" ] */
缓存可选
如果您愿意,可以缓存请求以提高性能,并减少对图片仓库的压力。
例如,如果您使用 symfony/cache
组件,您可以将它注入到初始配置中。
示例
$ composer require symfony/cache
创建 Redis 缓存服务。
use ImgFinder\Config; use Symfony\Component\Cache\Adapter\RedisAdapter; $redisConn = RedisAdapter::createConnection('redis://my.server.com:6379'); $cache = new RedisAdapter($redisConn, 'imgfinder', 60); $file = '/your/path/img-finder.yml'; $config = Config::fromYaml($file, $cache);