在 CSS 中将一张图片放在另一张图片上非常容易。你可以这样做:
<div class="container">
<img src="image1.png" alt="" />
<img src="image2.png" alt="" class="over" />
</div>
使用以下 CSS:
.container { position: relative; }
.container .over { position: absolute; top: 10px; left: 10px; }
但在你的情况下,这有点困难。我认为今天最好的方法是将模板图像放在顶部(当然其中有一些透明度),然后将用户上传的图像放在它下面。这样,裁剪将自动进行。会是这样的:
<div class="container">
<img src="image1.png" alt="" class="ontop" />
<img src="image2.png" alt="" class="below" />
</div>
.container { position: relative; }
.container .ontop{ z-index: 2; position: absolute; top: 0px; left: 0px; }
.container .below{ z-index: 1; position: absolute; top: 10px; left: 10px; }
然后,您可以使用 css 的宽度和高度属性来调整下面的图像大小。您也可以使用 transform 属性来旋转它。
.container { position: relative; }
.container .ontop{ z-index: 2; position: absolute; top: 0px; left: 0px; }
.container .below{ z-index: 1; position: absolute; top: 10px; left: 10px; transform: rotate(7deg); width: 100px; height: 50px; }
这将使前端显示。如果您希望用户能够下载最终图像,您应该使用 canvas 或 GD2 php 库。
当然,如果您希望自动生成(我参考您对自己帖子的评论),您可以使用 PHP 设置 CSS 属性值:
<img src="image2.png" alt="" class="below" style="top: <?php echo getTopPosition(); ?>px;" />