PHP code example of emsifa / random-image

1. Go to this page and download the library: Download emsifa/random-image 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/ */

    

emsifa / random-image example snippets


use Emsifa\RandomImage\RandomImage;

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

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"

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"


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

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

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

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"

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"



namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use Emsifa\RandomImage\RandomImage;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Post>
 */
class PostFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition()
    {
        return [
            'title' => $this->faker->words(5, true),
            'body' => $this->faker->paragraphs(5, true),
            'image' => RandomImage::make(600, 400)->store('posts', 'public'),
        ];
    }
}

public function definition()
{
    $image = RandomImage::make(600, 400)->store('posts', 'public');
    $thumbnail = $image->copy()->fit(300, 200);

    // Resize to 300x200px and make it greyscale
    // $thumbnail = $image->copy()->fit(300, 200)->greyscale();

    // Use copyAs if you want to specify filename 
    // $thumbnail = $image->copyAs('posts', 'my-thumb.jpg')->fit(300, 200);

    return [
        'title' => $this->faker->words(5, true),
        'body' => $this->faker->paragraphs(5, true),
        'image' => $image,
        'thumbnail' => $thumbnail,
    ];
}

public function definition()
{
    return [
        'title' => $this->faker->words(5, true),
        'body' => $this->faker->paragraphs(5, true),
        'image' => RandomImage::make(600, 400)->store('posts', 'public'),
        'thumbnail' => RandomImage::previous()->copy()->fit(300, 200),
    ];
}
bash
php artisan vendor:publish --tag="random-image-config"