views:

60

answers:

4

Hello!

I need to solve the following problem.

I have two (or more) .PNG images of the same dimensions. Each PNG image is created with transparent background color.

I need to display img1 and upon it img2, so in places where img2 has trancparent color, img1 will be seen.

For example: img1 upper part filled with transparent color and a cow on down part. img2 upper part contains clouds and downpart filled with transparent color.

I want to combine these two images and see clouds above the cow.

I understand that I need to use some filter when displaying both images, but not sure which one and what parameters of it to use.

+3  A: 

You don't need to use any sort of filter (except in IE6).

You can simply place to <img> elements on top of each-other, using CSS position: absolute to make both elements occupy the same area.

In IE6, you'll need an AlphaImageLoader filter simply to display the PNGs with transparency

SLaks
Sorry, haven't mentioned it: I am in IE6 :(Is there any way to solve this?
Leo
@Leo: Yes; you need to use an `AlphaImageLoader` filter. http://24ways.org/2007/supersleight-transparent-png-in-ie6
SLaks
I understood that in IE6 i need to use filter, like: filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(...);Can anybody please write how to do it?Thanks
Leo
`filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src="SomeImage.png", sizingMethod="image");`
SLaks
A: 

You can try setting their .style.position to "absolute", give them the same coordinates with left and top (or right or bottom), and then change their z-index. Although it's quite dirty, and maybe it doesn't work well with your page, but I can't think of another solution.

Pikrass
+2  A: 

Something like this should work (using just HTML/CSS):

HTML:

<div class="imageWrapper">
  <img class="overlayImage" src="cow.png">
  <img class="overlayImage" src="clouds.png">
  <img class="overlayImage" src="downpart.png">
</div>

CSS:

.imageWrapper {
  position: relative;
}
.overlayImage {
  position: absolute;
  top: 0;
  left: 0;
}

Keep in mind that IE6 does not handle transparency in PNGs very well. If you need it to work in IE6, you will need to apply the filters you mentioned. This procedure is detailed here.

pkaeding
I used your example (with CSS and HTML) and it always displays the second image and I don't see parts of the first looking one through. My images definitely have transparent backgroung, I used the following PHP for creating them:<p>$resultImage = imagecreatetruecolor (100, 100); $trans_colour = imagecolorallocatealpha ($resultImage, 0, 0, 0, 127); imagefill ($resultImage, 0, 0, $trans_colour); And later painter over the image with non-transparent color. </p>
Leo
Yeah, if you are using IE 6, then you will need to add the filter to make transparency work at all.
pkaeding
A: 

If you are using jquery try this in any browser

<script>
$(function () {
    var position = $("#i1").offset();
    $('#i2').css({ position:'absolute', top:position.top, left: position.left});
});
</script>
<img id='i1' src='images/zap_ring.png' />
<img id='i2' src='images/zap_ring_hover.png' />

and adjust top & left values

$('#i2').css({ position:'absolute', top:position.top-10, left: position.left+5});
Mithun P
using jQuery in this case is like using AK47 to kill a fly.
rochal
I used style="filter:progid:DXImageTransform.Microsoft.Alpha(Opacity=50)" for each image and they are both seen half transperent one upon the other.I need completely different efect: see the second image 100% opacity while in places where it has transparent color see the first image.
Leo