PHPackages                             emsifa/random-image - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. emsifa/random-image

ActiveLibrary

emsifa/random-image
===================

Store random image from Unsplash to your Laravel application

v1.0.0(3y ago)51.5k1[4 PRs](https://github.com/emsifa/random-image/pulls)MITPHPPHP ^8.1CI passing

Since Nov 25Pushed 4mo ago1 watchersCompare

[ Source](https://github.com/emsifa/random-image)[ Packagist](https://packagist.org/packages/emsifa/random-image)[ Docs](https://github.com/emsifa/random-image)[ RSS](/packages/emsifa-random-image/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (9)Dependencies (14)Versions (15)Used By (0)

Random Image
============

[](#random-image)

[![Latest Version on Packagist](https://camo.githubusercontent.com/94d22875ba840d5d25570480a135a1bf3ff0a12ebcd2a27baf90b11c7861b860/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f656d736966612f72616e646f6d2d696d6167652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/emsifa/random-image)[![GitHub Tests Action Status](https://camo.githubusercontent.com/4cd90bfbb241016dac29449a3fd898f156a7de8f83c817f4bf1425ef7d4e4457/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f656d736966612f72616e646f6d2d696d6167652f72756e2d74657374732e796d6c3f6c6162656c3d7465737473)](https://github.com/emsifa/random-image/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/dbd0da91de2f6ce19f2042d941b5ba8aa53c7e468d3c736a3fb9862f72ae06b8/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f656d736966612f72616e646f6d2d696d6167652f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6c6162656c3d636f64652532307374796c65)](https://github.com/emsifa/random-image/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/455e267ac8ff211461b050537b200fcefb44e71522b1321f0aa9ac3c589a2499/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f656d736966612f72616e646f6d2d696d6167652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/emsifa/random-image)

Random Image is a Laravel helper to get random image and store it in your application. It is designed to be used in [model factory](https://laravel.com/docs/9.x/eloquent-factories) to seed dummy data.

Currently we have 3 random image provider you can choose:

1. [LoremFlickr](https://loremflickr.com) (default)
2. [Unsplash](https://unsplash.com)
3. [LoremPicsum](https://picsum.photos) (doesn't support search query)

Please see their website for the license information.

Features
--------

[](#features)

- Get random image URL with search terms support.
- Store random image to filesystem.
- Manipulate downloaded image.
- Copy downloaded image and manipulate it (for thumbnail/placeholder image generation).

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require emsifa/random-image --dev
```

> Remove `--dev` flag if you are planned to use this library in production

After that, you may want to publish the config file with:

```
php artisan vendor:publish --tag="random-image-config"
```

It will generate `config/random-image.php` file in your project directory.

Usage Example
-------------

[](#usage-example)

### Get Random Image URL

[](#get-random-image-url)

If you just want to get image url from provider, you can use `url` method like an example below:

```
use Emsifa\RandomImage\RandomImage;

RandomImage::make()->url();
```

It will return `https://loremflickr.com/600/400/` which will resulting different image if you use it in `` tag.

You can also specify size and search terms inside `make` method like this:

```
RandomImage::make(200)->url();                  // "https://loremflickr.com/200/200/"
RandomImage::make(300, 200)->url();             // "https://loremflickr.com/300/200/"
RandomImage::make(300, 200, 'cat,dog')->url();  // "https://loremflickr.com/300/200/cat,dog"
RandomImage::make(query: 'cats')->url();        // "https://loremflickr.com/600/400/cats"
```

The URL returned will be different for every provider. For example, if you use Unsplash provider, the URL generated would be like this:

```
RandomImage::make(200)->url();                  // "https://source.unsplash.com/random/200x200/"
RandomImage::make(300, 200)->url();             // "https://source.unsplash.com/random/300x200/"
RandomImage::make(300, 200, 'cat,dog')->url();  // "https://source.unsplash.com/random/300x200/?cat,dog"
RandomImage::make(query: 'cats')->url();        // "https://source.unsplash.com/random/?cats"
```

> You can change provider in `config/random-image.php` file.

### Store Image

[](#store-image)

You can use `store` or `storeAs` method to download and store image into your filesystem disk.

```
RandomImage::make()->store();           // "{random-hash-name}.jpg"
RandomImage::make()->store('images');   // "images/{random-hash-name}.jpg"
```

You can specify disk by defining `disk` parameter:

```
RandomImage::make()->store('images', 's3'); // "images/{random-hash-name}.jpg"
RandomImage::make()->store(disk: 's3');     // "{random-hash-name}.jpg"
```

Use `storeAs` if you want to specify filename:

```
RandomImage::make()->storeAs('', 'my-image.jpg');       // "my-image.jpg"
RandomImage::make()->storeAs('images', 'my-image.jpg'); // "images/my-image.jpg"
```

You can also get stored URL just by chaining it with `url()` method like example below:

```
RandomImage::make()->store()->url();            // "http://your-app.test/storage/{random-hash-name}.jpg"
RandomImage::make()->store('images')->url();    // "http://your-app.test/storage/images/{random-hash-name}.jpg"

RandomImage::make()->storeAs('', 'my-image.jpg')->url();       // "http://your-app.test/storage/my-image.jpg"
RandomImage::make()->storeAs('images', 'my-image.jpg')->url(); // "http://your-app.test/storage/images/my-image.jpg"
```

Or if you want to get the filename only, you can chain it with `name()` method.

```
RandomImage::make()->store()->name();            // "{random-hash-name}.jpg"
RandomImage::make()->store('images')->name();    // "{random-hash-name}.jpg"

RandomImage::make()->storeAs('', 'my-image.jpg')->name();        // "my-image.jpg"
RandomImage::make()->storeAs('images', 'my-image.jpg')->name();  // "my-image.jpg"
```

### Usage Example in Model Factory

[](#usage-example-in-model-factory)

This package is designed to be used in the model factory, which is why methods above return a string of image path/url so you can just store it in the database. To use it in the model factory, you can just call the method in the example above in your model factory like this:

```
