www.webdeveloper.com
Results 1 to 6 of 6

Thread: how to convert the image file into binary

  1. #1
    Join Date
    May 2010
    Location
    Manila, Philippines
    Posts
    189

    how to convert the image file into binary

    Hi guys,

    I'm wondering how would you convert the exact file into binary so that I can save that binary into the database. Any ideas?

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,918
    My first suggestion is to not save the image file in the database, as the file system itself is a more efficient way to access, well, files. (You can store data about the image in your DB, including directory and file name info so you know where to get it.)

    If you are still determined to do so, then all you need to do is create a BLOB type column in your DB, and insert the contents into it (probably using file_get_contents() to grab the file contents).
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Oct 2012
    Posts
    17
    Try with below code to convert image file into binary.
    <script>
    var canvas = document.createElement("canvas");
    var ctx = canvas.getContext("2d");
    var img = new Image();
    img.onload = function () {
    canvas.width = img.width;
    canvas.height = img.height;
    ctx.drawImage(img, 0, 0);
    var data = canvas.toDataURL("image/jpeg");
    alert(data);
    };
    img.src = "http://localhost/MvcApplication3/test.png";
    </script>

  4. #4
    Join Date
    May 2010
    Location
    Manila, Philippines
    Posts
    189
    Thank you so much for replying guys.

    paulinetaylor85, im not quite sure how that works. do you have some php codes?

  5. #5
    Join Date
    Sep 2011
    Location
    Bristol, England, United Kingdom
    Posts
    192
    You can use the GB library for PHP to do this, take a look at the imagecreatefromstring function.

  6. #6
    Join Date
    Sep 2012
    Posts
    17
    Thank you for your replies guys,i was interested by the thread.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles