2010-12-18 111 views
1

我讀了幾件關於這個,但我不知道如何把它放在我的代碼。像素每像素圖像,透明變成黑色

這就是:

function go($image) { 
    // Open the image 
    $getimage=imagecreatefrompng($image); 
    //Get the width/height 
    $w = imagesx($getimage); 
    $h = imagesy($getimage); 
    //init Count variable, when it reach the width, skip a line 
    $count = 0; 
    // For each line 
    for($y=0;$y<$h;$y++) { 
     // For each column 
     for($x=0;$x<$w;$x++) { 
     $rgba  = imagecolorat($getimage, $x, $y); 
     $r = ($rgba >> 16) & 0xFF; 
     $g = ($rgba >> 8) & 0xFF; 
     $b = $rgba & 0xFF; 
     $a  = ($rgba & 0x7F000000) >> 24; 
     echo '<div class="pix" style="background-color: rgba('.$r.', '.$g.', '.$b.', '.$a.');"></div>'; 
     $count++; 
     if($count==$w) {echo '<br>'; $count = 0; } 

     } 
    } 
    echo $pixel_gen; 

} 

如果奧尤想看看是什麼樣子,集團公司在這裏:http://narks.xtreemhost.com/

而且在任何雙擊圖標,彈出窗口將出現。 (注意:任何圖標上的dbl-clinking都會顯示相同的圖像(我沒有修復這個問題)

任何想法如何讓黑色像素像真正的alpha像素一樣?

感謝您的幫助!

EDITED (新代碼,我只放了第一線,因爲我想節省空間)

function go($image) { 
    // Open the image 
    $getimage=imagecreatefrompng($image); 
    imagealphablending($getimage, false); 
    imagesavealpha($getimage, true); 

    //Get the width/height 
    $w = imagesx($getimage); 
    $h = imagesy($getimage); 
[...] 

,看看是什麼樣子,現在,請訪問上述網站,並上雙擊圖標。

編輯2 我只是試圖(用於測試)其中:

$getimage=imagecreatefrompng('iconDB/lib/chat_fav_48.png'); 
imagealphablending($getimage, false); 
imagesavealpha($getimage, true); 

header("Content-type: image/png"); 
imagepng($getimage); 
imagedestroy($getimage); 

,然後用

$getimage=imagecreatefrompng('iconDB/lib/chat_fav_48.png'); 

header("Content-type: image/png"); 
imagepng($getimage); 
imagedestroy($getimage); 

首先是行和所述第二像素作黑。所以它是當我得到每個像素的RGB顏色,當我顯示它。有人在那裏看到一個錯誤?

回答

2

這是正確的工作代碼,爲了完成這個問題。

function go($image) { 
// Open the image 
$getimage=imagecreatefrompng($image); 
imagealphablending($getimage, true); 
imagesavealpha($getimage, true); 

    //Get the width/height 
    $w = imagesx($getimage); 
    $h = imagesy($getimage); 
    //init Count variable, when it reach the width, skip a line 
    $count = 0; 
    // For each line 
    for($y=0;$y<$h;$y++) { 
    // For each column 
     for($x=0;$x<$w;$x++) { 
    // Get the image color for this pixel 
    $rgba  = imagecolorat($getimage, $x, $y); 
    $r = ($rgba >> 16) & 0xFF; 
    $g = ($rgba >> 8) & 0xFF; 
    $b = $rgba & 0xFF; 
    $a = ($rgba & 0x7F000000) >> 24; 
    //Calculating the correct Alpha value for rgba display in css 
    $a = (127-$a)/127; 
    echo '<div class="pix" style="background-color: rgba('.$r.', '.$g.', '.$b.', '.$a.');"></div>'; 
    $count++; 
    if($count==$w) {echo '<br>'; $count = 0; } 

     } 
    } 
    echo $pixel_gen; 

} 

我希望這將是有人

1

按照imagecreatefrompng網頁上留言,你需要調用imagealphablendingimagesavealpha

imagealphablending($getimage, false); 
imagesavealpha($getimage, true); 

也有關於Alpha透明度和PNG格式與網頁的其他意見。

+0

它不工作有用。查看我的代碼編輯的原始文章。你可以在這裏看到(http://narks.xtreemhost.com/)並雙擊一個圖標。 – 2010-12-19 04:45:55