I
Iñaki Baz Castillo
Hi, the folowing code encodes and decodes a image file as Base64:
=2D Encode "icon.png" in Base64 as "base64.txt":
=2D-------------------------------------
File.open("base64.txt","w") do |file|
file.write [open("icon.png").read].pack("m")
end
=2D-------------------------------------
=2D Decode "base64.txt" as a PNG "new_icon.png" file:
=2D-------------------------------------
File.open('new_icon.png', 'wb') do |file|
file << (IO.readlines('base64.txt').to_s.unpack('m')).first
end
=2D-------------------------------------
It works perfectly under Ruby1.8 (after encoding and decoding "icon.png" is=
=20
exatcly the same as "new_icon.png", the same binay file).
However running under Ruby1.9 the result is different since "new_icon.png" =
is=20
a corrupted image file. When I try to open it with a image viewer I get thi=
s=20
error (under Linux):
libpng warning: gAMA: CRC error
libpng error: PNG unsigned integer out of range.
Which is the difference when using Ruby1.9? how to solve it?
Thanks a lot.
=2D-=20
I=C3=B1aki Baz Castillo <[email protected]>
=2D Encode "icon.png" in Base64 as "base64.txt":
=2D-------------------------------------
File.open("base64.txt","w") do |file|
file.write [open("icon.png").read].pack("m")
end
=2D-------------------------------------
=2D Decode "base64.txt" as a PNG "new_icon.png" file:
=2D-------------------------------------
File.open('new_icon.png', 'wb') do |file|
file << (IO.readlines('base64.txt').to_s.unpack('m')).first
end
=2D-------------------------------------
It works perfectly under Ruby1.8 (after encoding and decoding "icon.png" is=
=20
exatcly the same as "new_icon.png", the same binay file).
However running under Ruby1.9 the result is different since "new_icon.png" =
is=20
a corrupted image file. When I try to open it with a image viewer I get thi=
s=20
error (under Linux):
libpng warning: gAMA: CRC error
libpng error: PNG unsigned integer out of range.
Which is the difference when using Ruby1.9? how to solve it?
Thanks a lot.
=2D-=20
I=C3=B1aki Baz Castillo <[email protected]>