I would use CSS for this. You should read about adjacent sibling selectors.
If your HTML is this:
<div class="image-block">
<!-- img here -->
</div>
<div class="text-block">
text here
</div>
You can target .text-block that is immediately after .image-block with this CSS:
.image-block + .text-block {
/* CSS here */
}
You could then either collapse top margin or padding in that scenario or add whatever else you need depending on your CSS.