PHP code example of salopot / image-generator

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

    

salopot / image-generator example snippets


$generator = \Faker\Factory::create();
$imageProvider = new \Salopot\ImageGenerator\ImageProvider($generator);
// Configure some or all image sources
$imageProvider->addImageSource(new \Salopot\ImageGenerator\ImageSources\Local\SolidColorSource($imageProvider));
$imageProvider->addImageSource(new \Salopot\ImageGenerator\ImageSources\Local\GallerySource($imageProvider, '/dir/with/images'));
$imageProvider->addImageSource(new \Salopot\ImageGenerator\ImageSources\Local\SolidColorSource($imageProvider));
$imageProvider->addImageSource(new \Salopot\ImageGenerator\ImageSources\Remote\LoremPixelSource($imageProvider));
$imageProvider->addImageSource(new \Salopot\ImageGenerator\ImageSources\Remote\PicsumPhotosSource($imageProvider));
$imageProvider->addImageSource(new \Salopot\ImageGenerator\ImageSources\Remote\UnsplashSource($imageProvider));
$imageProvider->addImageSource(new \Salopot\ImageGenerator\ImageSources\Remote\PlaceKittenSource($imageProvider));
$imageProvider->addImageSource(new \Salopot\ImageGenerator\ImageSources\Remote\PlaceImgSource($imageProvider));
$generator->addProvider($imageProvider);

$url = $generator->imageGenerator(640, 480)->getDataUrl();

$item1MainImage = $generator->imageGenerator(640, 480, 'item-1')->getDataUrl();
$item1ThumbnailImage = $generator->imageGenerator(50, 50, 'item-1')->getDataUrl();

$item2MainImage = $generator->imageGenerator(640, 480, 'item-2')->getDataUrl();
$item2ThumbnailImage = $generator->imageGenerator(50, 50, 'item-2')->getDataUrl();

$oneSourceUrl = $generator->imageGenerator(640, 480, null, 'SolidColor')
    ->grayscale()->getDataUrl();
$anotherSourceUrl = $generator->imageGenerator(640, 480, null, 'Unsplash')
    ->negative()->getDataUrl();

$filePath = $generator->imageGenerator(640, 480)
    ->setExtension('png')
    ->grayscale()
    ->opacity(95)
    ->contrast(30)
    ->gamma(1.6)
    ->brightness(15)
    ->blur(80)
    ->negative()
    ->insertImage('/path/to/logo.png','left', 'bottom')
    ->text('Hello world', 35, 'left', 'top', 30, -90)
    ->insertImage('/path/to/logo.png', 'right', 'top')
->getFilePath('/path/to/dir');

$dataUrl = $generator->imageGenerator(640, 480)->getDataUrl();
$filePath = $generator->imageGenerator(640, 480)->getFilePath('/path/to/dir');
$content = $generator->imageGenerator(640, 480)->getContent();

use Faker\Factory as FakerFactory;
use Faker\Generator as FakerGenerator;
use Salopot\ImageGenerator\ImageSources\Local;
use Salopot\ImageGenerator\ImageSources\Remote;

***

$this->app->singleton(FakerGenerator::class, function ($app) {
    $generator =  FakerFactory::create($app['config']->get('app.faker_locale', 'en_US'));
    
    // Additional faker providers
    $imageProvider = new \Salopot\ImageGenerator\ImageProvider($generator);
    $imageProvider->addImageSource(new Local\SolidColorSource($imageProvider));
    $imageProvider->addImageSource(new Local\GallerySource($imageProvider, '/dir/with/images'));
    $imageProvider->addImageSource(new Local\SolidColorSource($imageProvider));
    $imageProvider->addImageSource(new Remote\LoremPixelSource($imageProvider));
    $imageProvider->addImageSource(new Remote\PicsumPhotosSource($imageProvider));
    $imageProvider->addImageSource(new Remote\UnsplashSource($imageProvider));
    $imageProvider->addImageSource(new Remote\PlaceKittenSource($imageProvider));
    $imageProvider->addImageSource(new Remote\PlaceImgSource($imageProvider));
    $generator->addProvider($imageProvider);

   return $generator;
});