PHP code example of laratusk / pictomock

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

    

laratusk / pictomock example snippets


use Laratusk\PictoMock\PictoMock;

// Fluent API
$path = PictoMock::make()
    ->width(400)
    ->height(300)
    ->format('png')
    ->save('/tmp/images');

// Static one-liner
$path = PictoMock::generate('/tmp/images', 400, 300, 'png');

// SVG — no GD needed
$path = PictoMock::make()->format('svg')->width(800)->height(600)->save('/tmp');

// With dimension text overlay
$path = PictoMock::make()->format('png')->width(300)->height(200)->withText()->save('/tmp');

use Laratusk\PictoMock\PictoMock;

$path = PictoMock::generate(
    outputDir: '/tmp/images',
    width: 400,
    height: 300,
    format: 'jpeg',
);

PictoMock::generate(
    string $outputDir,
    int $width = 300,
    int $height = 300,
    string|ImageFormat $format = 'png',
    ?BackgroundStrategyInterface $background = null,
    bool $showText = false,
    ?string $filename = null,
): string

use Laratusk\PictoMock\PictoMock;

$path = PictoMock::make()
    ->width(1200)
    ->height(628)
    ->format('webp')
    ->withText()
    ->filename('og-image')     // custom filename (without extension)
    ->save('/var/www/public/images');

$base = PictoMock::make()->width(400)->height(300);
$png  = $base->format('png')->save('/tmp');
$svg  = $base->format('svg')->save('/tmp');

use Laratusk\PictoMock\BackgroundStrategies\RandomColor;

$path = PictoMock::make()
    ->background(new RandomColor())
    ->format('svg')
    ->save('/tmp');

use Laratusk\PictoMock\BackgroundStrategies\FixedColor;

$path = PictoMock::make()
    ->background(new FixedColor('#4a90e2'))   // accepts #RRGGBB or #RGB
    ->format('png')
    ->save('/tmp');

use Laratusk\PictoMock\BackgroundStrategies\GradientColor;

$path = PictoMock::make()
    ->background(new GradientColor('#ff6b6b', '#4ecdc4'))
    ->width(800)
    ->height(400)
    ->format('png')
    ->save('/tmp');

$path = PictoMock::make()
    ->width(400)
    ->height(300)
    ->format('png')
    ->withText()          // renders "400x300" centered on the image
    ->save('/tmp');

// Or pass false to disable explicitly:
$path = PictoMock::make()->withText(false)->format('svg')->save('/tmp');

$path = PictoMock::make()
    ->format('svg')
    ->width(1200)
    ->height(630)
    ->background(new GradientColor('#667eea', '#764ba2'))
    ->withText()
    ->save('/tmp/social-images');

use Laratusk\PictoMock\Laravel\PictoMockFacade as PictoMock;
// or with the alias:
use PictoMock;

$path = PictoMock::make()->format('svg')->width(400)->height(300)->save(storage_path('app/images'));

// config/pictomock.php
return [
    'width'            => 300,
    'height'           => 300,
    'format'           => 'png',
    'show_text'        => false,
    'output_dir'       => storage_path('app/pictomock'),
    'background'       => 'random',       // 'random' | 'fixed' | 'gradient'
    'background_color' => '#4a90e2',      // used when background = 'fixed'
    'gradient_colors'  => ['#4a90e2', '#7b2ff7'],  // used when background = 'gradient'
];

// app/Providers/AppServiceProvider.php
use Laratusk\PictoMock\Faker\PictoMockFakerProvider;

public function register(): void
{
    $this->app->extend(\Faker\Generator::class, function ($faker) {
        $faker->addProvider(new PictoMockFakerProvider($faker, storage_path('app/pictomock')));
        return $faker;
    });
}

// database/factories/PostFactory.php
public function definition(): array
{
    return [
        'title'         => $this->faker->sentence(),
        'thumbnail'     => $this->faker->pictoMock(800, 450, 'webp'),
        'thumbnail_url' => $this->faker->pictoMockUrl(800, 450, 'png'),
    ];
}

use Faker\Factory;
use Laratusk\PictoMock\Faker\PictoMockFakerProvider;

$faker = Factory::create();
$faker->addProvider(new PictoMockFakerProvider($faker, '/tmp/images'));

// Returns absolute path to the generated file
$path = $faker->pictoMock(400, 300, 'png');

// Returns data URI (data:image/png;base64,...)
$dataUri = $faker->pictoMockUrl(400, 300, 'svg');

// With dimension text overlay
$path = $faker->pictoMock(800, 600, 'jpeg', true);
bash
# On Ubuntu/Debian:
sudo apt-get install php-gd

# On macOS with Homebrew:
brew install php && brew install gd
bash
php artisan vendor:publish --tag=pictomock-config