在一个 .php 中使用 header('Content-type: image/png') 和 echo"<html>"

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25776805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:59:42  来源:igfitidea点击:

Using header('Content-type: image/png') and echo"<html>" in one .php

phphtmlheaderresize

提问by user3475821

I want to use imagecreatefromjpeg, imagecreatetruecolor, imagecopyresizedand imagejpegwhile making use of the echo "<html><body>";etc...

我想使用imagecreatefromjpeg, imagecreatetruecolor,imagecopyresizedimagejpeg同时使用echo "<html><body>";等...

For some reason I get "The image could not be displayed because there were errors on the page"until I comment out header('Content-type: image/png');and then I just get a picture of a broken picture, like a torn page.

出于某种原因,我得到了"The image could not be displayed because there were errors on the page"直到我注释掉header('Content-type: image/png');然后我才得到一张破碎图片的图片,就像一个撕破的页面。

All I've seen is that I can't have header('Content-type: image/png');and html in the same .php file. If that's the case can anybody tell me how to resize an image for a thumbnail gallery while still having html in a .php file?

我所看到的只是我不能header('Content-type: image/png');在同一个 .php 文件中包含和 html。如果是这种情况,有人可以告诉我如何在 .php 文件中仍然包含 html 的同时调整缩略图库的图像大小吗?

Thanks in advance.

提前致谢。

回答by MC Emperor

You are mixing up two different things.

你正在混淆两种不同的东西。

A webpage with an image on it, does not containthat image in general. Instead, it often refers to an external source. I said in general, because, yes, an image canbe embedded in an HTML page, see below.

带有图像的网页通常不包含该图像。相反,它通常指的是外部来源。我说一般,因为,是的,图像可以嵌入到 HTML 页面中,见下文。

You have two options:

您有两个选择:

  • You can create an apart PHP file where you create the image and output its bytes. In your HTML code, you refer to that image:

    page.html:

    <html>
        <body>
            <img src="myimage.php" alt="" />
        </body>
    </html>
    

    myimage.php:

    <?php
    
    header("Content-Type: image/png");
    createimageandso_on();
    // Do the drawing.
    
    ?>
    
  • Or you can embed the image in your HTML file, using base64 encoding:

    <?php
    
    $contents = all_bytes_from_created_image();
    // Get the bytes from the created image.
    $base64 = base64_encode($contents);
    
    ?>
    <html>
        <body>
            <img src="data:image/png;base64,<?php echo $base64; ?>" alt="" />
        </body>
    </html>
    
  • 您可以创建一个单独的 PHP 文件,您可以在其中创建图像并输出其字节。在您的 HTML 代码中,您引用了该图像:

    页面.html

    <html>
        <body>
            <img src="myimage.php" alt="" />
        </body>
    </html>
    

    myimage.php

    <?php
    
    header("Content-Type: image/png");
    createimageandso_on();
    // Do the drawing.
    
    ?>
    
  • 或者,您可以使用 base64 编码将图像嵌入到 HTML 文件中:

    <?php
    
    $contents = all_bytes_from_created_image();
    // Get the bytes from the created image.
    $base64 = base64_encode($contents);
    
    ?>
    <html>
        <body>
            <img src="data:image/png;base64,<?php echo $base64; ?>" alt="" />
        </body>
    </html>
    

The second option is suitable for smaller images, since the base64 encoded string will produce large portions of text.

第二个选项适用于较小的图像,因为 base64 编码的字符串将生成大部分文本。



Edit

编辑

If I understand it correctly, you want to read images from a directory and resize them to the same size, using them as thumbnails?

如果我理解正确,您想从目录中读取图像并将它们调整为相同大小,将它们用作缩略图吗?

What you might just want to do is create a PHP file where you read a source image and give them the same size.

您可能只想创建一个 PHP 文件,在其中读取源图像并赋予它们相同的大小。

Just like 'normal' PHP files, PHP can do something with the request parameters you give. Perhaps you've ever seen this:

就像“普通”PHP 文件一样,PHP 可以使用您提供的请求参数执行某些操作。也许你见过这样的:

http://example.com/somepage.php?key=value&anotherkey=anothervalue

http://example.com/somepage.php?key=value&anotherkey=anothervalue

That string behind the question mark (key=value&anotherkey=anothervalue) is the query string. PHP can do something with the values:

问号 ( key=value&anotherkey=anothervalue)后面的字符串是查询字符串。PHP 可以对这些值做一些事情:

<?php

echo $_GET['key']; // returns "value"
echo $_GET['anotherkey']; // returns "anothervalue"

?>

Now we can just do the same when creating an image. You don't have to make twenty PHP files with almost the same code, but just a single file which reads a file (you name it) and resizes it to the specified width (you name it) and height (you name it).

现在我们可以在创建图像时做同样的事情。您不必使用几乎相同的代码制作 20 个 PHP 文件,而只需一个文件,它读取文件(您命名)并将其调整为指定的宽度(您命名)和高度(您命名)。

thumbnail.php

缩略图.php

<?php

// Get some request parameters we're going to use.
// We're expecting the parameters below to exist.
$file = $_GET['filepath'];
$width = $_GET['width'];
$height = $_GET['height'];

// Now we're gonna create the image from the given file.
$src = imagecreatefromjpeg($file);
imagecreatetruecolor($width, $height);
// And the rest of the file reading and image creation.

header("Content-Type: image/jpeg");
imagejpeg($image);

?>

webpage.html

网页.html

<html>
    <body>
        <?php

        $width = 100;
        $height = 100;
        $files = read_some_directory_and_return_a_list_of_filenames();

        foreach ($files as $file) {
            // Echo an image tag in the HTML document;
            // use as image our thumbnail.php file and give it a query string:
            echo "<img src=\"thumbnail.php?width=".$width."&height=".$height."&filepath=".$file."\" alt=\"\" />";
        }

        ?>
    </body>
</html>

回答by AJ Richardson

What you've seen is correct - you can't have a file with Content-type: image/pngthat contains html contents. The browser will interpret the html code as encoded html data, which is wrong.

您所看到的是正确的 - 您不能拥有Content-type: image/png包含 html 内容的文件。浏览器会将 html 代码解释为编码的 html 数据,这是错误的。

What you should do is leave the content type as text/htmland send the image in an html document, as in my example below. I left out the <head>for simplicity, but you should add one.

您应该做的是保留内容类型text/html并将图像发送到 html 文档中,如下面的示例所示。<head>为简单起见,我省略了,但您应该添加一个。

<!DOCTYPE html>
<html>
<body>
    <img src="myimage.png" width="100" height="100" />
</body>
</html>

回答by Ryan Kempt

You cannot have markup inside your image you want to push down to the browser. You're telling the browser to expect an image and then sending down some markup, that's a no-no.

您不能在要推送到浏览器的图像中添加标记。你告诉浏览器期待一个图像,然后发送一些标记,这是一个禁忌。

What we can do is include a .php file which uses your imagecreatetruecolor, etc... functions inside some imgtags:

我们可以做的是imagecreatetruecolor在一些img标签中包含一个 .php 文件,该文件使用您的, etc... 函数:

<html>
<head><title>Image Test</title></head>
<body>
  <img src="image.php?file=A" />
  <img src="image.php?file=B" />
</body>
</html>

Then image.phpwould use our image/jpegheaders and contain your imagecreatefromjpegcode.

然后image.php将使用我们的image/jpeg标题并包含您的imagecreatefromjpeg代码。

<?
header('Content-Type: image/jpeg');
if ($_GET['file'] == 'A') {
    $img = imagecreatefromjpeg('image1.jpg');
} elseif ($_GET['file'] == 'B') {
    $img = imagecreatefromjpeg('image2.jpg');
}
// do your modification etc... here
imagejpeg($img);
imagedestroy($img);

You can only output "image code" in your image.phpfile because the browser is going to treat it as if it's a regular jpeg.

您只能在image.php文件中输出“图像代码”,因为浏览器会将其视为常规 jpeg。