Can't encode base64 for a jpg file

Discussion in 'Ruby' started by Eddy Velasquez, Sep 24, 2010.

  1. Hi everyone, I need some assistance, I'm trying to send a jpg file via
    http post to a page in PHP, the file is sent in base64 encode, so the
    page can convert it and save it to a directory in the server.

    I have been able to acomplish this using Java and Python, but for some
    reason I dont know, when I send via Ruby, the file is saved in the
    server but its not a valid image file. I have tried the following
    methods:

    Method 1 (reading each char)
    file64 = ''
    File.open(filename,"rb") do |file|
    file.each_char{|ch| file64+=ch }
    end
    encoded= [file64].pack('m')

    Method 2 (reading the whole file and packing it)
    encoded = [open(filename, "rb").read].pack('m')

    Method 3 (reading the whole file and using Base64)
    file64 = open(filename, "rb").read
    encoded = Base64::encode64(file64)

    Also, have tried to implement the script in this URL,
    http://en.wikibooks.org/wiki/Algorithm_Implementation/Miscellaneous/Base64#Ruby_version
    but I got the same result that the others.

    Am I missing something? Is there a special way to do this? There is
    something I noticed, the lines of the encoded file, are 60 chars long,
    and in python and java are 75 long.

    Thanks in advance.
    --
    Posted via http://www.ruby-forum.com/.
    Eddy Velasquez, Sep 24, 2010
    #1
    1. Advertising

  2. Hi,

    I tried it out of curiosity and it worked with pack and encodet64 as
    well. This is ruby 1.9.2p0 on Ubuntu 10.04 64. I was able to use the
    tool "base64 -d" to decode the image data properly and view in gimp, as
    well undecode in PHP.

    $ cat test.png | ruby -e 'puts [STDIN.read].pack "m"' | php -r 'echo
    base64_decode(file_get_contents("php://stdin"));' >test2.png
    $ md5sum test.png test2.png
    f7fa630a147264f328e8b878ce7b39d9 test.png
    f7fa630a147264f328e8b878ce7b39d9 test2.png

    What version of ruby on which platform are you using?

    - Markus
    Markus Fischer, Sep 24, 2010
    #2
    1. Advertising

  3. Thanks Markus, I also tried to encode and decode from Ruby and it works
    there, but when I send the encoded file to the PHP page, it somehow save
    a different file, my development environment is a Windows XP and the
    page is on a MacOs X Server, its on the same network that I'm.

    When I posted via http post with java (using BASE64Encoder().encode from
    package sun.misc.BASE64Encoder) the file size is 107,602 bytes, but from
    ruby using the methods I listed before its 105,796 bytes.

    the version of ruby i'm using is ruby 1.8.7 (2010-08-16 patchlevel 302)
    [i386-mingw32]
    --
    Posted via http://www.ruby-forum.com/.
    Eddy Velasquez, Sep 24, 2010
    #3
  4. Hi,

    I tested my code also with 1.8.7 -> works. However I don't have that
    version on Windows, I tested on Ubuntu.

    I'm not sure, I'd try to start small. E.g. the code I pasted uses ruby &
    PHP to encode/decode the file and it matches. Did you verify what the
    plain outcome of base64 encode is from ruby and compared that to the
    Java/Python version? Did they match?

    Because if they match, maybe the problem is the HTTP POST transportation
    (just a guess).

    HTH
    Markus Fischer, Sep 24, 2010
    #4
  5. Yes you were right the problem was on the HTTP POST transportation, i
    also needed to escape the data with URI.escape, it solved.

    Again thanks a lot for your help.

    --
    Posted via http://www.ruby-forum.com/.
    Eddy Velasquez, Sep 24, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roland
    Replies:
    0
    Views:
    1,052
    Roland
    May 12, 2004
  2. Damir Hakimov

    base64.encode and decode not correct

    Damir Hakimov, Aug 16, 2005, in forum: Python
    Replies:
    1
    Views:
    348
  3. Replies:
    0
    Views:
    552
  4. aaragon
    Replies:
    7
    Views:
    2,321
    Paul Brettschneider
    Mar 18, 2008
  5. Cyril.Liu
    Replies:
    3
    Views:
    445
    Clifford Heath
    Aug 20, 2010
Loading...

Share This Page