469,946 Members | 1,366 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,946 developers. It's quick & easy.

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.
Jul 17 '05 #1
6 4978
"Tim Streater" <ti**********@dante.org.uk> wrote in message
news:ti********************************@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">';
}
?>
Jul 17 '05 #2
"kingofkolt" <je**********@comcast.net> wrote in message
news:952wc.50566$Ly.48896@attbi_s01...
"Tim Streater" <ti**********@dante.org.uk> wrote in message
news:ti********************************@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 '='
Jul 17 '05 #3
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" <je**********@comcast.net> wrote in message
news:952wc.50566$Ly.48896@attbi_s01...
"Tim Streater" <ti**********@dante.org.uk> wrote in message
news:ti********************************@individu al.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 '='


Jul 17 '05 #4
> 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']);
?>
Jul 17 '05 #5
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.

Jul 17 '05 #6
In article <E4********************@comcast.com>,
Jin Mazumdar <ma**************@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
Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Martin | last post: by
4 posts views Thread by Michael T. Peterson | last post: by
3 posts views Thread by Kamyk | last post: by
2 posts views Thread by Casey Miller | last post: by
8 posts views Thread by mosscliffe | last post: by
reply views Thread by solostation | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.