2011-10-04 18:55

# 如何计算在我班级写水印文本的位置

i just finished my class to make water mark text on image

here is my problem

1 - user select font file

2 - user select font color

3 - user select watermark position

4 - user select font size

So - my problem is how to calculate the right position depended on font size , family

here is my code i just tried

`

\$width = 400; //image width

\$height = 300; //image height

if(\$this -> text_posi == 'topleft'){

\$x1  = (\$width  - \$width ) + 20;
\$y1  = (\$height - \$height) + 40;

}elseif(\$this -> text_posi == 'topright'){

\$x1  = \$width  - 200;
\$y1  = (\$height - \$height) + 40;

}
elseif(\$this -> text_posi == 'bottomleft'){

\$x1  = (\$width  - \$width ) + 20;
\$y1  = \$height - 30;

}
elseif(\$this -> text_posi == 'bottomright'){

\$x1  = \$width  - 200;
\$y1  = \$height - 30;

}
elseif(\$this -> text_posi == 'topcenter'){

\$x1  = \$width  * .5 - 100;
\$y1  = (\$height  - \$height) + 30;

}
elseif(\$this -> text_posi == 'bottomcenter'){

\$x1  = \$width  * .5 - 100;
\$y1  = \$height - 30;

}
elseif(\$this -> text_posi == 'rightcenter'){

\$x1  = \$width  - 180;
\$y1  = \$height * .5;

}
elseif(\$this -> text_posi == 'leftcenter'){

\$x1  = \$width  - \$width + 20;
\$y1  = \$height * .5 ;

}else{

\$x1  = (\$width  / 2) - 30;
\$y1  = (\$height / 2) - 20 ;
}

`

I ThInk It fine if image more than width >= 500 and height >= 500

Can You Help Please

• 点赞
• 写回答
• 关注问题
• 收藏
• 复制链接分享
• 邀请回答

#### 1条回答

• Turn away from your monitor, grab a pencil and a piece of paper and draw some pictures:

watermark in the bottom-right corner

img_width
|----------- -----------------|<--|
|                             |   |
|                             |   |
|                             |   |
|                             |   |
|                             |   | mark Y offset
img_height  |                             |   |
|                             |   |
|                             |   |
|                             |   |
|                mark_width   |   |
|               |-------------|<--|
|               |             |
|   mark_height |             |
|               |             |
|-----------------------------|
^               ^
|_______________|
mark X offset

It should be immediately obvious how to calculate the values for X and Y offsets. To find out how big the watermark is, use http://www.php.net/manual/en/function.imagettfbbox.php

点赞 评论 复制链接分享