doupiai5597 2017-05-16 16:01
浏览 42
已采纳

在PHP中创建随机PNG并在html中输出

i have a php file "builder.php" which is generating a random png file with imagepng and imagecopyresized. Every reload is generating a different png. I want to display a few of the generated pics in html to deside which is good and wich not. the good ones should be saved as png an the bad ones not.

builder.php

<?php

...
$mix=getmix($link);
header('Content-Type: image/png');

    $base = imagecreatefrompng("base.png");
    $logo = imagecreatefrompng("fs_logo_line.png");
    $nr1 = imagecreatefrompng("template_women_number_1.png");

    $pos1 = imagecreatefrompng($mix['jhjk']['img']);
    $pos2 = imagecreatefrompng($mix['hkjh']['img']);

    imagecopyresized($base,$pos1,0, 0, 0, 0, 501, 697, 501, 697);
    imagecopyresized($base,$pos2,451, 0, 0, 0, 485, 697, 485, 697);
    imagecopyresized($base,$nr1,20, 20, 0, 0, 39, 38, 39, 38);
    imagecopyresized($base,$logo,0, 1136, 0, 0, 1200, 64, 1200,64);

    imagepng($base);
    imagedestroy($base);
    imagedestroy($logo);
?>

$mix is a variable array with sql data. With evey reload $mix will be shuffled.

html page where the images should be loaded:

<!DOCTYPE html>
  <head>
  </head>
  <body>
    <img src="builder.php" />
    <img src="builder.php" />
  </body>
</html>

The Problem is that it seems that the builder.php just loaded only one time and displays the same pic twice. if i load builder.php standalone it generates a new pic every reload. i know i could work with get/post to change the builder.php but in this case there's no need, or?

  • 写回答

1条回答 默认 最新

  • doupa9062 2017-05-16 16:07
    关注

    Think about it for a second - at the time PHP runs on the server there is only one builder.php requested and cached. One way to fix this is to add a random string to your image source string:

    <img src="builder.php?t=<?php echo uniqid(); ?>" />
    

    Now each call to builder.php is unique and should return a random image.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog