PHP code example of jatniel / laravel-pexels

1. Go to this page and download the library: Download jatniel/laravel-pexels 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/ */

    

jatniel / laravel-pexels example snippets


use Jatniel\Pexels\Facades\Pexels;

// Search photos
$photos = Pexels::photos()->search('nature', perPage: 15, page: 1);

// Search with filters
$photos = Pexels::photos()->search(
    'ocean',
    orientation: 'landscape', // landscape, portrait, square
    size: 'large',            // large (24MP), medium (12MP), small (4MP)
    color: 'blue',            // color name or hex code (#ffffff)
    locale: 'es-ES',
);

// Get curated photos
$curated = Pexels::photos()->curated(perPage: 10);

// Get a specific photo by ID
$photo = Pexels::photos()->find(12345);

// Get a random photo (by query, or from curated photos)
$random = Pexels::photos()->random('ocean');
$random = Pexels::photos()->random();

// Get the URL of a photo directly
$url = Pexels::photos()->url(12345, 'medium');

$photos = Pexels::photos()->search('nature', perPage: 20, page: request('page', 1));

$photos->total();       // Total results on Pexels
$photos->currentPage();
$photos->lastPage();

foreach ($photos as $photo) {
    echo $photo->getUrl('medium');
}

$photo = Pexels::photos()->find(12345);

// Get photo URL in different sizes
$photo->getUrl('original');
$photo->getUrl('large2x');
$photo->getUrl('large');
$photo->getUrl('medium');
$photo->getUrl('small');

// Get available sizes
$photo->getSizes(); // ['original', 'large2x', 'large', 'medium', 'small', ...]

// Get attribution (ponse()->json($photo);

// Get your collections
$collections = Pexels::collections()->all();

// Get photos from a collection
$photos = Pexels::collections()->photos('collection-id');

$photo = Pexels::photos()->find(12345);

// Download a single size
$paths = Pexels::storage()->download($photo, 'original');
// ['original' => '/storage/pexels/12345/original.jpg']

// Download multiple sizes (sizes already stored are skipped)
$paths = Pexels::storage()->download($photo, ['original', 'medium', 'small']);

// Force re-download
$paths = Pexels::storage()->download($photo, 'original', force: true);

// Async download (queued on the configured connection and queue)
Pexels::storage()->downloadAsync($photo, ['original', 'medium']);

// Check if photo exists locally
Pexels::storage()->exists(12345, 'medium');

// Get local URL
Pexels::storage()->localUrl(12345, 'medium');

// Delete local photo
Pexels::storage()->delete(12345); // All sizes
Pexels::storage()->delete(12345, 'medium'); // Specific size

use Jatniel\Pexels\Exceptions\PexelsException;
use Jatniel\Pexels\Exceptions\PhotoNotFoundException;
use Jatniel\Pexels\Exceptions\RateLimitException;

try {
    $photo = Pexels::photos()->find(12345);
} catch (PhotoNotFoundException $e) {
    // The photo does not exist
} catch (RateLimitException $e) {
    // Local limit or Pexels API limit (HTTP 429) reached
} catch (PexelsException $e) {
    // Missing API key, connection error or any other API error
}

// config/pexels.php

return [
    // API keys. PEXELS_API_KEY_TEST is used outside production when set.
    'api_key' => env('PEXELS_API_KEY'),
    'api_key_test' => env('PEXELS_API_KEY_TEST'),

    // HTTP timeout in seconds
    'timeout' => env('PEXELS_TIMEOUT', 10),

    // Cache settings
    'cache' => [
        'enabled' => env('PEXELS_CACHE_ENABLED', true),
        'ttl' => env('PEXELS_CACHE_TTL', 3600), // 1 hour
    ],

    // Storage settings for local downloads
    'storage' => [
        'disk' => env('PEXELS_STORAGE_DISK', 'public'),
        'path' => env('PEXELS_STORAGE_PATH', 'pexels'),
    ],

    // Rate limiting (free plan: 200 req/hour)
    'rate_limit' => [
        'enabled' => env('PEXELS_RATE_LIMIT_ENABLED', true),
        'requests_per_hour' => env('PEXELS_RATE_LIMIT_REQUESTS', 200),
    ],

    // Queue settings for async downloads
    'queue' => [
        'connection' => env('PEXELS_QUEUE_CONNECTION'),
        'name' => env('PEXELS_QUEUE_NAME', 'pexels'),
    ],

    // Attribution format
    'attribution' => [
        'format' => 'Photo by :photographer on Pexels',
        'link_to_profile' => true,
    ],
];
bash
php artisan vendor:publish --tag="pexels-config"