【问题标题】:How to add image to our site with extension .php如何使用扩展名 .php 将图像添加到我们的网站
【发布时间】:2015-09-17 07:56:42
【问题描述】:

抱歉,这是个愚蠢的问题。我在php.net 上看到可以这样做。

我喜欢这个

$imagepath="smile.jpg";

$image=imagecreatefromjpeg($imagepath);
$imgheight=imagesy($image);
$color=imagecolorallocate($image,0, 153, 51);
imagestring($image, 20, 50, $imgheight-30, "this is testing", $color);
header('Content-Type: image/jpeg');
imagejpeg($image);
imagedestroy($image);

我的代码可以在浏览器上显示图像,当我检查它的元素时,它会显示在扩展名 .php 中调用,但我无法在其上添加文本或样式或任何内容。

例如,我只是添加更多这样的代码

<div style="height: 600px;width: 100%; position: relative; border:1px solid red">
<div>
    <?php echo "Hello text"; ?>
</div>
<div style=" float: left;height: 600px;margin: 0 auto;border:1px solid yellow">
    <?php
        $imagepath="smile.jpg";
        $image=imagecreatefromjpeg($imagepath);
        $imgheight=imagesy($image);
        $color=imagecolorallocate($image,0, 153, 51);
        imagestring($image, 20, 50, $imgheight-30, "this is testing", $color);
        header('Content-Type: image/jpeg');
        imagejpeg($image);
        imagedestroy($image);
    ?>
</div>
<div style="clear:both"></div>

节目

任何人都可以帮我解决这个问题

谢谢

【问题讨论】:

  • "但我不能在上面添加文字或样式或任何东西。",你是什么意思?
  • 我认为他的意思是该页面不支持除图片本身之外的任何其他内容。这是因为标题类型@pov 正在使用 ^^
  • 浏览器上的内容只有图像,我想添加文本或样式以及任何正常页面
  • 也许也发布 html 和 css 代码?这样我们就可以按照您的想法进行操作
  • 您可以使用 PHP 提供图像,但您必须只提供图像。您不能使呈现的 HTML 页面显示为图像。

标签: php image file-get-contents readfile


【解决方案1】:

从 PHP 提供图像的最简单方法是这样。这将起作用,但您将无法在代码本身中对图像进行任何更改。

然而允许您在您的服务器上执行其他操作,例如记录到查看图像的文件或数据库。

$image = file_get_contents('/path/to/image/.jpg');
header('Content-type: image/jpg');
echo $image;
die;

【讨论】:

    【解决方案2】:

    你的图片代码不能在html中内联工作的问题是因为header

    header('Content-Type: image/jpeg');
    

    您只能在提供任何内容之前设置标头(在您的情况下,echo html 在 header() 之前会使其失败。 引用 php 渲染的图像只能通过使用单独的图像的 php 文件来完成,并引用带有 &lt;img src="file.php"&gt; 的文件。

    我了解您也想添加一些文本,但该文本不是图像的一部分,无法使用这些标题提供。要使文本位于图像中或图像下方,您需要更改图像本身。

    要使用 php 编辑图像,请查看此处的页面以获取一些有用的功能: http://www.php.net/manual/en/book.image.php

    希望对您有所帮助。

    【讨论】:

    • 是的,我也是这样想的,因为我的页面是header('Content-Type: image/jpeg');,所以我不能满足为什么我发布这个问题以找到我需要的解决方案的文本,我想知道如何页面php.net可以的
    • php.net 不呈现文本。它只是文本 php 的图像。
    • 在我的问题中,或者您可以检查 php.net 徽标上的元素,它是图像,但并显示 &lt;img src="/images/logo.php" width="48" height="24" alt="php"&gt; 我也想喜欢这个网站
    【解决方案3】:

    你必须将 php 代码放入另一个文件并添加退出指令并将标头指令放在开头,如下所示:

    header('Content-Type: image/jpeg'); //Before every instruction
    $imagepath="smile.jpg";
    
    $image=imagecreatefromjpeg($imagepath);
    $imgheight=imagesy($image);
    $color=imagecolorallocate($image,0, 153, 51);
    imagestring($image, 20, 50, $imgheight-30, "this is testing", $color);
    
    imagejpeg($image);
    imagedestroy($image);
    
    exit; // You have to add exit instruction to escape rendered attempt 
    

    【讨论】:

      猜你喜欢
      • 2014-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-13
      • 2012-11-19
      • 1970-01-01
      相关资源
      最近更新 更多