PHP code example of dotxdd / coloring-book

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

    

dotxdd / coloring-book example snippets


use Dotxdd\ColoringBook\ColoringBookGenerator;

$generator = new ColoringBookGenerator();
$result = $generator->generate('input.jpg', 'output.png');

if ($result) {
    echo "Coloring book generated successfully!";
}

use Dotxdd\ColoringBook\ColoringBookGenerator;

class GenerateColoringBook extends Command
{
    protected $signature = 'coloring:generate 
                            {--input=test.jpg : Input image filename}
                            {--output=coloring_book.png : Output coloring book filename}
                            {--colors-json=colors.json : Output colors JSON filename}
                            {--threshold=30 : Contour detection threshold (1-100)}
                            {--colors=30 : Maximum number of colors (10-50)}
                            {--blur=4 : Blur radius}
                            {--minarea=0.015 : Minimum area percent for merging (e.g. 0.015 = 1.5%)}';

    public function handle()
    {
        $inputFile = $this->option('input');
        $outputFile = $this->option('output');
        $colorsJsonFile = $this->option('colors-json');
        
        $threshold = (int) $this->option('threshold');
        $maxColors = (int) $this->option('colors');
        $blur = (int) $this->option('blur');
        $minAreaPercent = (float) $this->option('minarea');

        $generator = new ColoringBookGenerator();
        $generator->setContourThreshold($threshold)
                  ->setMaxColors($maxColors)
                  ->loadImage($inputFile);

        // Generate coloring book with advanced settings
        $image = $generator->generateChildFriendlyColoringBook($maxColors, $blur, $minAreaPercent);
        
        // Save the generated image
        $result = $image->save($outputFile);
        
        // Save color palette as JSON
        $generator->saveColorPalette($colorsJsonFile);
        
        if ($result) {
            echo "Coloring book generated successfully!";
        }
    }
}