2009-04-30 49 views

回答

1

这是一个肮脏的黑客我刚才做了一个项目。它将灰度图像作为透明度图映射到另一图像(黑色透明,白色不透明,将地图缩放为图像比例得到支持)。你可以创建一个合适的圆角透明图(包括抗锯齿,哇!)。

它很慢,因为它是纯粹的PHP,但我总是缓存结果。

$ image和$ transparencyMap是gd图像资源,所以你必须自己imagecreatefromxyz。

<?php 
function applyTransparencyMap($image, $transparencyMap) { 
    if (!function_exists('extractrgb')) { 
     function extractrgb($rgb) { 
      $a = ($rgb >> 24) & 0xFF; 
      $r = ($rgb >> 16) & 0xFF; 
      $g = ($rgb >> 8) & 0xFF; 
      $b = $rgb & 0xFF; 
      return array($r, $g, $b, $a); 
     } 
    } 

    $sx = imagesx($image); 
    $sy = imagesy($image); 
    $tx = imagesx($transparencyMap); 
    $ty = imagesy($transparencyMap); 
    $dx = $tx/$sx; 
    $dy = $ty/$sy; 

    $dimg = imagecreatetransparent(imagesx($image), imagesy($image)); 

    for ($y = 0; $y<imagesy($image); $y++) { 
     for ($x = 0; $x<imagesx($image); $x++) { 
      $intcolor    = imagecolorat($image, $x, $y); 
      $intalpha    = imagecolorat($transparencyMap, floor($x*$dx), floor($y*$dy-1)); 
      list($tr, $tg, $tb, $ta) = extractrgb($intalpha); 
      $alphaval    = 127-floor(($tr+$tg+$tb)/6); 
      list($r, $g, $b, $a)  = extractrgb($intcolor); 
      $targetAlpha    = max(0, min(127,$alphaval+$a)); 
      $sct      = imagecolorallocatealpha($image, $r, $g, $b, $targetAlpha); 
      imagesetpixel($dimg, $x, $y, $sct); 
     } 
    } 

    return $dimg; 
} 
?> 

另一方面,更好地使用wideimage,因为apikot建议。做同样的更多。