每页多张 GD 图像
对于这个项目,我需要根据 MySQL 数据库中的数据为页面创建动态徽标。该表存储图像索引、前景色和背景色。
出于测试目的,我在 Photoshop 中创建了一个小图像来模拟该图像,并使用随机数来创建前景色。
我想在每个页面创建多个随机图像,但浏览器在每次调用时都保持图像的资源相同。有没有办法在同一页面上创建多个随机图像?
我尝试使用随机查询字符串和输出缓冲让它工作,但他们没有给我任何运气。
屏幕截图:
https://i.sstatic.net/h0SLU.png
目录结构:
index.php
logo.png/
logo.png/circle.png
logo.png/index.php
index.php
<html>
<body style="background-color: #000000; color: #FFFFFF; font-family: 'Segoe UI'">
<?php
for ( $i = 0 ; $i <= 10 ; $i++ )
{
echo "<img src='logo.png' />";
}
?>
</body>
</html>
logo.png/index.php
<?php
$im = imagecreatefrompng("circle.png");
$white = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
$black = imagecolorallocate($im, 0x00, 0x00, 0x00);
$x = imagecolorexact($im, 0xFF, 0xFF, 0xFF);
$y = imagecolorexact($im, 0xCC, 0xCC, 0xCC);
$z = imagecolorexact($im, 0xAA, 0xAA, 0xAA);
$randx = mt_rand(0, 255);
$randy = mt_rand(0, 255);
$randz = mt_rand(0, 255);
imagecolorset($im, $x, $randx, $randz, $randz);
imagecolorset($im, $y, $randy, $randy, $randx);
imagecolorset($im, $z, $randz, $randx, $randy);
header('Content-Type: image/png');
imagepng($im);
?>
For this project, I'm needing to create dynamic logos for a page based on data from a MySQL database. The table stores an image index, a foreground color, and a background color.
For testing purposes, I've created a small image in Photoshop to simulate the image, and I am using random numbers to create the foreground colors.
I'm wanting to create multiple randomized images per page, but the browser is keeping the resource for the image the same on each call. Is there a way to create multiple random images on the same page?
I have attempted to try to get it to working using random query strings and output buffering, but they haven't given me any luck.
Screenshot:
https://i.sstatic.net/h0SLU.png
Directory structure:
index.php
logo.png/
logo.png/circle.png
logo.png/index.php
index.php
<html>
<body style="background-color: #000000; color: #FFFFFF; font-family: 'Segoe UI'">
<?php
for ( $i = 0 ; $i <= 10 ; $i++ )
{
echo "<img src='logo.png' />";
}
?>
</body>
</html>
logo.png/index.php
<?php
$im = imagecreatefrompng("circle.png");
$white = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
$black = imagecolorallocate($im, 0x00, 0x00, 0x00);
$x = imagecolorexact($im, 0xFF, 0xFF, 0xFF);
$y = imagecolorexact($im, 0xCC, 0xCC, 0xCC);
$z = imagecolorexact($im, 0xAA, 0xAA, 0xAA);
$randx = mt_rand(0, 255);
$randy = mt_rand(0, 255);
$randz = mt_rand(0, 255);
imagecolorset($im, $x, $randx, $randz, $randz);
imagecolorset($im, $y, $randy, $randy, $randx);
imagecolorset($im, $z, $randz, $randx, $randy);
header('Content-Type: image/png');
imagepng($im);
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很可能您的图像已被浏览器缓存,因此仅发出一个请求,并且所有十次调用都会重复同一张图片。
您需要使用随机 GET 参数使每个 URL 看起来不同,例如:
但请注意,这会使图像不可缓存:GD 将针对每个请求重新运行。但看起来这就是您想要的行为。
Chances are that your image is being cached by the browser, so there is only one request being made and the same picture is repeated for all ten calls.
You would need to make each URL look different using a random GET parameter, e.g. like this:
note however that this makes the images uncacheable: GD will run anew on every request. But it looks like that's the behaviour that you want.