I am trying to apply pattern on a transparent image with GD library.
I have an inverse transparent image and a couple of pattern images to apply on placeholder.
Any suggestion how to achieve this using GD library or image magic.
I can tell you how to do it in ImageMagick Unix command line syntax. The basic idea is to use -compose hardlight between the shirt and the tiled pattern.
First step is to trim off your ragged border around your pattern image. So I manually cropped it to look as follows so it could be tiled without the ragged borders showing:
When you use -compose hardlight, the effect is best if the average color of the image is mid gray (50%). So the first thing is to get the average gray level value and the difference from mid gray:
mean=`convert shirt.png -scale 1x1! -alpha off -format "%[fx:100*mean]
" info:`
diff=`convert xc: -format "%[fx:50-$mean)]" info:`
echo "mean=$mean; diff=$diff;"
mean=86.9006; diff=-36.9006;
Note: The above method gets the average gray level of only the opaque shirt and ignores areas that are transparent.
Next, you need to remove the alpha channel and save it for later. Then modify the image without the alpha channel by adding back the required difference to get it to mean of mid gray. Then you need to modify the contrast to increase it so that the shading of the wrinkles show.
convert shirt.png -alpha extract mask.png
convert shirt.png -alpha off -evaluate add $diff% -sigmoidal-contrast 10,50% shirt_mod.png
Then you tile out the pattern with a resizing to make the pattern more dense than in your original. And finally composite with hardlight and put the mask back into the alpha channel.
convert shirt_mod.png \
\( -clone 0 -resize 400% -tile pattern.png -draw "color 0,0 reset" -resize 25% \) \
-compose hardlight -composite \
mask.png -alpha off -compose copy_opacity -composite \
shirt_pattern.png
Note that there are many ways to tile out the pattern to fill some image dimension. See http://www.imagemagick.org/Usage/canvas/#tile
You can change the brightness by using -modulate brightness,saturation,hue when creating the tiled image. The defaults for no change are 100. So if you want to lower the brightness reduce it from 100. -modulate also allows you to change the saturation of the color and even the hue. It might also help if the pattern is tighter, more like yours. So I have changed the resize arguments.
So use the same commands as in my previous post, but change the last one as follows
Brightness=95
convert shirt_mod.png \
\( -clone 0 -resize 500% -tile pattern.png -draw "color 0,0 reset" -resize 20% -modulate 95,100,100 \) \
-compose hardlight -composite \
mask.png -alpha off -compose copy_opacity -composite \
shirt_pattern1.png
Brightness=90
convert shirt_mod.png \
\( -clone 0 -resize 500% -tile pattern.png -draw "color 0,0 reset" -resize 20% -modulate 90,100,100 \) \
-compose hardlight -composite \
mask.png -alpha off -compose copy_opacity -composite \
shirt_pattern2.png
Adjust the resize and brightness as desired.