Nastary
Nastary

Reputation: 345

How replace base color of transparent png image with texture?

is there any idea about how i can replace base color of transparent png image with texture?

In this url: Php - replace base color of transparent png image

some code to replace color with another color...

code :

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">

my goal is replace color of text witch create by php or imagemagick in png file with texture...

Upvotes: 0

Views: 685

Answers (1)

Nastary
Nastary

Reputation: 345

I find a way to do this:

My way : for example you have this texture :

enter image description here

and your text is: "Sample Text", you can create this text with imagemagick or in my way i need to create Persian text, so i change my text to PNG file with black background and withe text.

like :

enter image description here

enter image description here

ok, then you need to composite this image file with some imagemagick code like:

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

and your result will be like:

enter image description here

enter image description here

i hope this useful. Best regard.

Upvotes: 1

Related Questions