显示使用 imagecreatefromstring 创建的图像

发布于 2024-11-28 07:16:38 字数 283 浏览 6 评论 0原文

假设我的代码看起来像这样:

<?PHP

//
//... stuff here
//

$im = imagecreatefromstring( $imageData );

echo "<img src=" . /* what goes here? */ . "alt=\"the image\" />";


//
// more stuff here
//
?>

我该替换什么/* 这里放什么? */ 这样我的图像数据就会显示?

谢谢。

Let's say I have the code that looks something like:

<?PHP

//
//... stuff here
//

$im = imagecreatefromstring( $imageData );

echo "<img src=" . /* what goes here? */ . "alt=\"the image\" />";


//
// more stuff here
//
?>

What do I replace /* what goes here? */ with so my image data will display?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦境 2024-12-05 07:16:38

我该替换什么 /* 这里放什么? */ 这样我的图像数据就会显示出来?

您突出显示的位置是 < 的所谓 src 属性代码>img HTML 标签文档。该值是所谓的 URIDocs

在您的情况下,您希望该 URI 指向有问题的图像数据。您尚未指定图像应输出为哪种类型,因此在以下示例中我将假设它是 PNG 图像。

您现在需要将图像数据转换为 URI。从图像数据创建的最直接的 URI 是所谓的 data: URIWikipedia

<?PHP

//
//... stuff here
//

$im = imagecreatefromstring( $imageData );

ob_start();
imagepng($img);
$png = ob_get_clean();
$uri = "data:image/png;base64," . base64_encode($png);

echo "<img src=" . $uri /* URI goes here */ . " alt=\"the image\" />";

//
// more stuff here
//
?>

即使这是最直接的方法,也不总是建议这样做,因为图像数据将与 HTML 一起返回到浏览器。如果图像很大,这通常被认为是一种开销。

您也可以在其中放置任何其他 URI,而不是使用 data: URI,例如指向服务器上返回图像的 PHP 脚本的 HTTP URI。这样的脚本可以非常简单:

<?php
$img = imagecreatefromstring($string);
header('Content-type: image/png');
imagepng($img);

这与 Marc B 建议的相当,也请参阅他的回答

What do I replace /* what goes here? */ with so my image data will display?

The location you highlighted is the so called src attribute of the img HTML-tagDocs. The value is a so called URIDocs.

In your case you want that URI to point to the image data in question. You have not specified which type the image should be output as, so I will assume it's a PNG image in the following example.

You now need to convert your image data into an URI. The most straight forward URI to create from the image data is a so called data: URIWikipedia:

<?PHP

//
//... stuff here
//

$im = imagecreatefromstring( $imageData );

ob_start();
imagepng($img);
$png = ob_get_clean();
$uri = "data:image/png;base64," . base64_encode($png);

echo "<img src=" . $uri /* URI goes here */ . " alt=\"the image\" />";

//
// more stuff here
//
?>

Even this is the most straight forward way, it is not always recommended to do so because the image data will be returned with the HTML to the browser. If the image is large, this is commonly considered an overhead.

Instead of using the data: URI you can place any other URI in there as well, for example a HTTP URI that is pointing to a PHP script on your server that is returning the image. Such a script can be very simple:

<?php
$img = imagecreatefromstring($string);
header('Content-type: image/png');
imagepng($img);

This is comparable to what Marc B suggested, see his answer as well.

梦途 2024-12-05 07:16:38
<?php

$img = imagecreatefromstring($string);

header('Content-type: image/jpeg');
imagejpeg($img);

应该就是你所需要的。按照原样使用图像标签执行此操作,您需要将图像输出到临时文件并将图像标签指向该文件(这会产生第二个 HTTP 请求),或者使用 数据网址

<?php

$img = imagecreatefromstring($string);

header('Content-type: image/jpeg');
imagejpeg($img);

should be all you need. Doing it with the image tag as you are, you'd need to output the image to a temporary file and point the image tag at that (which incurs a second HTTP request), or use a data url.

完美的未来在梦里 2024-12-05 07:16:38

我认为你可以做这样的事情......

$src = "data:image/gif;base64," . $imageData ;
echo "<img src=\"$src\" alt=\"the image\" />";

I think you can do something like this...

$src = "data:image/gif;base64," . $imageData ;
echo "<img src=\"$src\" alt=\"the image\" />";
贪恋 2024-12-05 07:16:38

您必须首先将资源保存到文件中,或者使用类似 的内容将其输出imagepng() 在一个单独的请求中。

有关详细信息,请参阅 imagecreatefromstring() 文档。

如果您想使用数据 URI 方案,您可以尝试以下操作:

<?php

// If your image is binary data. use `base64_encode($imageData)`.
$imageData = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
           . 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
           . 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
           . '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';

echo '<img src="data:image/png;base64,'. $imageData .'" />';

You have to save the resource to a file first or output it using something like imagepng() in a separate request.

See imagecreatefromstring() documentation for more information.

If you want to use a Data URI scheme, you can try this instead:

<?php

// If your image is binary data. use `base64_encode($imageData)`.
$imageData = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
           . 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
           . 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
           . '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';

echo '<img src="data:image/png;base64,'. $imageData .'" />';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文