想要使用 PHP GD 库渲染图像而不将其保存到磁盘

发布于 2024-10-20 16:50:09 字数 1059 浏览 1 评论 0原文

您好,有以下功能

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 技术交流群。

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

发布评论

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

评论(2

欢你一世 2024-10-27 16:50:09

您需要在单独的资源中渲染图像。没有好的方法可以在 HTML 文档中添加图像数据。 (任何想推荐 data: URI 的人:这是一个糟糕的主意。)

<img src="renderBusinessCard.php?param1=1¶m2=2">

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

<img src="renderBusinessCard.php?param1=1¶m2=2">
沙沙粒小 2024-10-27 16:50:09

如果 $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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文