SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot
    Join Date
    Feb 2005
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Upload files to PHP/GD - keep filetype?

    Hi

    I was wondering if you could help me.

    I have a script where a user can upload a photo and it is saved into a database. Later, there is another script that allows you to load that photo out of the database and show it.

    The script to upload the photo basically uses fopen() to open the file from $_FILES['name']['tmp_name'] into a variable.

    Then GD uses imagecreatefromstring() to open the photo from that variable and resize it as necessary.

    Then using imagejpeg it is saved to the $_FILES['name']['tmp_name'].

    Then it is opened that using fopen(), and inserted into the MySQL database.

    This is the simplest way I could find to transfer between an upload, GD, and then to a database. Please feel free to let me know if there is a better way.

    Anyway, in the script later on to retrieve the photo and display it, I just take it from the database and output it using imagejpeg.

    However, this all only works with jpegs. I need it to work with PNG, JPEG, preferably GIF, and if possible BMP as well. How can I do this? Ideally I would like it to be converted to a single universal filetype (probably either PNG, JPEG or GIF) BEFORE being added to the database.

    Is this possible? I have been searching for a long time with no luck. If anyone could point me in the right direction I would be very grateful!

    Thanks very much,
    Shai

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    take a look at www.php.net/gd

    tons of functions, im sure you can find what you need.

    you can find the type of image by using getimagesize()
    you could have another field in your db to store the image type

    or you could just convert the image to the type of your choice.

    also, i dont think you need to resave the file to the filesystem before adding it to the db. theres probably a function to output the image data into a string if you dont want to write it to a file, or output to the browser. if you cant find one, just use output buffering

    PHP Code:
    ob_start();
    imagejpeg($resource);
    $image ob_get_contents();
    ob_end_clean();
    // now add $image to your db 

  3. #3
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Storing binary data in a database is not the most intellegent thing to do. Store files in the file system and data in the database.

    Also the idea of a universal file type is not very clever either. The file type should be chosen because it is best suited to each particular image.

  4. #4
    SitePoint Zealot
    Join Date
    Feb 2005
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why should I not store binary data in a database?

  5. #5
    SitePoint Addict rwar's Avatar
    Join Date
    Sep 2001
    Location
    PF / RS / BR
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why don't you save only the path to your image and upload it to a specific directory?
    php? mysql? apache? That's it.

  6. #6
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by shaiphp
    Why should I not store binary data in a database?
    That is a bit like asking why you shouldn't use a hammer to open a window. If you can't see how impractical it is I am not going to waste my time trying to convince you.

  7. #7
    SitePoint Zealot
    Join Date
    Feb 2005
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am a beginner with this side of PHP, and would really appreciate it if you could explain to me why not to store binary data in a database?

    Thanks!

  8. #8
    SitePoint Member
    Join Date
    Mar 2006
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    u mind sharing the script


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •