【问题标题】:how do I call create image function?如何调用创建图像功能?
【发布时间】:2014-07-29 01:33:58
【问题描述】:

我找到了这个话题:convert text to image in php

我测试了 Sourabh Shankar 提供的代码。

当我将此代码粘贴到一个新的空 php 文件中并调整字体路径时,它可以完美运行!

但是当我在 header('Content-type: image/png'); 之前或之后放置一个回显时行,然后一切都失败了

谁能给我一个如何在 HTML 文件中调用此图像创建器的代码:

示例:

<span>Here comes the image</span>
<?php [call function??] ?>
<span>and here we continue with some HTML</span>

我需要创建一个php函数吗?如果需要:

我究竟要返回什么? 如果我返回这个:

imagepng($im);

然后他不会执行

imagedestroy($im);

我不知道该怎么做

【问题讨论】:

    标签: php http-headers


    【解决方案1】:

    您链接到的 PHP 代码作为独立文件工作,因此当浏览器请求它时,它会发回图像。用

    设置标题
    header('Content-type: image/png');
    

    表示代码将返回浏览器解释为图像文件的内容,而不是 HTML 文件。

    您不能使用 PHP 直接将文件输出为 HTML。您需要在单独的文件中使用该代码并将其链接到代码中图像的 src 标记。

    【讨论】:

      【解决方案2】:

      你需要通过http从其他脚本获取图片。

      <span>Here comes the image</span>
      <img src="/path/to/image_handler.php"/>
      <span>and here we continue with some HTML</span>
      

      其中 image_handler.php - 你的 php 脚本:

      <?php
      …
      header('Content-type: image/png');
      imagepng($im);
      

      注意:在调用header之前不要输出任何东西

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-20
        • 2018-09-09
        • 2020-07-25
        • 1970-01-01
        • 2021-05-30
        相关资源
        最近更新 更多