【问题标题】:How replace base color of transparent png image with texture?如何用纹理替换透明png图像的基色?
【发布时间】:2014-11-24 23:46:32
【问题描述】:

有什么想法可以用纹理替换透明 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 文件中创建的文本的颜色...

【问题讨论】:

  • 请给出“之前”和“之后”的图形示例。这段代码做什么(或不做什么)?
  • 此代码结果在帖子顶部的 url 中可能很好,但我的目标是纹理而不是颜色...

标签: php imagemagick png


【解决方案1】:

我找到了一种方法:

我的方式: 例如你有这个纹理:

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

喜欢:

好的,那么你需要用一些 imagemagick 代码来合成这个图像文件:

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

你的结果会是这样的:

我希望这有用。 最好的尊重。

【讨论】:

    猜你喜欢
    • 2013-07-18
    • 2020-04-14
    • 2015-02-03
    • 1970-01-01
    • 2014-02-01
    • 2010-12-28
    • 2016-05-08
    • 1970-01-01
    • 2011-10-19
    相关资源
    最近更新 更多