HOWTO: output a picture in the middle of a page | | |
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. | | | | re: HOWTO: output a picture in the middle of a page
"Tim Streater" <tim.streater@dante.org.uk> wrote in message
news:tim.streater-C7DF5E.17582804062004@individual.net...[color=blue]
> 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.[/color]
<?php // begin thispage.php
if ($_GET['action']="img") {
header("Content-type: image/jpeg");
// output image here
} else {
echo '<img src="thispage.php?action=img">';
}
?> | | | | re: HOWTO: output a picture in the middle of a page
"kingofkolt" <jessepNOSPAM@comcast.net> wrote in message
news:952wc.50566$Ly.48896@attbi_s01...[color=blue]
> "Tim Streater" <tim.streater@dante.org.uk> wrote in message
> news:tim.streater-C7DF5E.17582804062004@individual.net...[color=green]
> > 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.[/color]
>
> <?php // begin thispage.php
> if ($_GET['action']="img") {
> header("Content-type: image/jpeg");
> // output image here
> } else {
> echo '<img src="thispage.php?action=img">';
> }
> ?>
>[/color]
CORRECTION:
change line 2 to:
if ($_GET['action']=="image") { // I forgot the second '=' | | | | re: HOWTO: output a picture in the middle of a page
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:[color=blue]
> "kingofkolt" <jessepNOSPAM@comcast.net> wrote in message
> news:952wc.50566$Ly.48896@attbi_s01...
>[color=green]
>>"Tim Streater" <tim.streater@dante.org.uk> wrote in message
>>news:tim.streater-C7DF5E.17582804062004@individual.net...
>>[color=darkred]
>>>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.[/color]
>>
>><?php // begin thispage.php
>>if ($_GET['action']="img") {
>> header("Content-type: image/jpeg");
>> // output image here
>>} else {
>> echo '<img src="thispage.php?action=img">';
>>}
>>?>
>>[/color]
>
> CORRECTION:
>
> change line 2 to:
>
> if ($_GET['action']=="image") { // I forgot the second '='
>
>[/color] | | | | re: HOWTO: output a picture in the middle of a page
> Won't this complain that the header has already been sent assuming that[color=blue]
> thispage.php is sending HTML before the image? I have been trying to
> find a solution around this too.[/color]
[snip][color=blue][color=green][color=darkred]
> >>>
> >>>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">';
> >>}
> >>?>
> >>[/color]
> >
> > CORRECTION:
> >
> > change line 2 to:
> >
> > if ($_GET['action']=="image") { // I forgot the second '='
> >
> >[/color]
>[/color]
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']);
?> | | | | re: HOWTO: output a picture in the middle of a page
[color=blue]
> 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']);
> ?>
>
>[/color]
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. | | | | re: HOWTO: output a picture in the middle of a page
In article <E4adnTb887G7g1rdRVn-jw@comcast.com>,
Jin Mazumdar <mazumdar_REMOVE_@REMOVE.comcast.net> wrote:
[color=blue][color=green]
> > 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']);
> > ?>
> >
> >[/color]
>
> 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.[/color]
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 |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,449 network members.
|