如何在背景上显示存储在数据库中的重复图像在Twitter上? [英] How to display a repeated image stored in database on background as on Twitter?

查看:87
本文介绍了如何在背景上显示存储在数据库中的重复图像在Twitter上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要在背景上显示一个图像(通过使用CSS重复)与在Twitter上相同的方式,但问题是,我从一个MySQL数据库中检索它,CSS不能处理 src 标签。



如何从MySQL数据库检索图像(我知道如何通过从数据库检索来显示图像,但这里我想以不同的方式显示),即,将图像显示为背景在主体上,并且图像应该重复,如同使用CSS repeat 语句。像twitter.com。



我的代码是在PHP和MySQL。我需要获取从数据库检索此图片的网址。

解决方案

您可以分两步执行。 / p>

创建一个PHP脚本,接受一个参数,通过唯一的ID来识别哪一行具有要显示的图像。此脚本将从数据库中提取映像并发送带有适当MIME类型的代码,以便浏览器了解。这样,将一个类应用到容器(或body标签)并显示背景:

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

示例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){
casetext:
$ theValue =($ theValue!=)? '。 $ theValue。 ' : 空值;
break;
caselong:
caseint:
$ theValue =($ theValue!=)? intval($ theValue):NULL;
break;
casedouble:
$ theValue =($ theValue!=)? doubleval($ theValue):NULL;
break;
casedate:
$ theValue =($ theValue!=)? '。 $ theValue。 ' : 空值;
break;
casedefined:
$ 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)或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);
?>


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.

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), i.e display the image as backround on body and the image should repeat as if CSS repeat statement were used. Like on twitter.com.

My code is is in PHP and MySQL. I need to get the URL for as this image is retrieved from a database.

解决方案

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. This script will extract the image from database and send the codes with appropriate mime-type, so that browser understands. This way, apply a class to the container (or body tag) and display the background like:

.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 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);
?>

这篇关于如何在背景上显示存储在数据库中的重复图像在Twitter上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆