您可以分两步执行此操作。
创建一个 PHP 脚本,该脚本接受参数以通过唯一 ID 进行标识,该 ID 具有要显示的图像。该脚本将从数据库中提取图像并发送具有适当 mime 类型的代码,以便浏览器理解。这样,将一个类应用到容器(或 body 标签)并显示背景,如下所示:
.backgroundTile { background-image: url('/path/to/php-image-render-script.php?image_id=1212') !important; background-repeat: repeat; }
示例 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);
?>