image processing - Changing Black to Transparent in PHP -
this question has answer here:
- how to remove colors image php or ruby? 3 answers
i'm trying replace black background gif/jpeg files transparent background, , because it's tiring manually 1 one via photoshop, thought of trying use php. code i'm using, doesn't work.
why that?
$image = imagecreatefromgif( 'items/item_spear06.gif' ); imagealphablending($image, true); $transparentcolour = imagecolorallocate($image, 0,0,0); imagecolortransparent($image, $transparentcolour);
and image got same image last time in png format.
update
$image = imagecreatefromgif( 'items/sword/iv_sword_refined19.gif' ); //get pixel data $rgb = imagecolorat($image, 10, 10); $r = ($rgb >> 16) & 0xff; $g = ($rgb >> 8) & 0xff; $b = $rgb & 0xff; $index = imagecolorexact($image, $r, $g, $b); if ($index === -1) { $index = imagecolorallocate($image, $r, $g, $b); } imagecolortransparent($image, $index); var_dump($r, $g, $b); // save image imagepng($image, 'imagecolortransparent.png'); imagedestroy($image);
now var_dump returns: int(0) int(0) int(0)
when should int(4) int(2) int(4)
i'm little bit out on whim here guess color have chosen here (black) exists in palette. so, try find color in palette , set 1 transparent, if not exist, can use imagecolorallocate
$image = imagecreatefromgif( 'items/item_spear06.gif' ); $index = imagecolorexact($image, 0, 0, 0); if ($index === -1) { $index = imagecolorallocate($image, 0, 0, 0); } imagecolortransparent($image, $index);
to top-left pixels color
$index = imagecolorat($image, 0, 0); $rgb = imagecolorsforindex($image, $index);
Comments
Post a Comment