Jump to content

Use php gd to apply texture image on top of another ignoring transparent pixels


Recommended Posts

I’m trying to work out how I can use php gd to take an image, in this case it’s a rectangle that has text in the middle and the rest is transparent, so it’s a png with just text in the centre.

 

I have another image which is a texture. Its also a rectangle of the same size. I have managed to merge the two together so the texture on top of the text image but it covers the entire image rather than just the text in the middle.

 

what I need it to do it apply the texture to the text in the image and ignore the surrounding transparent pixels.

 

 

On 10/22/2020 at 10:21 AM, Barand said:

image.png.23e84fed85a5dfdd0b13d90501bb3826.png

I'm not sure this is helpful? I'm looking for code to make this happen, your images are correct, but the text in the middle of my image won't necessarily be just a solid colour, we need a way to apply the texture ignoring transparent pixels on the text image so the text ends up with the texture like your third image.

Can you help out?

Yes, it finds the black ones.

To start with, all pixels in im3 are transparent. It scans im2 and when it finds a black pixel it sets the corresponding pixel in im3 to the colour of that pixel in im1.

1 hour ago, Barand said:

Yes, it finds the black ones.

To start with, all pixels in im3 are transparent. It scans im2 and when it finds a black pixel it sets the corresponding pixel in im3 to the colour of that pixel in im1.

Brilliant, i'll give it a try, thank you

On 10/27/2020 at 1:01 PM, glenelkins1984 said:

Brilliant, i'll give it a try, thank you

Hi man

 

This doesn't work, $c for example is never 0 and even if i take that out nothing is ever applied to the image. I have attached 2 images i am using, shape-5.png is the one with the transparent pixels, and the other one is the texture i want applying to the non transparent pixels. Can you adivse? (the shape image is white with transparent text)

shape-5.png

a6.png

Edited by glenelkins1984
On 10/27/2020 at 11:03 AM, Barand said:

Yes, it finds the black ones.

To start with, all pixels in im3 are transparent. It scans im2 and when it finds a black pixel it sets the corresponding pixel in im3 to the colour of that pixel in im1.

See my post above please - doesn't work

1 hour ago, glenelkins1984 said:

doesn't work

Then you ain't doing it right.

My background pattern...

stripes.jpg.d73456f4f66ea2138fb0ec2c266cb495.jpg

My code...

<?php
// texture
$im1 = imagecreatefromjpeg('images/stripes.jpg');
list ($w, $h) = getimagesize('images/stripes.jpg');

// mask
$im2 = imagecreatetruecolor($w, $h);
$bg2 = imagecolorallocatealpha($im2, 255, 255, 255, 127);
$txcolor = imagecolorallocate($im2, 0,0,0);
imagefill($im2, 0, 0, $bg2);
imagettftext($im2, 96, 0, 20, $h-20, $txcolor, 'c:/windows/fonts/arlrdbd.ttf', 'TEXT');

// target
$im3 = imagecreatetruecolor($w, $h);
$bg3 = imagecolorallocatealpha($im3, 80, 80, 255, 127);
imagefill($im3, 0, 0, $bg3);
imagesavealpha($im3, 1);

// set pixels
for ($y = 0; $y<$h; $y++) {
    for ($x = 0; $x<$w; $x++) {
        $c = imagecolorat($im2, $x, $y);
        if ($c == 0) imagesetpixel($im3, $x, $y, imagecolorat($im1, $x, $y) );
    }
}

header("Content-Type: image/png");
imagepng($im3);
imagedestroy($im1);
imagedestroy($im2);
imagedestroy($im3);
?>

My output...

image.png.85087547dba7b82619e66a42f6447119.png

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.