问题描述
对于这个项目,我需要根据来自MySQL数据库的数据为页面创建动态标识。该表存储图像索引,前景色和背景色。为了测试目的,我在Photoshop中创建了一个小图像来模拟图像,并且我使用随机数字来创建前景色。
我想要在每个页面上创建多个随机图片,但浏览器会保持图片的资源不变每次通话。有没有一种方法可以在同一页面上创建多个随机图像?
我试图让它使用随机查询字符串和输出缓冲工作,但他们避难'b
$ b
截图:
目录结构:
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'> ($ i = 0; $ i {
echo< img src ='logo.png'
<?php
' />中;
}
?>
< / body>
< / html>
logo.png / index.php $ b
<?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);
?>
有可能您的图片被浏览器缓存,所以只有一个请求正在进行,并且所有十个调用都重复相同的图片。
您需要使用随机GET参数使每个URL看起来不同,例如像这样:
pre $ <?php
for($ i = 0; $ i {
$ postfix = mt_rand(0,10000);
echo< img src ='logo.png?random = $ postfix'/>;
}
?>
请注意,这会使图像无法缓存:GD会在每次请求时重新运行。但它看起来就是你想要的行为。
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:
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);
?>
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:
<?php
for ( $i = 0 ; $i <= 10 ; $i++ )
{
$postfix = mt_rand(0, 10000);
echo "<img src='logo.png?random=$postfix' />";
}
?>
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.
这篇关于每页多个GD图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!