每页多张 GD 图像

发布于 2024-12-21 20:50:42 字数 1447 浏览 2 评论 0原文

对于这个项目,我需要根据 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心不设防 2024-12-28 20:50:42

很可能您的图像已被浏览器缓存,因此仅发出一个请求,并且所有十次调用都会重复同一张图片。

您需要使用随机 GET 参数使每个 URL 看起来不同,例如:

<?php
    for ( $i = 0 ; $i <= 10 ; $i++ )
    {
        $postfix = mt_rand(0, 10000);
        echo "<img src='logo.png?random=$postfix' />";
    }
?>

但请注意,这会使图像不可缓存: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:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文