Use AJAX to display php generated image

前端 未结 6 2031
攒了一身酷
攒了一身酷 2021-01-25 23:44

I have a php script that randomly generates an image. Something like this:



        
6条回答
  •  温柔的废话
    2021-01-26 00:03

    The resulting image has to be base64 encoded to be included like that.

    So you need to do the following:

    • Edit the image
    • Get resulting image in data string.
    • To get image string, you either store it to filesystem and read it through file_get_contents() (useful for cache) or use imagejpeg() without location, which places the image in output buffer. To get the value from output buffer use ob_start() and ob_get_contents().
    • Convert data string of the image to base64 (using base64_encode())
    • Return this string to browser
    • Set image "src" field to "data:image/png;base64,[BASE64]" where [BASE64] is the string returned from PHP.

提交回复
热议问题