简体   繁体   English

如何像Twitter一样在后台显示数据库中存储的重复图像?

[英]How to display a repeated image stored in database on background as on Twitter?

I want to display an image on background (by using CSS repeat) the same way as on Twitter but the problem is that I am retrieving it from a MySQL database and CSS cannot handle the src tag on background. 我想在背景上显示图像(通过使用CSS重复)与在Twitter上相同,但是问题是我正在从MySQL数据库中检索图像,而CSS无法在背景上处理src标签。

How can I retrieve an image from the MySQL database (I know how to display image by retrieving from database but here I want to display it differently), ie display the image as backround on body and the image should repeat as if CSS repeat statement were used. 如何从MySQL数据库检索图像(我知道如何通过从数据库检索来显示图像,但是在这里我想以不同的方式显示),即在主体上显示为背景,并且图像应该像CSS repeat语句那样repeat用过的。 Like on twitter.com. 就像在twitter.com上一样。

My code is is in PHP and MySQL. 我的代码在PHP和MySQL中。 I need to get the URL for as this image is retrieved from a database. 我需要获取URL,因为从数据库中检索了此图像。

You cannot repeat <img src="#" /> so for that you need to use CSS in your PHP document, you can do it like 您不能重复<img src="#" />因此您需要在PHP文档中使用CSS,您可以这样做

<style>
body {
   background-image: url('<?php echo $whatever; ?>') !important;
   background-repeat: repeat;
}
</style>

Either you can do is 您可以做的是

Make a stylesheet with a .php extension. 制作一个扩展名为.php的样式表。

Than <link rel='stylesheet' type='text/css' href='css/stylesheet.php' /> <link rel='stylesheet' type='text/css' href='css/stylesheet.php' />

At the top of the page mention this 在页面顶部提及此

<?php
    header("Content-type: text/css; charset: UTF-8");
?>

Now you can set variables accordingly 现在您可以相应地设置变量

<?php
    header("Content-type: text/css; charset: UTF-8");
    $background = "$imgsrc"; /* Retrieve your image here */

    /*Now simply use the $background variable for setting the body background */
?>

You can do this in two steps. 您可以分两步执行此操作。

Create a PHP script that accepts a parameter to identify through a unique ID, which Row has the image to display. 创建一个PHP脚本,该脚本接受一个参数以通过唯一ID进行标识,该Row具有要显示的图像。 This script will extract the image from database and send the codes with appropriate mime-type, so that browser understands. 该脚本将从数据库中提取图像,并以适当的mime类型发送代码,以便浏览器理解。 This way, apply a class to the container (or body tag) and display the background like: 这样,将一个类应用于容器(或body标签)并显示背景,如下所示:

.backgroundTile { background-image: url('/path/to/php-image-render-script.php?image_id=1212') !important; background-repeat: repeat; }

Example PHP Script (Source- http://cookbooks.adobe.com/post_Display_an_image_stored_in_a_database_ PHP -16637.html ) : PHP脚本示例( 来源-http : //cookbooks.adobe.com/post_Display_an_image_stored_in_a_database_ PHP -16637.html ):

<?php require_once('Connections/testConn.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$colname_getImage = "-1";
if (isset($_GET['image_id'])) {
  $colname_getImage = $_GET['image_id'];
}
mysql_select_db($database_testConn, $testConn);
$query_getImage = sprintf("SELECT mimetype, image FROM images WHERE image_id = %s", GetSQLValueString($colname_getImage, "int"));
$getImage = mysql_query($query_getImage, $testConn) or die(mysql_error());
$row_getImage = mysql_fetch_assoc($getImage);
$totalRows_getImage = mysql_num_rows($getImage);
header('Content-type: ' . $row_getImage['mimetype']);
echo $row_getImage['image'];
mysql_free_result($getImage);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM