gabrielesbaiz/unsplash-toolkit

A lightweight helper package to handle Unsplash photos

1.1.1 2025-03-04 05:55 UTC

This package is auto-updated.

Last update: 2025-03-04 05:57:16 UTC


README

Latest Version on Packagist Total Downloads

A lightweight helper package to handle Unsplash photos

Original code from marksitko/laravel-unsplash

Features

  • ✅ Fluent api to use the Unsplash within Laravel applications
  • ✅ Works seamlessly with Laravel facades

Installation

You can install the package via composer:

composer require gabrielesbaiz/unsplash-toolkit

You can publish and run the migrations with:

php artisan vendor:publish --tag="unsplash-toolkit-migrations"
php artisan migrate

You can publish the config file with:

php artisan vendor:publish --tag="unsplash-toolkit-config"

This is the contents of the published config file:

return [
    'access_key' => env('UNSPLASH_ACCESS_KEY'),
    'store_in_database' => env('UNSPLASH_STORE_IN_DATABASE', false),
    'disk' => env('UNSPLASH_STORAGE_DISK', 'local'),
    'app_name' => env('UNSPLASH_APP_NAME', 'your_app_name'),
];

Usage

Basic Usage

Take a look at the full Unsplash API documentation https://unsplash.com/documentation

Random Photos

// Returns the http response body.
$twoRandomPhotosOfSomePeoples = UnsplashToolkit::randomPhoto()
    ->orientation('portrait')
    ->term('people')
    ->count(2)
    ->toJson();

// Store the image in on your provided disc
$theNameFromTheStoredPhoto = UnsplashToolkit::randomPhoto()
    ->orientation('landscape')
    ->term('music')
    ->randomPhoto()
    ->store();
];

Photos

$photos = UnsplashToolkit::photos()->toJson();
$photo = UnsplashToolkit::photo($id)->toJson();
$photosStatistics = UnsplashToolkit::photosStatistics($id)->toJson();
$trackPhotoDownload = UnsplashToolkit::trackPhotoDownload($id)->toJson();

Users

$user = UnsplashToolkit::user($username)->toJson();
$userPortfolio = UnsplashToolkit::userPortfolio($username)->toJson();
$userPhotos = UnsplashToolkit::userPhotos($username)->toJson();
$userLikes = UnsplashToolkit::userLikes($username)->toJson();
$userCollections = UnsplashToolkit::userCollections($username)->toJson();
$userStatistics = UnsplashToolkit::userStatistics($username)->toJson();

Search

$search = UnsplashToolkit::search()
    ->term('buildings')
    ->color('black_and_white')
    ->orientation('squarish')
    ->toJson();

$searchCollections = UnsplashToolkit::searchCollections()
    ->query('events')
    ->page($pageNumber)
    ->toJson();

$searchUsers = UnsplashToolkit::searchUsers()
    ->query('search_term')
    ->toJson();

Collections

$collectionsList = UnsplashToolkit::collectionsList()
    ->page($pageNumber)
    ->perPage($itemsPerPage)
    ->toJson();

$featuredCollection = UnsplashToolkit::featuredCollection()
    ->page($pageNumber)
    ->perPage($itemsPerPage)
    ->toJson();

$showCollection = UnsplashToolkit::showCollection()
    ->id($collectionId)
    ->toJson();

$showCollectionPhotos = UnsplashToolkit::showCollectionPhotos()
    ->id($collectionId)
    ->toJson();

$showCollectionRelatedCollections = UnsplashToolkit::showCollectionRelatedCollections()
    ->id($collectionId)
    ->toJson();

Stats

$totalStats = UnsplashToolkit::totalStats()->toJson();
$monthlyStats = UnsplashToolkit::monthlyStats()->toJson();

Usage with Database

If you wanna persist automaticly some informations (i.e. Copyrights) about you stored images you have to run the published migrations.

Set the .env variable

UNSPLASH_STORE_IN_DATABASE=true

When you execute store(), the image is stored in your provided disc and informations like

  • the unsplash photo id
  • the stored image name
  • author name
  • author link

Note that these informations are all required to use a Unsplash photo on your website.

Trait

You can also use the HasUnsplashables Trait on any model.

Example with HasUnsplashables Trait on the User Model

use Illuminate\Foundation\Auth\User as Authenticatable;

use Gabrielesbaiz\UnsplashToolkit\Traits\HasUnsplashables;

class User extends Authenticatable
{
    use HasUnsplashables;

    // ...
}

Now you are able to use it like:

// store the unsplash asset in a morphToMany relation
$unsplashAsset = UnsplashToolkit::randomPhoto()->store();

User::unsplash()->save($unsplashAsset);

// retrive all related unsplash assets
User::find($userId)->unsplash();

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.