显示使用imagecreatefromstring创建的图像 [英] Displaying an image created with imagecreatefromstring

查看:105
本文介绍了显示使用imagecreatefromstring创建的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我有看起来像这样的代码:

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?

谢谢.

推荐答案

我要替换什么/*这里的内容是什么? */可以显示我的图像数据吗?

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

您突出显示的位置是 img的所谓src属性. HTML标签 文档 .该值就是所谓的 URI Docs .

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

在您的情况下,您希望该URI指向相关的图像数据.您尚未指定图像输出的类型,因此在下面的示例中,我将假定它是PNG图像.

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.

您现在需要将图像数据转换为URI.从图像数据创建的最直接的URI是所谓的 data: URI 维基百科 :

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
//
?>

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

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.

除了使用data: URI之外,您还可以在其中放置任何其他URI,例如,指向要返回图像的服务器上的PHP脚本的HTTP URI.这样的脚本可能非常简单:

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);

这类似于 Marc B的建议,他的回答为好.

这篇关于显示使用imagecreatefromstring创建的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆