joukhar / laravel-pixabay
Laravel package for accessing the Pixabay API effortlessly.
Fund package maintenance!
joukhar
Requires
- php: ^8.0
- illuminate/contracts: ^9.0||^10.0||^11.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^9.0.0||^8.22.0
- pestphp/pest: ^2.34
- pestphp/pest-plugin-arch: ^2.7
- pestphp/pest-plugin-laravel: ^2.3
README
Overview
LaravelPixabay is a Laravel package that simplifies the process of interacting with the Pixabay API. It provides a way to search and retrieve images and videos from the Pixabay platform with minimal effort. This package includes various customizable options like image/video type, categories, order, page results, and more.
Requirements
- PHP 8.0 or higher
- Laravel 9.x or higher
- Pixabay API key (you can get this from Pixabay's Developer page)
Support us
If you find my packages useful, consider buying me a coffee to support further development.
Installation
You can install the package via composer:
composer require joukhar/laravel-pixabay
You can publish the config file or add pixabay service to (config/services.php) file:
php artisan vendor:publish --tag="laravel-pixabay-config"
Configuration file (config/pixabay.php):
return [ 'key' => env('PIXABAY_API_KEY'), ];
Then add your Pixabay API key to the .env file:
PIXABAY_KEY=your_pixabay_api_key
Usage
get images
$laravelPixabay = new Joukhar\LaravelPixabay(); $laravelPixabay->getImages();
get specific image
$laravelPixabay = new Joukhar\LaravelPixabay(); $laravelPixabay->getImages(id:"< IMAGE ID HERE >");
get images with specific type ( Default : PixabayImageType::ALL
)
use Joukhar\LaravelPixabay\Enums\PixabayImageType; $laravelPixabay = new Joukhar\LaravelPixabay(); $laravelPixabay->getImages(type: PixabayImageType::ILLUSTRATION );
get videos
$laravelPixabay = new Joukhar\LaravelPixabay(); $laravelPixabay->getVideos();
get videos with specific type ( Default : PixabayVideoType::ALL
)
use Joukhar\LaravelPixabay\Enums\PixabayVideoType; $laravelPixabay = new Joukhar\LaravelPixabay(); $laravelPixabay->getVideos(type: PixabayVideoType::ANIMATION);
get specific video
$laravelPixabay = new Joukhar\LaravelPixabay(); $laravelPixabay->getVideos(id:"< VIDEO ID HERE >");
Customizing API Requests
You can customize your queries using several configuration methods provided by the LaravelPixabay
class.
Set Current Page
You can specify which page of results to fetch using setCurrentPage()
:
$pixabay->setCurrentPage(2);
Set Maximum Results Per Page
The number of results per page can be adjusted using setMaxResults()
:
$pixabay->setMaxResults(50);
Enable Safe Search
To filter out adult content, use setSafeSearch()
:
$pixabay->setSafeSearch(true);
Only Editor's Choice
You can request only editor's choice images or videos using setOnlyEditorChoice()
:
$pixabay->setOnlyEditorChoice(true);
Set Category
To fetch resources of a specific category, use setCategory()
:
use Joukhar\LaravelPixabay\Enums\PixabayCategory; $pixabay->setCategory(PixabayCategory::NATURE);
Set Order
To order results by popularity, latest, or custom order, use setOrder()
:
$pixabay->setOrder('latest');
Set Minimum Dimensions
You can set the minimum width and height for the images/videos to be returned using setDimentions()
:
$pixabay->setDimentions(800, 600);
Example: Fetching Nature Photos
use Joukhar\LaravelPixabay\LaravelPixabay; use Joukhar\LaravelPixabay\Enums\PixabayCategory; use Joukhar\LaravelPixabay\Enums\PixabayImageType; $pixabay = new LaravelPixabay(); $photos = $pixabay ->setCategory(PixabayCategory::NATURE) ->setMaxResults(10) ->setSafeSearch(true) ->getImages(null, PixabayImageType::PHOTO);
Exception Handling
If there is an issue with the API request (e.g., invalid API key, request failure), the package throws an Exception
with a detailed error message.
try { $images = $pixabay->getImages(); } catch (Exception $e) { echo 'Error: ' . $e->getMessage(); }
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.