glenelkins1984 Posted October 22, 2020 Share Posted October 22, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/311624-use-php-gd-to-apply-texture-image-on-top-of-another-ignoring-transparent-pixels/ Share on other sites More sharing options...
Barand Posted October 22, 2020 Share Posted October 22, 2020 Quote Link to comment https://forums.phpfreaks.com/topic/311624-use-php-gd-to-apply-texture-image-on-top-of-another-ignoring-transparent-pixels/#findComment-1581996 Share on other sites More sharing options...
glenelkins1984 Posted October 27, 2020 Author Share Posted October 27, 2020 On 10/22/2020 at 10:21 AM, Barand said: 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? Quote Link to comment https://forums.phpfreaks.com/topic/311624-use-php-gd-to-apply-texture-image-on-top-of-another-ignoring-transparent-pixels/#findComment-1582078 Share on other sites More sharing options...
Barand Posted October 27, 2020 Share Posted October 27, 2020 Here's code to translate the algorithm I gave you Quote Link to comment https://forums.phpfreaks.com/topic/311624-use-php-gd-to-apply-texture-image-on-top-of-another-ignoring-transparent-pixels/#findComment-1582081 Share on other sites More sharing options...
glenelkins1984 Posted October 27, 2020 Author Share Posted October 27, 2020 2 minutes ago, Barand said: Here's code to translate the algorithm I gave you So is this finding the non transparent pixels? Quote Link to comment https://forums.phpfreaks.com/topic/311624-use-php-gd-to-apply-texture-image-on-top-of-another-ignoring-transparent-pixels/#findComment-1582082 Share on other sites More sharing options...
Barand Posted October 27, 2020 Share Posted October 27, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/311624-use-php-gd-to-apply-texture-image-on-top-of-another-ignoring-transparent-pixels/#findComment-1582083 Share on other sites More sharing options...
glenelkins1984 Posted October 27, 2020 Author Share Posted October 27, 2020 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 Quote Link to comment https://forums.phpfreaks.com/topic/311624-use-php-gd-to-apply-texture-image-on-top-of-another-ignoring-transparent-pixels/#findComment-1582085 Share on other sites More sharing options...
glenelkins1984 Posted October 29, 2020 Author Share Posted October 29, 2020 (edited) 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) Edited October 29, 2020 by glenelkins1984 Quote Link to comment https://forums.phpfreaks.com/topic/311624-use-php-gd-to-apply-texture-image-on-top-of-another-ignoring-transparent-pixels/#findComment-1582128 Share on other sites More sharing options...
glenelkins1984 Posted October 29, 2020 Author Share Posted October 29, 2020 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 Quote Link to comment https://forums.phpfreaks.com/topic/311624-use-php-gd-to-apply-texture-image-on-top-of-another-ignoring-transparent-pixels/#findComment-1582134 Share on other sites More sharing options...
Barand Posted October 29, 2020 Share Posted October 29, 2020 1 hour ago, glenelkins1984 said: doesn't work Then you ain't doing it right. My background pattern... 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... Quote Link to comment https://forums.phpfreaks.com/topic/311624-use-php-gd-to-apply-texture-image-on-top-of-another-ignoring-transparent-pixels/#findComment-1582136 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.