phpimage-processinggdlib

PHP Image to Color-able image using GDLib


I am currently working on a PHP script to build a coloring book. User will upload the images(colored) and I've convert those images into colorless images(Color-able) and arrange them in a PDF file. Everything is managed but I am not able to convert image into the color-able image. Image show have white background and black strokes.

Currently I am using this code:

$im = imagecreatefromjpeg($_FILES['image']['tmp_name'][$i]);
/* R, G, B, so 0, 255, 0 is green */

if ($im) {
    imagefilter($im, IMG_FILTER_GRAYSCALE);
    imagefilter($im, IMG_FILTER_EDGEDETECT);
    imagefilter($im, IMG_FILTER_MEAN_REMOVAL);
    imagefilter($im, IMG_FILTER_CONTRAST, -1000);
    imagejpeg($im, "tmp_image/image-".$i.".jpg");
    $pdf_images[] = "tmp_image/image-".$i.".jpg";
    imagedestroy($im);
}

For example:
Color Image to Color-able Image

Thank you for the help.

I tried the PHP GDLib Edge Detection filter but couldn't get the required result.


Solution

  • Not sure why you are filtering or doing edge detection if your image is already stroked in black? Surely you just want to make everything that is:

    all turn into white.

    #!/usr/bin/php -f
    <?php
    
    $img  = imagecreatefrompng('apple.png');
    $w = imagesx($img);
    $h = imagesy($img);
    
    // Create a new palettised colorable image same size
    // We only need 2 colours so palettised will be fine and nice and small
    $colorable = imagecreate($w,$h);
    $white = imagecolorallocate($colorable,255,255,255);
    $black = imagecolorallocate($colorable,0,0,0);
    
    for($x=0;$x<=$w;$x++){
        for($y=0;$y<=$h;$y++){
            $px = imagecolorsforindex($img,imagecolorat($img,$x,$y));
            // Assume we will make this pixel black in new image
            $newcolour = $black;
            $R = $px['red']; $G = $px['green']; $B = $px['blue']; $A = $px['alpha'];
    
            // If this pixel is transparent, or has any significant red, green or blue component, make it white
            if(($A==127) || ($R > 80) || ($G > 80) || ($B > 80)){
                $newcolour = $white;
            }
            imagesetpixel($colorable,$x,$y,$newcolour);
        }
    }
    imagepng($colorable,'result.png');
    ?>
    

    enter image description here