【发布时间】:2014-12-02 17:14:30
【问题描述】:
我正在学习使用 JQuery 和 PHP。我只是想以特定的时间间隔在浏览器上显示一个文本和一个 PHP 生成的图像。
HTML 页面:
<html>
<head>
<title>Testing PHP</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
<!--
$(document).ready(function() {
loadPHP();
});
function loadPHP() {
$("#PHP_data").load("loadPHP.php");
setTimeout(loadPHP, 2000);
}
//-->
</script>
</head>
<body>
<div id="PHP_data"></div>
</body>
</html>
加载PHP页面
<?php
$rannum = mt_rand(1,100);
echo $rannum;
$thick = 10;
// create a 200*200 image
$img = imagecreatetruecolor(200, 200);
// Add antialias
imageantialias ($img, true);
// allocate some colors
$white = imagecolorallocate($img, 255, 255, 255);
// draw the dashed circle
for($t = 1;$t<($thick+1);$t++) {
for($i = 0;$i<360;$i+=10) {
imagearc($img, 100, 100, 200-($t/5), 200-($t/5), $i, $i+5, $white);
imagearc($img, 100, 100, 200+($t/5), 200+($t/5), $i, $i+5, $white);
}
}
// output image in the browser
header("Content-type: image/png");
imagepng($img);
// free memory
imagedestroy($img);
?>
当我调用 HTML 页面时,我得到以下信息:
随机数似乎工作正常。我确实看到它每两秒就会改变一次,但图像都搞砸了。我看到的只是二进制字符。这是因为您只能从 PHP 中返回图像并且绝对没有 TEXT 以及图像吗?我该如何解决这个问题?
【问题讨论】:
-
HTML 不能那样工作。您需要考虑将内容加载到
<img>元素的src属性中,而不是加载到 div 中。
标签: javascript php jquery html image