pcdf Posted May 8, 2009 Share Posted May 8, 2009 Hi, I don't think this can be done but I'll ask anyway. I've got a function that generates images according to different parameters; e.g. <? draw_img($param1,$param2,$param3) { // imagecreate. // draw the image // set headers // imagejpeg // imagedestroy } ?> What I'd really like to do is display the generated pictures directly from that function. By this I mean; <img scr="<? draw_img($param1,$param2,$param3); ?>"> I've tried that but unfortunately I get raw ascii code instead of the image. I know I can use a separate php file to draw the image and pass parameters to it but I'd rather avoid that if at all possible; e.g. <img scr="image.php?p1=param1&p2=param2&p3=param3"> I also know that I could write my images to files and then point to them but once again I'd really like to avoid that. Is there any way I can display the images I'm generating directly from my function? Whithout passing parameters to any other file, using cookies/session or writing the images to files. Looking forward to hear your thoughts. Best, Pat. PS: Hope my question makes sense! :-\ Link to comment https://forums.phpfreaks.com/topic/157346-output-image-via-function/ Share on other sites More sharing options...
dgoosens Posted May 8, 2009 Share Posted May 8, 2009 can't you just print or echo it ? Link to comment https://forums.phpfreaks.com/topic/157346-output-image-via-function/#findComment-829345 Share on other sites More sharing options...
pcdf Posted May 8, 2009 Author Share Posted May 8, 2009 Do you mean; <img scr="<? echo draw_img($param1,$param2,$param3); ?>"> Tried that I'm afraid but all I got was the ascii representation of the image, not the actual image. Sample of the output I get: ЧŸÃÚL³K§Û¼’Ie3±I$•É$÷ŸøBü+ÿ�BÎÿ�€ñ4x/þDOØ2Ûÿ�E-nPü!~ÿ�¡gFÿ�À¿øš?áð¯ý:7þ�Eÿ�ÄÖå‡ÿ�_…èYÑ¿ð/þ&øBü+ÿ�BÎÿ�€ñ5¹E�aÿ�Âá_útoü�‹ÿ�‰£þ¿ ÿ�г£à_üMnQ@ð…øWþ… Anything I'm doing wrong? Link to comment https://forums.phpfreaks.com/topic/157346-output-image-via-function/#findComment-829352 Share on other sites More sharing options...
dgoosens Posted May 8, 2009 Share Posted May 8, 2009 you have to tell the browser it is a header... header('Content-Type: image/jpeg'); this will only work if there has been no output before you call the function here's more about that: http://be.php.net/manual/en/function.header.php Link to comment https://forums.phpfreaks.com/topic/157346-output-image-via-function/#findComment-829358 Share on other sites More sharing options...
thebadbad Posted May 8, 2009 Share Posted May 8, 2009 The only solution I can think of, is using the data URI scheme: <?php //storing image data in a string instead of outputting/saving a file ob_start(); //run imagejpeg() here (set to outputting the image data) $image_data = ob_get_contents(); ob_end_clean(); //convert image data to base64 $base64_data = base64_encode($image_data); echo '<img src="data:image/jpeg;base64,' . $base64_data . '" alt="" />'; ?> It's only supported by the newest browsers, and is by no means a method I would use personally. Link to comment https://forums.phpfreaks.com/topic/157346-output-image-via-function/#findComment-829361 Share on other sites More sharing options...
thebadbad Posted May 8, 2009 Share Posted May 8, 2009 you have to tell the browser it is a header... header('Content-Type: image/jpeg'); this will only work if there has been no output before you call the function here's more about that: http://be.php.net/manual/en/function.header.php He wants to mix HTML with the image data, so that's not going to work. Link to comment https://forums.phpfreaks.com/topic/157346-output-image-via-function/#findComment-829363 Share on other sites More sharing options...
Axeia Posted May 8, 2009 Share Posted May 8, 2009 IE (pre- supports it as well, but not in a pretty way.. think it had something to do with making it think the file is saved to disk using "save page". The way you want to tackle this problem is by passing the variables into the url of the image, where the baseurl is an image creating script (I used it this way for example to create a 'table' layout with divs where the background image (200px width for a column) got its color from $_GET['hex']. That way the stylesheet is still allowed full control over the looks of the page.. unlike with a static image) Link to comment https://forums.phpfreaks.com/topic/157346-output-image-via-function/#findComment-829365 Share on other sites More sharing options...
pcdf Posted May 8, 2009 Author Share Posted May 8, 2009 Thank you guys, you've unfortunately confirmed what I was thinking, i.e. can't be done that way. Thanks thebadbad, clever solution using data URI but I doubt I'll use it since it's not supported by older browsers. Link to comment https://forums.phpfreaks.com/topic/157346-output-image-via-function/#findComment-829366 Share on other sites More sharing options...
thebadbad Posted May 8, 2009 Share Posted May 8, 2009 Thank you guys, you've unfortunately confirmed what I was thinking, i.e. can't be done that way. Thanks thebadbad, clever solution using data URI but I doubt I'll use it since it's not supported by older browsers. Just tested it. It works in Firefox 3.0.10, but breaks even in IE8 (well, it works, but content is cut at 32KB, like it says on Wikipedia). So yeah, not a reliable solution Link to comment https://forums.phpfreaks.com/topic/157346-output-image-via-function/#findComment-829369 Share on other sites More sharing options...
pcdf Posted May 8, 2009 Author Share Posted May 8, 2009 Thanks again. Your help was much appreciated! :-) Link to comment https://forums.phpfreaks.com/topic/157346-output-image-via-function/#findComment-829371 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.