Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/152231-solved-generated-images-within-div-tag/
Share on other sites

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

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

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

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?

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

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.