如何获取从mysql中取出的图像作为背景?

时间:2013-01-01 18:33:55

标签: php html mysql css

  

可能重复:
  How to display a repeated image stored in database on background as on Twitter?

我想在背景上显示图像(通过使用CSS重复的东西),就像在twitter上一样,但问题是我从MySql数据库中检索它并且CSS无法处理背景上的src标记我已经使用了以下代码和链接

body{
    background:url(<?php $lastid=$_SESSION['lastid']; echo "get_test.php?id=$lastid";?>)     repeat;

   }

http://www.webdeveloper.com/forum/showthread.php?210964-Set-CSS-Background-Image-with-PHP

http://net.tutsplus.com/tutorials/php/supercharge-your-css-with-php-under-the-hood/

我在主站点上的整个代码

<html>
<head>
<?php
$temp = tmpfile();
?>
<style>
  body{
    background:url(<?php $lastid=$_SESSION['lastid']; echo "get_test.php?id=$lastid"; ?>) repeat;

   }


</style>
</head>
<body>
<form action="" method="POST" enctype="multipart/form-data">
File:<br>
<input type="file" name="image">

<input type="submit" name="submit" value="Upload">

</form>



<?php

mysql_connect("host", "nam", "database") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());

$file= $_FILES['image']['tmp_name'];

$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name = addslashes($_FILES['image']['name']);
$image_size = addslashes(getimagesize($_FILES['image']['tmp_name']));

if($image_size==FALSE)
{
echo "thats not an image";
}

else
{

mysql_query("INSERT INTO image_store VALUES ('','$image_name','$image')") or die(mysql_error());
 $lastid=mysql_insert_id();

$_SESSION['lastid']=$lastid;
 echo "the image is"."<image src=get_test.php?id=$lastid>";


}

?>
</body>

并且get_test.php上的代码是

<?php
mysql_connect("host", "nam", "database") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());


$id = addslashes($_REQUEST['id']);

$image=mysql_query("SELECT * FROM image_store WHERE id=$id ");
$image = mysql_fetch_assoc($image);
$image = $image['image'];

header("Content-Type: image/jpeg");
echo $image;

?>

2 个答案:

答案 0 :(得分:0)

回答这个问题:

1:在最后一个分号后添加一个空格。这之前引起了人们的关注。

2:要停止重复背景,请使用background-repeat属性,并将其设置为no-repeat,如下所示:

background-repeat: no-repeat;

答案 1 :(得分:0)

要设置/使用$_SESSION变量(即$_SESSION['lastid'],您需要在每个页面上启动会话 -

<?php
session_start();
?>
<html>
<head>
<?php
$temp = tmpfile();
?>
<style>
  body{
    background:url(<?php $lastid=$_SESSION['lastid']; echo "get_test.php?id=$lastid"; ?>) repeat;
   }
</style>
</head>
<body>
...
</body>