# SOAP::SOAPBase64 (byte array) to Image (JPG/GIF)

**URL:** <https://www.ruby-forum.com/t/soap-soapbase64-byte-array-to-image-jpg-gif/140488>\
**Category:** Ruby\
**Created:** [June 14, 2008, 9:48pm UTC](https://www.ruby-forum.com/t/soap-soapbase64-byte-array-to-image-jpg-gif/140488 "2008-06-14T21:48:12Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![shambolic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e36b37/32.png) [@shambolic](https://www.ruby-forum.com/u/shambolic)\
**Post date:** [June 14, 2008, 9:48pm UTC](https://www.ruby-forum.com/t/soap-soapbase64-byte-array-to-image-jpg-gif/140488/1 "2008-06-14T21:48:12Z")

</div>

Hello,

Attempting to convert and expand on an old Java project. It pulls from  
a SOAP project created from a WSDL using wsdl2ruby. The portion I am  
having troubles with involves converting a “byte array” of image data  
(either JPEG or GIF) into a valid image file. The soap call says it  
returns an object of SOAP::SOAPBase64, but the documentation from the  
server says it is a compressed image file in a byte array format.

The following is the Java code, that I want to get Ruby to duplicate:

// --------- Begin Java Code ------------------  
import java.io.InputStream;  
import java.io.ByteArrayInputStream;  
import java.awt.image.BufferedImage;  
import javax.imageio.ImageIO;

byte[] pic\_bytes = getImageFromServer();  
ByteArrayInputStream bais = new ByteArrayInputStream(pic\_bytes);  
BufferedImage bi = ImageIO.read((InputStream) bais);  
ImageIO.write(bi, “JPEG”, file\_name);  
// --------- End Java Code -------------------

I can get “data” from the server, but when I attempt to save it to a  
file, it saves as characters (like ABCDEF). The Ruby version of  
“getImageFromServer()” returns an object of String. I have tried  
String.pack() to encode and decode from Base64, as well as other  
formats, and I have tried using the RMagick library to save the image,  
but the image “header” is not correct for a JPEG or GIF. I know the  
Java version works, so I would like to know what about a  
ByteArrayInputStream or BufferedImage makes the data become an image  
that I cannot get to work in Ruby?

Thanks,  
Jay
