PHP Image to Color-able image using GDLib

275 Views Asked by At

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.

2

There are 2 best solutions below

1
Mark Setchell On BEST ANSWER

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:

  • either transparent, or
  • not black

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

2
Second2None On

What you want is pretty hard, I dont know if this will work on all images but this will help get you on your way:

<?php

$image_name = 'apple.png';

$img = imagecreatefrompng ( $image_name );
$size = getimagesize( $image_name );

if( $img ){ 
    $new_image = colourable( $img , $size );
}

function colourable( $img , $size ) {

    $new_img = imagecreate( $size[0] , $size[1] );
    $white = imagecolorallocate( $new_img , 255 , 255 , 255 );
    imagefill( $new_img , 0 , 0 , $white );

    $black = imagecolorallocate( $new_img , 0 , 0 , 0 );

    for( $x = 0; $x <= ( $size[0] - 1 ); $x++ ){
        for( $y = 0; $y <= ( $size[1] - 1 ); $y++ ){
            $pixel = imagecolorsforindex( $img, imagecolorat ( $img , $x , $y ) );
            if( ( $pixel['red'] >= 0 &&  $pixel['red'] < 50 ) && ( $pixel['green'] >= 0 && $pixel['green'] < 50 ) && ( $pixel['blue'] >= 0 && $pixel['blue'] < 50 ) ){
                imagesetpixel( $new_img , $x , $y , $black );
            }               
        }
    }
    return $new_img;
}

header ( 'Content-Type: image/png' );
imagepng( $new_image );

?>

This is the result from the supplied image: enter image description here

It also worked with a few other images, some good some not so good but play around with it and see what you can do. It's pretty self explanatory but it basically grabs the colour of each pixel within the image and checks if it falls within the RGB code of black with some tolerance. (black being 0, 0, 0). It could be fine tuned.

It checks if: RED & GREEN & BLUE all fall between 0 and 50 on the rgb scale.

if( ( $pixel['red'] >= 0 &&  $pixel['red'] < 50 ) && ( $pixel['green'] >= 0 && $pixel['green'] < 50 ) && ( $pixel['blue'] >= 0 && $pixel['blue'] < 50 ) ){