How to integrate Adobe Stock with the Laravel PHP Framework

Summary
This tutorial explains how you can call the Adobe Stock API from a Laravel install. This can be quickly applied to any other PHP application.

Demo of the Adobe Stock image search and license in the WordPress plugin StockPack

Prerequisites
You will need:

  1. A Laravel installation (code can be adapted easily for any PHP application) – if you want to learn more about Laravel you can find it here: https://laravel.com
  2. Composer

Add the Adobe Stock PHP library to Laravel

First, add the following to your composer.json file:

    "require-dev": {
        "astock/stock-api-libphp": "^1.1.2"
    },

Then run composer install

Create the Adobe Stock Controller

Create a new file called AdobeStockController.php and add it to app/Http/Controllers/

Next, add the following code in the file. We will explain all of it right after, but for now, you can copy-paste it.

<?php

namespace App\Http\Controllers;

use AdobeStock\Api\Core\Constants;
use AdobeStock\Api\Request\SearchFiles;
use Illuminate\Http\Request;
use AdobeStock\Api\Client\AdobeStock;
use AdobeStock\Api\Client\Http\HttpClient;
use AdobeStock\Api\Models\SearchParameters;

class AdobeStockController extends Controller {
    public function search( Request $request ) {
        // initialize client
        $client = new AdobeStock( env( 'ADOBE_API_KEY' ), env( 'API_REFERRAL' ), 'PROD', new HttpClient() );

        // set the params object
        $search_params = new SearchParameters();
        $search_params->setWords( $request->get( 'query' ) );
        $search_params->setLimit( 10 )->setOffset( 0 );

        // create the search request
        $search_request = new SearchFiles();
        $search_request->setLocale( 'En_US' );
        $search_request->setSearchParams( $search_params );
        $search_request->setResultColumns( $this->result_columns() );

        // initialize the files search
        $search_files_response = $client->searchFilesInitialize( $search_request );

        return array_map( array( $this, 'convert' ), $search_files_response->getNextResponse()->files );

    }

    // specify the columns that should be returned
    private function result_columns() {
        $results_columns = Constants::getResultColumns();

        return [
            $results_columns['NB_RESULTS'],
            $results_columns['WIDTH'],
            $results_columns['HEIGHT'],
            $results_columns['TITLE'],
            $results_columns['THUMBNAIL_URL'],
            $results_columns['THUMBNAIL_220_URL'],
            $results_columns['THUMBNAIL_220_HEIGHT'],
            $results_columns['THUMBNAIL_220_WIDTH'],
            $results_columns['THUMBNAIL_500_URL'],
            $results_columns['THUMBNAIL_500_HEIGHT'],
            $results_columns['THUMBNAIL_500_WIDTH'],
            $results_columns['THUMBNAIL_1000_URL'],
            $results_columns['THUMBNAIL_1000_HEIGHT'],
            $results_columns['THUMBNAIL_1000_WIDTH'],
            $results_columns['ID'],
        ];
    }

    private function convert( $image ) {

        $ratio = $this->ratio( $image );

        return [
            'id'          => $this->getProp( $image, 'id' ),
            'description' => $this->description( $image ),
            'url'         => $this->getProp( $image, 'thumbnail_url' ),
            'download'    => $this->getProp( $image, 'thumbnail_1000_url' ),
            'sizes'       => [
                'full'      => $this->size( $image, 'thumbnail_1000', $ratio ),
                'medium'    => $this->size( $image, 'thumbnail_500', $ratio ),
                'thumbnail' => $this->size( $image, 'thumbnail_220', $ratio )
            ]
        ];

    }

    private function getProp( $image, $prop ) {
        if ( is_array( $image ) ) {
            return $image[ $prop ];
        }

        return $image->$prop;
    }

    private function ratio( $image ) {
        return $this->getProp( $image, 'width' ) / $this->getProp( $image, 'height' );
    }

    private function description( $image ) {
        $description = $this->getProp( $image, 'description' );

        return $description ?? $this->getProp( $image, 'title' );

    }

    private function size( $image, $size, $ratio ) {
        $width = $this->width( $image, $size );

        return [
            'height'      => $this->height( $width, $ratio ),
            'width'       => $width,
            'orientation' => ( $ratio > 1 ? 'landscape' : 'portrait' ),
            'url'         => $this->getProp( $image, $size . '_url' )
        ];
    }

    private function width( $image, $size ) {
        return $this->getProp( $image, $size . '_width' );
    }

    private function height( $width, $ratio ) {
        return round( $width / $ratio );
    }
}

Add the web route to routes/web.php

<?php

Route::get( '/adobe-stock-search', [
    'as'         => 'adobe-stock-search',
    'uses'       => 'AdobeStockController@search',
] );

Get the Adobe Stock api key

To access the Adobe Stock api you need an api key. To get one you will need to access the Adobe I/O Console. Register/Login and then click the Create integration button

Adobe I/O Console add integration button

On the next screen, check the Access an API checkbox. On the second screen choose Adobe Stock under Creative Cloud.

Select Adobe Stock in the Adobe I/O Console

On the final screen, you can add any name and description. You can defer this decision until you submit for production limits. The same applies to the Redirect fields.

Click the Create integration button in the bottom left. Then, on the integration screen copy the API Key. Add the key into your Laravel env file.

ADOBE_API_KEY="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

Add the API_REFERRAL="Demo Integration" to the env file. This is required as a Referral for the Adobe Stock API.

Now you can access your Laravel application on the route you created. You should get a JSON list with images matching the query in the URL.

http://demo.local/adobe-stock-search?query=mountains

Sample Adobe Stock Api Response

The Controller Explained

We start with the search method where we create the AdobeStock client with the configuration credentials. The client object does the search request.

The second block instantiates the SearchParameters. This object, from the Adobe Stock PHP library, sets the query parameters. In the example we set only the search query but you can find all the options at https://github.com/adobe/stock-api-libphp#searchparameters

In the next block we create a search request. We are using the result_columns method to select the fields we need to be returned. Finally we link the search parameters. You can find all the options at https://github.com/adobe/stock-api-libphp#result-columns

The block before the return is the actual call via the client. This call creates an object with all the items in the response. The response also has some additional info like the number of items.

The last row in the search method is where we call a convert function on all the results to format them. This is the form used by the WordPress plugin StocPack. You can change this to anything you need.

Private methods

The result_columns method returns all the columns we want to be returned for the search request.

The convert method creates an array with a specific format from the adobe stock response. This can be configured to your preference.

All the remaining methods are support methods for the conversion.

Conclusion

This wraps up the Adobe Stock PHP library usage instructions for Laravel. This is how you can search the Adobe Stock website using their API. You can find the full demo on Github

Important

For the purpose of the demo, the code has been simplified. The actual code uses data interfaces as multiple providers are part of the API and it’s behind an actual API route accessible via a token. To move this to an API route I recommend this Laravel article: https://laravel.com/docs/5.8/api-authentication

If you are looking for the WordPress plugin with the image search you can find that on https://stockpack.co or on https://wordpress.org/plugins/stockpack/

Leave a Comment