Want to render an image without saving it to disk using PHP GD libs

假如想象 提交于 2019-12-30 10:47:05

问题


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.


回答1:


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&param2=2">



回答2:


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.



来源:https://stackoverflow.com/questions/5239286/want-to-render-an-image-without-saving-it-to-disk-using-php-gd-libs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!