Thread: reading a gif

    #1
  1. No Profile Picture
    beans-curry
    Guest
    Devshed Newbie (0 - 499 posts)
    hello everyone

    i want to read a gif and display it on the browser.the code i use is as follows..
    -------------------------------------------
    $fp = fopen("../images/Powered1.gif","rb");
    $size = filesize("../images/Powered1.gif");
    for($i=0 ; $i<$size ; ++$i)
    echo fgetc($fp);
    -------------------------------------------
    but the gif is not shown.well i guess in the binary mode when i use feof() it reads the end-of-file character before the end !!

    what should i do ??
    please help me out


  2. #2
  3. No Profile Picture
    Robert_J_Sherman
    Guest
    Devshed Newbie (0 - 499 posts)
    I guess I don't follow, why would you go that way with an image?

    If the image is on the server, then you only need your <img> tag. If it's in a database, then the approach is quite similar.. you still need the <img> tag..

    just echoing the code isn't going to display the image.. you'll still need the <img> tag.

    also, one thing to note about images, and such, is that the call for the file and it's header "must" come before the html header.

    otherwise, you'll get a nice little error telling you that a new header can't be defined... I forget the exact wording, but that's the gist of it.

    ------------------
    SnR Graphics,
    Low Cost Hosting and Web Development.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Posts
    2
    Rep Power
    0
    Hello,

    You going about the image all wrong, bless your heart!

    The fopen() function is designed to work with, and manipulate files on a file system, typically text files. Its usage is not intended to work with image files.

    It appears that your attempting to dump the contents of a gif image to a variable, and then echo that variable to browser. There are several reasons why this doesn't work.

    First of all, the data from the image file needs to let the browser know what kinda image it is, or rather what mime type. The web server normally takes care of doing that for you.

    On a web server, a file is requested, and the web server is supposed to send a responce back telling the browser that it is about to get an html file(Content-Type: text/html), or a gif image(Content-Type: image/gif), etcetera, etc.. If the browser doens't get the content type info, it simply doesn't kwno what to do with the data, except the default, and that is displaying the data in text.

    All the http responce's, otherwise known as headers, must be sent before any data is sent to the browser. That means all the php files, html files, and images have a header sent to the browser telling the how to handle the data. The php, or html is interpreted by the client, and that is where the browser knows how to anrchor the <img> tag's in the various places you put them.

    So in sort, and simple words: YOu must first have your php tell the browser that yoru goign to send it a GIF image. Secondly, you must have php send the image.

    Here is where I show you the correct way to do part two: sending the image from php.

    Before I begin, I'd like to inform you that php4 has a built in way of handling various images in your php. PHP4 uses the GD library to create, open, manipulate, and send images. My example uses the GD library to correctly open a gif image on a filesystem, and send a gif image to your browser.

    Here goes:
    <php
    header ("Content-type: image/gif");
    $yourimg = ImageCreateFromGIF("smile.gif");
    ImageGif($yourimg);
    ImageDestroy($yourimg);
    ?>
    (this is quick and dirty. its not going to work with out more lines of code, but demonstrated the idea)

    The header() functin is used to tell php/apache to pretend that this is php is really an image format. $yourimage is actually not a variable, but rather a variable acting as a file pointer. Sorta like the fopen() function does. Anyways, the ImageCreateFromGIF() function is part of the GD library, and its very obvious what it does. ImageGif() does all the work of streaming the data to the browser, kinda like how you were attempting to do with fgetc(). The ImageDestroy() function simply dumps the output buffer from having just used ImageGif().

    In your output from the main html, php, you can have something like the following:
    <img scr="path/yourimage.php" alt="your image">.

    I hope that helps?

    Best regards,
    --Jon
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Location
    houston, tx
    Posts
    21
    Rep Power
    0
    Would there be a way to use jpeg or bitmap files, as well as GIF files with those PHP functions?
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    5
    Rep Power
    0

    outputting multiple gifs combined with text


    I have tried this header(content type: image/gif) at the top of my php page and it works beautifully

    However I find this very limited as it (in my case maybe) only seems to display 1 gif on the page - I need to display multiple images on the page in a search results table relating to text entries also and I am finding it difficult if not nearly impossible to pass headers beyond the top of the php page. I need header content to include multiple images and text.

    This is really exhausting all this imaging in php stuff, and would love a hand.

    Thanks
  10. #6
  11. Mobbing Gangster
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Sep 2001
    Location
    "Best City" 2002 and 2003- Melbourne, Australia
    Posts
    4,912
    Rep Power
    37
    Actually as simple as
    PHP Code:
    <?php
    $file
    ='blah.gif';
    $hd=fopen($file,'rb');
    fpassthru($hd);
    fclose($hd);
    ?>
    Will do just fine.

    jdisnard,
    >>The php, or html is interpreted by the client
    Actually client never gets to see php as it is server side

    >>I'd like to inform you that php4 has a built in way of handling various images in your php.
    It doesn't. GD is external module and should be installed separately. On windows, however, its as simple as uncommenting line in php.ini, but still it is not 'built in'.

    Your code for showing image with php is fine as long as he uses old version of gd, as new ones do not support gif.

    >>The header() functin is used to tell php/apache...
    Not quite, in this case header is for the client only.



    diversereality,
    >>Would there be a way to use jpeg or bitmap files, as well as GIF files with those PHP functions?
    jpeg yes, bitmap no edit: sure about jpeg, but not 100% about bitmap </edit>.


    ShelleyChambers,
    >>I find this very limited as it only seems to display 1 gif on the page
    jdisnard did a great job explaining major points. If you want several images, you will have to send several images and they should be separated. That means them as <img src=image.php?image=house> with one at the time.

    >> I am finding it difficult to pass headers beyond the top of the php page
    Just make sure you do not output anything. Or use buffering.

    >>I need header content to include multiple images and text.
    There is no such thing.
    And you know I mean that.

Similar Threads

  1. Convert Image GIF to JPG ??
    By daviddeveloper in forum PHP Development
    Replies: 2
    Last Post: February 15th, 2004, 10:45 PM
  2. how to change color on gif file?
    By msa969 in forum Web Design Help
    Replies: 1
    Last Post: December 2nd, 2003, 09:45 PM
  3. file upload only 1 format either jpg or gif
    By phprock in forum PHP Development
    Replies: 4
    Last Post: November 29th, 2003, 02:57 PM
  4. reading dir for GIF files / storing names in array
    By can't-C-through in forum C Programming
    Replies: 2
    Last Post: October 20th, 2003, 12:38 AM
  5. Replies: 4
    Last Post: June 11th, 2003, 04:46 PM

IMN logo majestic logo threadwatch logo seochat tools logo