HOWTO: output a picture in the middle of a page

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tim Streater

    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.
  • kingofkolt

    #2
    Re: HOWTO: output a picture in the middle of a page

    "Tim Streater" <tim.streater@d ante.org.uk> wrote in message
    news:tim.streat er-C7DF5E.17582804 062004@individu al.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.p hp?action=img"> ';
    }
    ?>


    Comment

    • kingofkolt

      #3
      Re: HOWTO: output a picture in the middle of a page

      "kingofkolt " <jessepNOSPAM@c omcast.net> wrote in message
      news:952wc.5056 6$Ly.48896@attb i_s01...[color=blue]
      > "Tim Streater" <tim.streater@d ante.org.uk> wrote in message
      > news:tim.streat er-C7DF5E.17582804 062004@individu al.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.p hp?action=img"> ';
      > }
      > ?>
      >[/color]

      CORRECTION:

      change line 2 to:

      if ($_GET['action']=="image") { // I forgot the second '='


      Comment

      • Jin Mazumdar

        #4
        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@c omcast.net> wrote in message
        > news:952wc.5056 6$Ly.48896@attb i_s01...
        >[color=green]
        >>"Tim Streater" <tim.streater@d ante.org.uk> wrote in message
        >>news:tim.stre ater-C7DF5E.17582804 062004@individu al.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.p hp?action=img"> ';
        >>}
        >>?>
        >>[/color]
        >
        > CORRECTION:
        >
        > change line 2 to:
        >
        > if ($_GET['action']=="image") { // I forgot the second '='
        >
        >[/color]

        Comment

        • Dennis Biletsky

          #5
          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.p hp?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><ti tle>Image output</title></head><body><img
          src=image.php?i magename=imagen ame></body></html>

          image.php
          <?
          header("Content-type: image/gif");
          readfile($_GET['imagename']);
          ?>


          Comment

          • Jin Mazumdar

            #6
            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><ti tle>Image output</title></head><body><img
            > src=image.php?i magename=imagen ame></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=<secon d 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=<seco nd 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.

            Comment

            • Tim Streater

              #7
              Re: HOWTO: output a picture in the middle of a page

              In article <E4adnTb887G7g1 rdRVn-jw@comcast.com> ,
              Jin Mazumdar <mazumdar_REMOV E_@REMOVE.comca st.net> wrote:
              [color=blue][color=green]
              > > Try to use separate script for image output like this:
              > > image.html
              > > <html><head><ti tle>Image output</title></head><body><img
              > > src=image.php?i magename=imagen ame></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=<secon d 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=<seco nd 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

              Comment

              Working...