想要使用 PHP GD 库渲染图像而不将其保存到磁盘
您好,有以下功能
function renderBusinessCard($details){
//Getting the template for the business card
$filename = Templates::model()->getTemplateFileName($details['BusinessCards']['dp_id']);
header("Content-type: image/jpeg");
$image = $_SERVER['DOCUMENT_ROOT'].'resources/templates/'.$filename;
// header("Content-Type: image/jpeg");
//Getting the width and height of the image
list($width,$height) = getimagesize($image);
//echo $width;die;
//Creating a copy of a loaded image
$create = imagecreatefromjpeg($image);
//Creating a blank template to work from
$template = imagecreatetruecolor($width,$height);
imagecopyresized($template, $create, 0, 0, 0, 0, $width, $height, $width, $height);
imagejpeg($template, null, 100);
}
我想要实现的是在图像标签中显示图像,如下所示
<img src="<?php renderBusinessCard($details); ?>"
,但不知何故它总是在屏幕上呈现乱码,而不是我想要的结果。这有可能吗?以及如何在不使用单独的文件的情况下,将其保留在函数或方法中。
Hi have the following function
function renderBusinessCard($details){
//Getting the template for the business card
$filename = Templates::model()->getTemplateFileName($details['BusinessCards']['dp_id']);
header("Content-type: image/jpeg");
$image = $_SERVER['DOCUMENT_ROOT'].'resources/templates/'.$filename;
// header("Content-Type: image/jpeg");
//Getting the width and height of the image
list($width,$height) = getimagesize($image);
//echo $width;die;
//Creating a copy of a loaded image
$create = imagecreatefromjpeg($image);
//Creating a blank template to work from
$template = imagecreatetruecolor($width,$height);
imagecopyresized($template, $create, 0, 0, 0, 0, $width, $height, $width, $height);
imagejpeg($template, null, 100);
}
What I want to achieve is to display the image within an image tag like the following
<img src="<?php renderBusinessCard($details); ?>"
but somehow it's always rendering garbled code to the screen and not the result I want. Is this at all possible? And how without using a seperate file, I want this to remain within a function or method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在单独的资源中渲染图像。没有好的方法可以在 HTML 文档中添加图像数据。 (任何想推荐
data:
URI 的人:这是一个糟糕的主意。)You need to render the image in a separate resource. There is no good way of adding the image data inside the HTML document. (Anybody thinking of recommending
data:
URIs: It's a terrible idea.)如果 $details 数组不太大,最好将其保留在会话数据中。
然后调用将调用脚本并关联正确的详细数据。
It may be better to keep the $details array in session data if it's not too big.
Then calling will call the script and associate the correct details data.