2012-03-24 93 views
1

我在这里新的Stackoverflow。PHP:如何使图像中的绿色区域(背景)透明?

我非常想弄清楚是否可以使用PHP脚本使图像的特定颜色透明。如果是这样,如何去做。

比方说,这是将背景颜色(绿色)必须完全透明的图像:French Hotdog w/ green background

enter image description here

我有绝对不知道这样一个脚本会是什么样子或甚至从何处开始。 我想你一定要设置不需要的RGB颜色,然后“扫描”图像中的每个像素以确定哪些像素必须是透明的。但除此之外,我很无能。

真的很希望这里有人能帮我解决这个问题。

+0

嗨@Ace,这个网站是关于具体的编程问题。你写你没有线索开始。然后先做功课,并找到至少一个线索,例如颜色(提示)。然后开始编写代码,然后突出显示您碰到路障的位置。 – hakre 2012-03-24 14:18:18

回答

0

imagecolortransparent()将帮助您:

... 
$yourColor = imagecolorallocate($im, 0, 0, 0); 
imagecolortransparent($im, $yourColor); 
... 
+0

非常感谢Narek,这使得完美的感觉。不过,恐怕我需要一些帮助才能获得扫描$ yourColor的图像像素。 – Ace 2012-03-24 14:23:36

3

首先是所有的图像应该是PNG原因JPEG不支持透明度,那么代码是这样的:

<?php 
    $image = 'test.png'; 
    $im = imagecreatefrompng($image); 
    //if you exactly know the RGB color indexes 
    //$rgb = imagecolorexact($im, 0, 0, 0); 
    //or keep the rgb color by position so at top 0 left 0 
    $rgb = imagecolorat($im, 0, 0); 
    imagecolortransparent($im, $rgb); 
    header("Content-type: image/png"); 
    //display the image directly 
    imagepng($im); 
    // or save it 
    // imagepng($im, 'test-to-transparent.png'); 
    imagedestroy($im); 
?> 
+0

辉煌aSeptik! :)谢谢你在脚本中的评论 - 真的很有帮助。现在我只想着是否有办法让图像中的每个绿色像素都透明,无论色彩的色调,色调,饱和度或亮度如何。这似乎是完全“移除”背景颜色的唯一方法。有关如何做到这一点的任何想法? – Ace 2012-03-24 21:32:28