2014-09-30 67 views
-1

有没有关于如何用纹理替换透明PNG图像的基本颜色的任何想法?如何用纹理替换透明PNG图像的底色?

在此网址:Php - replace base color of transparent png image

一些代码,使另一种颜色更换颜色...

代码:

function colorizeBasedOnAplhaChannnel($file, $targetR, $targetG, $targetB, $targetName) { 

$im_src = imagecreatefrompng($file); 

$width = imagesx($im_src); 
$height = imagesy($im_src); 

$im_dst = imagecreatefrompng($file); 

// Note this: 
// Let's reduce the number of colors in the image to ONE 
imagefilledrectangle($im_dst, 0, 0, $width, $height, 0xFFFFFF); 

for($x=0; $x<$width; $x++) { 
    for($y=0; $y<$height; $y++) { 

     $alpha = (imagecolorat($im_src, $x, $y) >> 24 & 0xFF); 

     $col = imagecolorallocatealpha($im_dst, 
      $targetR - (int) (1.0/255.0 * $alpha * (double) $targetR), 
      $targetG - (int) (1.0/255.0 * $alpha * (double) $targetG), 
      $targetB - (int) (1.0/255.0 * $alpha * (double) $targetB), 
      $alpha 
      ); 

     if (false === $col) { 
      die('sorry, out of colors...'); 
     } 

     imagesetpixel($im_dst, $x, $y, $col); 

    } 

} 

imagepng($im_dst, $targetName); 
imagedestroy($im_dst); 

} 

unlink(dirname (__FILE__) . '/newleaf.png'); 
unlink(dirname (__FILE__) . '/newleaf1.png'); 
unlink(dirname (__FILE__) . '/newleaf2.png'); 

$img = dirname (__FILE__) . '/leaf.png'; 
colorizeBasedOnAplhaChannnel($img, 0, 0, 0xFF, 'newleaf1.png'); 
colorizeBasedOnAplhaChannnel($img, 0xFF, 0, 0xFF, 'newleaf2.png'); 
?> 

Original 
<img src="leaf.png"> 
<br /> 
<img src="newleaf1.png"> 
<br /> 
<img src="newleaf2.png"> 

我的目标是替换文本女巫的颜色由PHP或ImageMagick的创建在PNG文件与纹理...

+0

请给出'之前'和'之后'的图形示例。这段代码做了什么(或不做)? – usr2564301 2014-09-30 12:23:43

+0

这段代码的结果可以在网址顶部的罚款,但我的目标是纹理,不是颜色... – Nastary 2014-09-30 13:57:22

回答

0

我找到一种方法来做到这一点:

我的方式: 比如你有这样的结构:

enter image description here

,你的文字是:“示例文字”, 你可以用ImageMagick的或以我的方式创建此文我需要创建波斯文字,所以我将我的文本更改为带有黑色背景和文本的PNG文件。

,如:

enter image description here

enter image description here

OK,那么你就需要复合材料像一些ImageMagick的代码,这个映像文件:

exec(' convert ripple-overlay.png text-mask.png -alpha Off -compose CopyOpacity -composite result.png'); 

,你的结果会是这样的:

enter image description here

enter image description here

我希望这是有用的。 最好的方面。