Skip to content

A skeleton repository for Spatie's Laravel Packages

License

Notifications You must be signed in to change notification settings

joukhar/laravel-pixabay

 
 

Repository files navigation

Laravel package for accessing the Pixabay API effortlessly.

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

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

Support us

If you find my packages useful, consider buying me a coffee to support further development.

Buy Me A Coffee

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.

About

A skeleton repository for Spatie's Laravel Packages

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%