PHP code example of ginsen / img-finder

1. Go to this page and download the library: Download ginsen/img-finder library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ginsen / img-finder example snippets




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'
                ]
            ]
        ]
    ]
];

$file = '/your/path/img-finder.yml';

$config = ImgFinder\Config::fromYaml($file);
$finder = new ImgFinder\ImgFinder($config);

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);

// 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);

// 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"
 ]
 */


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);