使用PHP函数显示具有绝对定位的图像?

时间:2012-09-25 22:12:22

标签: php image function css-position

我从来没有使用PHP的一件事是按需图像放置 - 但现在这个问题已经出现了......

我需要一个函数,在给定X和Y坐标的情况下,使用某种形式的绝对定位在屏幕上的某处放置.png或.gif。

恐怕我完全不知道从哪里开始寻找这些信息。对我来说,PHP是关于显示数据库中的数据。这对我来说是一个完全陌生的概念! :)

1 个答案:

答案 0 :(得分:2)

也许这个例子可以帮助您入门。 我使用HTML,CSS和PHP

<?php 
/*
 * here you can define your coordinates in pixel
 */
$myXCoordinate = '100px';
$myYCoordinate = '200px';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">    
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Your Page title</title>
  <style type="text/css">
  .absolute-pos {
    position: absolute;
    width: 160px;  /* here you could set a fixed pixel size, e.g. 200px */
    height: 160px; /* here you could set a fixed pixel size, e.g. 200px */
    left: <?php echo $myXCoordinate; ?>;
    top: <?php echo $myYCoordinate; ?>;
  }
  </style>

</head>
<body>
  <p>showing the logo of wikipedia [source: upload.wikimedia.org/wikipedia/en/b/bc/Wiki.png]</p>
  <img class="absolute-pos" src="http://upload.wikimedia.org/wikipedia/en/b/bc/Wiki.png" alt="Some description." />
</body>
</html>

复制此代码并将其保存到php文件,例如“的index.php”。然后更改$ myXCoordinate和$ myYCoordinate的值。这将改变图像的绝对位置。