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.

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

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP