PHP code example of bugo / pexels-laravel

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

    

bugo / pexels-laravel example snippets


return [
    // Your Pexels API key
    'api_key' => env('PEXELS_API_KEY'),

    // Rate limits (adjust based on your Pexels plan)
    'rate_limits' => [
        'hourly' => env('PEXELS_HOURLY_LIMIT', 200),
        'monthly' => env('PEXELS_MONTHLY_LIMIT', 20000),
    ],

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

    // Fallback images when API limits are exceeded
    'fallback_images' => [
        'nature' => [...],
        'technology' => [...],
        'food' => [...],
    ],
];

use Bugo\PexelsLaravel\Services\PexelsService;

$pexelsService = app(PexelsService::class);

// Get a random image by search query
$image = $pexelsService->getRandomImage('nature');

[
    'url' => 'https://images.pexels.com/photos/...',
    'photographer' => 'John Doe',
    'photographer_url' => 'https://www.pexels.com/@johndoe',
    'pexels_url' => 'https://www.pexels.com/photo/...',
    'alt' => 'Image description',
    'query' => 'nature',
    'from_api' => true, // false if fallback image
]

namespace App\Http\Controllers;

use Bugo\PexelsLaravel\Services\PexelsService;

class ImageController extends Controller
{
    public function random(PexelsService $pexelsService)
    {
        $image = $pexelsService->getRandomImage('technology');

        return view('image', ['image' => $image]);
    }
}

use Bugo\PexelsLaravel\Services\PexelsService;

class ArticleSeeder extends Seeder
{
    public function run(): void
    {
        $pexelsService = app(PexelsService::class);

        $queries = ['nature', 'technology', 'business', 'travel'];

        for ($i = 0; $i < 50; $i++) {
            $query = $queries[array_rand($queries)];
            $image = $pexelsService->getRandomImage($query);

            Article::create([
                'title' => fake()->sentence,
                'content' => fake()->paragraphs(3, true),
                'image_url' => $image['url'],
                'image_alt' => $image['alt'],
                'image_photographer' => $image['photographer'],
                'image_photographer_url' => $image['photographer_url'],
            ]);
        }
    }
}
bash
php artisan vendor:publish --tag=pexels-config
bash
php artisan pexels:usage