How to display an inline image generated on the fly using PHP GD

人盡茶涼 提交于 2019-12-01 06:35:53

Convert it to Base64 with base64_encode and echo it as a dataURI in an img tag !

Inline Images with Data URLs

You can either:

  1. Display the image as data: http://en.wikipedia.org/wiki/Data_URI_scheme (Warning: High bandwidth consumption.)
  2. Make a separate file that display the image, such as display_image.php

And use the code:

<img src="display_image.php">

With the header and code you have shown.

<img src="image.php?other_image=(filename)">

and create your image in image.php, output with

header('Content-Type:image/png');imagepng($main_image);

You can also put the image creation part in the same script:

if($other_image=$_GET['other_image'])
{
    // create image
    ...
    // output image
    header('Content-Type:image/png');
    imagepng($main_image);
}
else
{
    // default behaviour
    ...
    echo '<img src="',basename(__FILE__),'?other_image=',urlencode('images/other.png'),'">';
    ...
}

Take care to prevent injections!

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!