Удалить цвет фона изображения в PHP - PullRequest
0 голосов
/ 07 мая 2019

Я хочу удалить цвет фона изображения. Но это не работает должным образом. Мой код может частично удалить цвет фона.

Вот мой код:

function transparent_background($filename) 
{

    $output_file_name = $_SERVER['DOCUMENT_ROOT'].'/test/removeBackgroundColor/'.rand(100,200).'.png';
    $_filename= $_SERVER['DOCUMENT_ROOT'].'/test/removeBackgroundColor/'.$filename;
    $_backgroundColour='0,0,0';
    $imageData =  file_get_contents($_filename);
    $_img = imagecreatefromstring($imageData);
    $_backgroundColours = explode(',', $_backgroundColour);
    $_removeColour = imagecolorallocate($_img, (int)$_backgroundColours[0], (int)$_backgroundColours[1], (int)$_backgroundColours[2]);
    imagecolortransparent($_img, $_removeColour);
    imagesavealpha($_img, true);


    $_transColor = imagecolorallocatealpha($_img, 0, 0, 0, 127);
    imagefill($_img, 0, 0, $_transColor);
    imagesavealpha($_img, true);
    imagepng($_img, $output_file_name);

    echo $output_file_name;
}

transparent_background('6.png');

Я передал это изображение в качестве ввода: http://prntscr.com/nldb94

Но возвращаем это: http://prntscr.com/nldbg4

Я хочу также удалить цвет этого раздела: http://prntscr.com/nldbr2

Не могли бы вы помочь мне выяснить решение? Заранее спасибо

1 Ответ

0 голосов
/ 07 мая 2019
$_transColor = imagecolorallocatealpha($_img, 0, 0, 0, 127);
    imagefill($_img, 0, 0, $_transColor);
    imagesavealpha($_img, true);
    imagepng($_img, $output_file_name);

Use imagecolortransparent($_img, $_removeColour); in the above code.

Поэтому, пожалуйста, попробуйте включить изображение-прозрачный код во второй код изображения.

...