Trouble Using a PHP Script to Send an Image

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael J. Astrauskas

    Trouble Using a PHP Script to Send an Image

    I have a script, which I've called test-loadpic.php and some pages
    reference it by means of

    <img src="test-loadpic.php?sou rcepic=$picNum" >

    where $picNum stores a number. This part itself works fine.
    test-loadpic.php uses the "sourcepic" GET variable to reference a file
    on disk, get it's file/MIME type, transmit the headers and then the raw
    image. The important code is below:

    header ("Content-type: $fileMIME");
    header ("Content-Length: ".filesize($fil ePath.$fileName ));

    $fp = fopen ($filePath.$fil eName, "rb");
    $er = fpassthru ($fp);
    fclose($fp);

    $fileMIME is the MIME type of the image, of course ("image/gif" or
    "image/jpeg" in my case). $filePath and $fileName point to the image
    (ex: "/var/www/phptest/images/" and "filter.jpg ").

    Is this all I need to properly transmit an image?

    This seems to work fine in IE but sometimes Netscape 7.1 tries to
    download the image instead of showing it. Are my headers incorrect? I
    suspect there's a glitch in my scripting that IE can tolerate but
    Netscape can't. Is there a proper "end of image" code I need to send to
    the browser that isn't included at the end of the images themselves?

    Thank you in advance to anyone with some insight.

    --
    - Michael J. Astrauskas

  • Shane Lahey

    #2
    Re: Trouble Using a PHP Script to Send an Image - showimage.php (0/1)

    yes should work fine...

    you could try this function to display the image....
    Usage: showImage('/path/to/image.jpg');


    <?php

    function showimage($path )
    {
    if (!file_exists($ path))
    return false;

    $filesize = filesize($path) ;
    $basename = basename($path) ;

    $info = @getimagesize($ path);
    if (!is_array($inf o))
    return false; // invalid image

    switch ($info[2])
    {
    case 1:
    $contentType = 'image/gif';
    break;
    case 2:
    $contentType = 'image/jpeg';
    break;
    case 3:
    $contentType = 'image/png';
    break;
    default:
    return false; // unsupported
    break;
    }

    $fd = @fopen($path, 'rb');
    if (!is_resource($ fd))
    return false; // could not read file.

    $contents = '';
    while ($data = fread($fd, 4096))
    {
    if (strlen($data) == 0)
    break; // EOF
    $contents .= $data;
    }
    fclose($fd);

    Header('Content-type: ' . $contentType);
    Header('Content-length: ' . $filesize);
    Header('Content-Disposition: inline; filename='.$bas ename);
    echo $contents;
    return true;
    }

    ?>


    On Tue, 25 May 2004 10:42:31 -0700, "Michael J. Astrauskas"
    <trevie@nospam. cox.net> wrote:
    [color=blue]
    >I have a script, which I've called test-loadpic.php and some pages
    >reference it by means of
    >
    > <img src="test-loadpic.php?sou rcepic=$picNum" >
    >
    >where $picNum stores a number. This part itself works fine.
    >test-loadpic.php uses the "sourcepic" GET variable to reference a file
    >on disk, get it's file/MIME type, transmit the headers and then the raw
    >image. The important code is below:
    >
    > header ("Content-type: $fileMIME");
    > header ("Content-Length: ".filesize($fil ePath.$fileName ));
    >
    > $fp = fopen ($filePath.$fil eName, "rb");
    > $er = fpassthru ($fp);
    > fclose($fp);
    >
    >$fileMIME is the MIME type of the image, of course ("image/gif" or
    >"image/jpeg" in my case). $filePath and $fileName point to the image
    >(ex: "/var/www/phptest/images/" and "filter.jpg ").
    >
    >Is this all I need to properly transmit an image?
    >
    >This seems to work fine in IE but sometimes Netscape 7.1 tries to
    >download the image instead of showing it. Are my headers incorrect? I
    >suspect there's a glitch in my scripting that IE can tolerate but
    >Netscape can't. Is there a proper "end of image" code I need to send to
    >the browser that isn't included at the end of the images themselves?
    >
    >Thank you in advance to anyone with some insight.[/color]

    Comment

    • Tim Van Wassenhove

      #3
      Re: Trouble Using a PHP Script to Send an Image - showimage.php (0/1)

      In article <rl17b0l66l1u9b egnbur1nb4nvajo j26k7@4ax.com>, Shane Lahey wrote:[color=blue]
      ><?php
      >
      > function showimage($path )
      > {
      > if (!file_exists($ path))
      > return false;[/color]

      Assuming the $path is in public space

      header('Locatio n: http://url/to/image');

      Done ;)

      --
      Tim Van Wassenhove <http://home.mysth.be/~timvw/contact.php>

      Comment

      • Michael J. Astrauskas

        #4
        Re: Trouble Using a PHP Script to Send an Image - showimage.php(0/1)

        Shane Lahey wrote:
        [color=blue]
        > $contents = '';
        > while ($data = fread($fd, 4096))
        > {
        > if (strlen($data) == 0)
        > break; // EOF
        > $contents .= $data;
        > }
        > fclose($fd);[/color]

        What is the purpose of this loop?

        --
        - Michael J. Astrauskas

        Comment

        • Shane Lahey

          #5
          Re: Trouble Using a PHP Script to Send an Image - showimage.php (0/1)

          On Tue, 25 May 2004 22:48:42 -0700, "Michael J. Astrauskas"
          <trevie@nospam. cox.net> wrote:
          [color=blue]
          >Shane Lahey wrote:
          >[color=green]
          >> $contents = '';
          >> while ($data = fread($fd, 4096))
          >> {
          >> if (strlen($data) == 0)
          >> break; // EOF
          >> $contents .= $data;
          >> }
          >> fclose($fd);[/color]
          >
          >What is the purpose of this loop?[/color]

          binary safe way to read the contents of a file into a variable.......

          Comment

          • Dennis Biletsky

            #6
            Re: Trouble Using a PHP Script to Send an Image - showimage.php (0/1)


            "Shane Lahey" <s.lahey@roadru nner.nf.net>
            news:rl17b0l66l 1u9begnbur1nb4n vajoj26k7@4ax.c om...[color=blue]
            > yes should work fine...
            >[/color]
            [skip][color=blue]
            > $fd = @fopen($path, 'rb');
            > if (!is_resource($ fd))
            > return false; // could not read file.
            >
            > $contents = '';
            > while ($data = fread($fd, 4096))
            > {
            > if (strlen($data) == 0)
            > break; // EOF
            > $contents .= $data;
            > }
            > fclose($fd);
            >
            > Header('Content-type: ' . $contentType);
            > Header('Content-length: ' . $filesize);
            > Header('Content-Disposition: inline; filename='.$bas ename);
            > echo $contents;
            > return true;
            > }
            >
            > ?>[/color]

            Header('Content-type: ' . $contentType);
            readfile($path) ;
            I'm using a little bit shorter script. Would you like to compare with yours?
            I'm interesting in your opinion and others opinion too.


            Comment

            • Shane Lahey

              #7
              Re: Trouble Using a PHP Script to Send an Image - showimage.php (0/1)

              heh, sorry I don't actually have one, I came up with this off the top
              of my head :/

              On Wed, 26 May 2004 10:19:46 +0300, "Dennis Biletsky" <ufafa@ua.fm>
              wrote:
              [color=blue]
              >
              >"Shane Lahey" <s.lahey@roadru nner.nf.net>
              >news:rl17b0l66 l1u9begnbur1nb4 nvajoj26k7@4ax. com...[color=green]
              >> yes should work fine...
              >>[/color]
              >[skip][color=green]
              >> $fd = @fopen($path, 'rb');
              >> if (!is_resource($ fd))
              >> return false; // could not read file.
              >>
              >> $contents = '';
              >> while ($data = fread($fd, 4096))
              >> {
              >> if (strlen($data) == 0)
              >> break; // EOF
              >> $contents .= $data;
              >> }
              >> fclose($fd);
              >>
              >> Header('Content-type: ' . $contentType);
              >> Header('Content-length: ' . $filesize);
              >> Header('Content-Disposition: inline; filename='.$bas ename);
              >> echo $contents;
              >> return true;
              >> }
              >>
              >> ?>[/color]
              >
              >Header('Conten t-type: ' . $contentType);
              >readfile($path );
              >I'm using a little bit shorter script. Would you like to compare with yours?
              >I'm interesting in your opinion and others opinion too.
              >[/color]

              Comment

              • Michael J. Astrauskas

                #8
                Re: Trouble Using a PHP Script to Send an Image - showimage.php(0/1)

                Tim Van Wassenhove wrote:
                [color=blue]
                > Shane Lahey wrote:
                >[color=green]
                >> <?php
                >>
                >> function showimage($path )
                >> {
                >> if (!file_exists($ path))
                >> return false;[/color]
                >
                > Assuming the $path is in public space
                >
                > header('Locatio n: http://url/to/image');
                >
                > Done ;)[/color]

                This is a good idea, and I'll keep it in mind, but I'm trying to hide
                file names from users.

                --
                - Michael J. Astrauskas

                Comment

                Working...