This is a discussion on HOWTO: output a picture in the middle of a page within the PHP Language forums, part of the PHP Programming Forums category; I want to generate a web page using PHP, and in the middle generate a graphic I read from another ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
I want to generate a web page using PHP, and in the middle generate a
graphic I read from another server. So the graphic is in $picture. So I can either write $picture to a temp file and then use an ordinary <img src=filename> to get the browser to display it, or output it by itself in a new page, because then I have a chance to output the correct header so I don't just get garbage on the screen. Evidently first approach is poor because I have to write a local file, and second approach is not what I want anyway. Is there a way to do this? That is, I have $picture that contains an image, and I want to output it here and now in this HTML page I am in the middle of creating, and have it show as a picture. Thanks for any pointers. |
|
|||
|
"Tim Streater" <tim.streater@dante.org.uk> wrote in message
news:tim.streater-C7DF5E.17582804062004@individual.net... > I want to generate a web page using PHP, and in the middle generate a > graphic I read from another server. So the graphic is in $picture. So I > can either write $picture to a temp file and then use an ordinary <img > src=filename> to get the browser to display it, or output it by itself > in a new page, because then I have a chance to output the correct header > so I don't just get garbage on the screen. > > Evidently first approach is poor because I have to write a local file, > and second approach is not what I want anyway. > > Is there a way to do this? That is, I have $picture that contains an > image, and I want to output it here and now in this HTML page I am in > the middle of creating, and have it show as a picture. > > Thanks for any pointers. <?php // begin thispage.php if ($_GET['action']="img") { header("Content-type: image/jpeg"); // output image here } else { echo '<img src="thispage.php?action=img">'; } ?> |
|
|||
|
"kingofkolt" <jessepNOSPAM@comcast.net> wrote in message
news:952wc.50566$Ly.48896@attbi_s01... > "Tim Streater" <tim.streater@dante.org.uk> wrote in message > news:tim.streater-C7DF5E.17582804062004@individual.net... > > I want to generate a web page using PHP, and in the middle generate a > > graphic I read from another server. So the graphic is in $picture. So I > > can either write $picture to a temp file and then use an ordinary <img > > src=filename> to get the browser to display it, or output it by itself > > in a new page, because then I have a chance to output the correct header > > so I don't just get garbage on the screen. > > > > Evidently first approach is poor because I have to write a local file, > > and second approach is not what I want anyway. > > > > Is there a way to do this? That is, I have $picture that contains an > > image, and I want to output it here and now in this HTML page I am in > > the middle of creating, and have it show as a picture. > > > > Thanks for any pointers. > > <?php // begin thispage.php > if ($_GET['action']="img") { > header("Content-type: image/jpeg"); > // output image here > } else { > echo '<img src="thispage.php?action=img">'; > } > ?> > CORRECTION: change line 2 to: if ($_GET['action']=="image") { // I forgot the second '=' |
|
|||
|
Won't this complain that the header has already been sent assuming that
thispage.php is sending HTML before the image? I have been trying to find a solution around this too. kingofkolt wrote: > "kingofkolt" <jessepNOSPAM@comcast.net> wrote in message > news:952wc.50566$Ly.48896@attbi_s01... > >>"Tim Streater" <tim.streater@dante.org.uk> wrote in message >>news:tim.streater-C7DF5E.17582804062004@individual.net... >> >>>I want to generate a web page using PHP, and in the middle generate a >>>graphic I read from another server. So the graphic is in $picture. So I >>>can either write $picture to a temp file and then use an ordinary <img >>>src=filename> to get the browser to display it, or output it by itself >>>in a new page, because then I have a chance to output the correct header >>>so I don't just get garbage on the screen. >>> >>>Evidently first approach is poor because I have to write a local file, >>>and second approach is not what I want anyway. >>> >>>Is there a way to do this? That is, I have $picture that contains an >>>image, and I want to output it here and now in this HTML page I am in >>>the middle of creating, and have it show as a picture. >>> >>>Thanks for any pointers. >> >><?php // begin thispage.php >>if ($_GET['action']="img") { >> header("Content-type: image/jpeg"); >> // output image here >>} else { >> echo '<img src="thispage.php?action=img">'; >>} >>?> >> > > CORRECTION: > > change line 2 to: > > if ($_GET['action']=="image") { // I forgot the second '=' > > |
|
|||
|
> Won't this complain that the header has already been sent assuming that
> thispage.php is sending HTML before the image? I have been trying to > find a solution around this too. [snip] > >>> > >>>Thanks for any pointers. > >> > >><?php // begin thispage.php > >>if ($_GET['action']="img") { > >> header("Content-type: image/jpeg"); > >> // output image here > >>} else { > >> echo '<img src="thispage.php?action=img">'; > >>} > >>?> > >> > > > > CORRECTION: > > > > change line 2 to: > > > > if ($_GET['action']=="image") { // I forgot the second '=' > > > > > Try to use separate script for image output like this: image.html <html><head><title>Image output</title></head><body><img src=image.php?imagename=imagename></body></html> image.php <? header("Content-type: image/gif"); readfile($_GET['imagename']); ?> |
|
|||
|
> Try to use separate script for image output like this: > image.html > <html><head><title>Image output</title></head><body><img > src=image.php?imagename=imagename></body></html> > > image.php > <? > header("Content-type: image/gif"); > readfile($_GET['imagename']); > ?> > > Yes, that is one way of doing it and is what I am doing now. However, there are limitations to it. First, myphpfile is a separate script and one has to pass the bits in $picture into it which is inefficient and perhaps not much better than creating a file. The second more serious drawback is when you want to display multiple pictures. The following will not work and will display the second picture twice. <img src="myphpfile.phtml"> $picture=<second picture> <img src="myphpfile.phtml"> Of course you can get by with something like the following after modifying the myphpfile script <img src="myphpfile.phtml"> $picture2=<second picture> <img src="myphpfile.phtml?num=2"> but this makes everything even more inefficient as you now have to pass more data from one script to another. It would be neat to find a way to display the image directly from the original script but I am not sure it can be done in php. |
|
|||
|
In article <E4adnTb887G7g1rdRVn-jw@comcast.com>,
Jin Mazumdar <mazumdar_REMOVE_@REMOVE.comcast.net> wrote: > > Try to use separate script for image output like this: > > image.html > > <html><head><title>Image output</title></head><body><img > > src=image.php?imagename=imagename></body></html> > > > > image.php > > <? > > header("Content-type: image/gif"); > > readfile($_GET['imagename']); > > ?> > > > > > > Yes, that is one way of doing it and is what I am doing now. However, > there are limitations to it. > > First, myphpfile is a separate script and one has to pass the bits in > $picture into it which is inefficient and perhaps not much better than > creating a file. > > The second more serious drawback is when you want to display multiple > pictures. The following will not work and will display the second > picture twice. > > <img src="myphpfile.phtml"> > > $picture=<second picture> > > <img src="myphpfile.phtml"> > > Of course you can get by with something like the following after > modifying the myphpfile script > > <img src="myphpfile.phtml"> > > $picture2=<second picture> > > <img src="myphpfile.phtml?num=2"> > > but this makes everything even more inefficient as you now have to pass > more data from one script to another. > > It would be neat to find a way to display the image directly from the > original script but I am not sure it can be done in php. In the end I did something like this too. As I was obtaining the picture in the second script, and just in effect passing a pointer to this script so it knew which picture to get, there was no extra overhead because that was work needing to be done anyway. Thanks for all feedback. --tim |