简体   繁体   English

从相同的PHP文件输出图像,该图像也接收并发送JSON

[英]Outputting an image from the same PHP file which also receives & sends JSON

Ok, I'm hoping I can explain my situation rather than pasting lines and lines of code. 好的,我希望我能解释我的情况,而不是粘贴行和代码行。

Currently, JSON sends positional info to my PHP file which in turn uses this data to generate an image, saves it and returns the filename via JSON back to browser. 当前,JSON将位置信息发送到我的PHP文件,该文件随后使用此数据生成图像,将其保存并通过JSON将文件名返回给浏览器。 Javascript then refreshes the image on screen. 然后,Javascript刷新屏幕上的图像。

This all works fine at the moment, but I am wanting to optimise the process and look at the possibility of outputting the image file straight after it's created then save afterwards. 目前,这一切工作正常,但是我想优化流程,并研究在创建图像文件后立即输出然后再保存的可能性。

My ideal solution would be something like: 我理想的解决方案是:

    header('Content-Type: image/gif');
    echo $this->canvas;

    // Save user file
    $this->canvas->writeImage( $this->userFile = 'user_img.gif' );          
    $this->canvas->destroy();

    // encode everything and send to browser
    echo json_encode(array('misc data back to the browser'));

(I still need to send data back to browser via JSON) (我仍然需要通过JSON将数据发送回浏览器)

And in my HTML I would have the image laid out like this: 在我的HTML中,我会像这样布置图像:

    <img src='json-processing-script.php' />

But as usual nothing is ever that simple, so I'd like to hear if anyone can make any pointers. 但是像往常一样,没有什么比这更简单了,所以我想听听是否有人可以做任何指针。

In your example, the json would be added to the gif, messing up your image. 在您的示例中,将json添加到gif中,使您的图像混乱。 If you want to return these two completely different things from your php script, you would have to encode the image, add it to the json and extract it in the javascript to get the source of your image. 如果要从PHP脚本返回这两种完全不同的内容,则必须对图像进行编码,将其添加到json并在javascript中提取,以获取图像的来源。

See for example this question . 参见例如这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM