使用GDLib将PHP图像转换为可着色图像

时间:2019-02-09 04:27:04

标签: php image-processing gdlib

我目前正在使用 PHP 脚本来构建着色书。用户将上传图像(彩色),然后将这些图像转换为无色图像(可彩色显示),并将其排列为PDF文件。一切都已管理,但我无法将图像转换为可彩色图像。图片显示有白色背景和黑色笔画。

当前我正在使用以下代码:

$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);
}

例如:
Color ImageColor-able Image

感谢您的帮助。

我尝试使用PHP GDLib Edge Detection过滤器,但无法获得所需的结果。

2 个答案:

答案 0 :(得分:0)

您想要的东西很难,我不知道这是否适用于所有图像,但这将帮助您上路:

<?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 );

?>

这是提供的图像的结果: enter image description here

它还可以与其他一些图像一起使用,有些很好,有些不太好,但是尝试一下,看看可以做什么。这是很容易解释的,但它基本上可以捕获图像中每个像素的颜色,并以一定的公差检查它是否落在黑色的RGB代码内。 (黑色为0、0、0)。可以进行微调。

它检查是否: 红& 绿色 & 蓝色 在rgb等级上,所有数值都在0到50之间。

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

答案 1 :(得分:0)

如果图像已经被黑色描边了,不知道为什么要过滤或进行边缘检测?当然,您只想制作以下所有内容:

  • 透明或
  • 不是黑色

全部变成白色。

#!/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

相关问题