如何用js和css给一个站内所有图片加上水印

html-css013

如何用js和css给一个站内所有图片加上水印,第1张

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Document</title>

<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>

<style>

.str {

font: normal bold 5px Arial

/*font-size: 5px*/

color: rgb(224, 226, 226,0.4)

position: absolute

padding-left: 16px

padding-top: 53px

display: none

}

.str2 {

font: normal bold 5px Arial

/* font-size: 5px*/

color: rgb(224, 226, 226,0.4)

position: absolute

padding-left: 66px

padding-top: 130px

display: none

}

</style>

</head>

<body>

<div>HOMEANGEL</div>

<div>HOMEANGEL</div>

<img src="https://dss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3252521864,872614242&fm=26&gp=0.jpg" style="width:100pxheight:100px" />

<div id="picture" style="width:100%margin-left: 60px">

</div>

<script>

$(function () {

var img = ["https://dss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3252521864,872614242&fm=26&gp=0.jpg"]

GetCanvas(img)

})

function AddCanvas(src, ById) {

var img = new Image()

img.src = src

img.onload = function () {

imgW = img.width

imgH = img.height

//准备canvas环境

var canvas = document.getElementById(ById)

canvas.width = img.width

canvas.height = img.height

var ctx = canvas.getContext("2d")

// 绘制图片

ctx.drawImage(img, 0, 0, img.width, img.height)

// 绘制水印

ctx.font = "20px bold Arial"

ctx.fillStyle = "rgb(224, 226, 226,0.6)"//这里是颜色

ctx.fillText("watermark watermark", 20, 60)

ctx.fillText("watermark watermark", 120, 160)

ctx.fillText("watermark watermark", 220, 220)

//ctx.fillText("HOMEANGEL", 90, 130)

canvas.style.width = "100px"

canvas.style.height = "100px"

}

}

function GetCanvas(Strhtml) {

if (Strhtml.length >0 &&Strhtml != "") {

var html = ""

for (var i = 0i <Strhtml.lengthi++) {

var str = "sample" + (i + 1)

html += ' <canvas id=' + str + ' name="test" src=' + Strhtml[i] + ' "></canvas>'

}

$("#picture").html(html)//这里图片添加到html,然后for,添加水印

}

for (var i = 0i <Strimg.lengthi++) {

var str = "sample" + (i + 1)

var src = Strhtml[i]

AddCanvas(src, str)//id没传递

}

}

</script>

</body>

文字在图片上显示

<!DOCTYPE html>

<html>

<head>

<title></title>

<meta charset="UTF-8">

<style>

#abox{

position:relative/*容器相对定位*/

width:300px/*宽度300px*/

margin:0 auto/*横向居中*/

}

#abox img{

width:300px/*宽度300px*/

}

span{

position:absolute/*文字容器绝对定位*/

display:block/*span转为块元素*/

width:100%/*宽度相对于父容器100%*/

text-align:center/*文字居中*/

top:0/*距离父容器顶部0*/

left:0/*距离父容器左侧0*/

color:red/*文字颜色红色*/

font-size:18px/*文字大小既文字高度18px*/

font-weight:bold/*文字加粗*/

}

</style>

</head>

<body>

<div id="abox"><!--容器,相对定位-->

  <img src="123.jpg"><!--图片-->

  <span>我要显示文字</span><!--文字,绝对定位-->

</div>

</body>

</html>