debug Posted April 2, 2009 Share Posted April 2, 2009 Hi- Yesterday I wrote a quick script to generate an image using the GD graphics library and it works great however now when I try to put the code into a Div tag I'm getting a error stating that the header has already been set and then it churns out a load of garbage (which I assume is the png file in its raw format). any ideas how I could embed my generated image into a div tag? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/152231-solved-generated-images-within-div-tag/ Share on other sites More sharing options...
MadTechie Posted April 2, 2009 Share Posted April 2, 2009 Okay, the script is creating the png file (in its raw format) and what your doing is the same as opening the png file in a text editor and dumping it contents in a HTML page, this fails (as your seeing) To solved this you need to make the php appear as a png file.. to do this you use header() which you seam to be doing.. BUT what you need to do is call that php script as it is was a image So if my php script is called "createPNG.php" you put this in your HTML output like this <img src="createPNG.php"> So simply put create a file that is only used outputting the PNG raw data with the header set and then call that VIA the html file.. if thats confused you then please post what you have Quote Link to comment https://forums.phpfreaks.com/topic/152231-solved-generated-images-within-div-tag/#findComment-799400 Share on other sites More sharing options...
debug Posted April 2, 2009 Author Share Posted April 2, 2009 Thanks! that worked first time! but for some reason everytime I refresh the page the image fails to update (its a generative image that should change with every refresh). heres the code I'm using: // this is within the index.php file <div id="backgroundImage"><img src="generative.php"></div> --- // this is within the generative file (its just the end of the code as its a big file): header('Content-Type: image/png'); imagepng($handleBG); imagedestroy($handleBG); ?> Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/152231-solved-generated-images-within-div-tag/#findComment-799415 Share on other sites More sharing options...
MadTechie Posted April 2, 2009 Share Posted April 2, 2009 its probably due to cache add the following to the image script header( 'Expires: Sat, 26 Jul 1997 05:00:00 GMT' ); //passed date header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' ); header( 'Cache-Control: no-store, no-cache, must-revalidate' ); header( 'Cache-Control: post-check=0, pre-check=0', false ); header( 'Pragma: no-cache' ); if that failed then the old fall back is <div id="backgroundImage"><img src="generative.php?<php echo time();?>"></div> this make the browser thing its a different file thus forcing a reload of it Quote Link to comment https://forums.phpfreaks.com/topic/152231-solved-generated-images-within-div-tag/#findComment-799440 Share on other sites More sharing options...
debug Posted April 2, 2009 Author Share Posted April 2, 2009 I've tried both methods and the first didn't make any difference whatsoever and the second did appear to be attempting to load a new image (the page took longer to load) however it just returned a '?' swatch so I'm guessing it can't find the file. Heres the link: http://www.reality-debug.co.uk/index_new.php Do you want me to upload the code? Quote Link to comment https://forums.phpfreaks.com/topic/152231-solved-generated-images-within-div-tag/#findComment-799451 Share on other sites More sharing options...
MadTechie Posted April 2, 2009 Share Posted April 2, 2009 It appears your echoing that line so change form (i assume the line looks like this) echo '<div id="backgroundImage"><img src="generative.php?<php echo time();?>"></div>'; to echo '<div id="backgroundImage"><img src="generative.php?'.time().'"></div>'; EDIT: also the generative.php? has errors can you post the first 10 lines Quote Link to comment https://forums.phpfreaks.com/topic/152231-solved-generated-images-within-div-tag/#findComment-799472 Share on other sites More sharing options...
debug Posted April 2, 2009 Author Share Posted April 2, 2009 Hi- I've uploaded the code here as its still acting weird and spitting some weird errors: www.reality-debug.co.uk/generativeBG.zip Thanks for this! please excuse all badly named variables (its a habit of mine). Quote Link to comment https://forums.phpfreaks.com/topic/152231-solved-generated-images-within-div-tag/#findComment-799478 Share on other sites More sharing options...
MadTechie Posted April 2, 2009 Share Posted April 2, 2009 Okay the problem is mainly due to the 3 extra spaces at the stop of the file before the <? heres a working version i also changed how you get the random item from the array [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/152231-solved-generated-images-within-div-tag/#findComment-799521 Share on other sites More sharing options...
debug Posted April 2, 2009 Author Share Posted April 2, 2009 Thanks so so much I didn't know about the array_rand function either, so thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/152231-solved-generated-images-within-div-tag/#findComment-799533 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.