Extracting data from net ldap response containing image - ruby

In my application I sync users image uploaded as photo field in the LDAP, I am using NET::LDAP for the same.
the object returned for the image field is of type Net::BER::BerIdentifiedArray, I can convert it to Net::BER.
My question is how do I extract the type of image(jpeg/gif/bmp etc) while creating a image file from the binary response given by the LDAP.

You'll need something to decode the binary data into an image. RMagick is a Ruby wrapper for ImageMagick that should do the trick. Specifically, Image.from_blob will read image data from a string in memory.
From there, this answer shows image.format will tell you the format.

Related

Firebase functions callable - image as argument

I have a firebase function which given some arguments creates a "post" with a title and the such. Is there a way to send an image as an argument which can then be processed by firebase functions and then upload to firebase storage.
I think it is possible by decoding the file/image object to a base64 string which could be then sent as an argument. How would I convert the file object from html file input to base64? On the other side how would firebase functions know that the string is an actual image? How can I tell the size of the image? Is there a limit to the size of arguments given to a firebase callable function?
I ditched this question but came across the problem later in a different project. So to anyone seeing it is possible through base 64 strings. It has the restraints that the image cannot be bigger than 10mb but it is easier than trying to "catch" the file after it being uploaded directly.
Here is the solution
I would do the following:
Let the user upload the file directly into firebase storage in a folder where only the user has access to.
Let a function trigger on upload that takes this image, checks if the user is authorized to put that in the target folder and put the file in there (or whatever you want the function to do exactly).

Determine image size of file in aws s3 without downloading the image

I have some images (jpg, png) uploaded to aws s3 bucket. I want to extract some informations (lambda is written in golang) from the image (width and height). Is it possible to do this without downloading the image?
You can do a partial download of the object using the range header
See this SO answer S3: How to do a partial read / seek without downloading the complete file?
in the AWS go sdk func (Downloader) DownloadWithContext seems like it should provide range feature
Once you have the partial file it may be possible to extract the size information, see this answer What is the header size of png, jpg/jpeg, bmp, gif and other common graphics format?
S3 will not calculate dimensions for you. You calculate them beforehand.
You can use user-defined meta tags. You can set them when you send the object, or update them after they exist.
For private objects, the API must return user-defined meta tags.
For public objects, you can use the traditional GET verb or just the http HEAD verb in the object's public url.
Even javascript + ajax can read this header information.

Unable to open image after base64 decode at server-side

I am developing an cross-platform mobile app using Titanium Appcelerator. This app is based on Sakai, in this application i have to send image to the server.
Client side i am encoding the image with base64 encoding technique using Titanium API.
//Client-Side JavaScript Code
var selectedImageB64 = Ti.Utils.base64encode(selectedImage).toString();
Ti.Utils.base64encode API
and now I am sending this string to server and there I am decoding it,
//Server-Side Java Code
byte[] photoData = Base64.decode(selectedImageB64);
byte[] content = photoData;
Base64 API
now the decoded data (content) is passed to the appropriate method to save the image into the database. Till here everything is working well. Image is successfully stored in the database. The size of the original image and the image stored in the database are of equal, so that I thought this encoding and decoding process is done successfully. But when I am trying to open the image in the database the image viewer displays an error message saying "Windows Photo Viewer can't open this picture because either Photo Viewer doesn't support this file format, or you don't have the latest updates
to Photo Viewer.". So what I have to do now. What exactly is the problem?
Regards..
please try the following code:
var selectedImage=image.getImage();
var selectedImageB64=Ti.Utils.base64encode(selectedImage).getText()
works for me.
For mobile side :
var base64String = Ti.Utils.base64encode(imageView.toImage()).getText()
send base64String to server.
For Server Side :
String tempPic = (String)jsonMap.get("base64String");
byte pic[] = Base64.decodeBase64(tempPic.getBytes());
Now, Play with pic[] byte array. This code work for me.
A couple things to check:
1) Save the bytes from the server to the file system instead just to eliminate a variable (namely the db)
2) Actually print out the numeric value of say the first 10 bytes on the server side, and do the same on the client side. This is to make sure the base64 encode/decode functions are implemented correctly (or they are following the same standard).
3) I don't think you need to the toString after the base64encode, you might to a Ti.Api.Info on the object before and after to the toString
4) I would like to know more about what selectedImage object is, if its a blob object in titanium it may not be the image directly, but rather a wrapper around the image (So you maybe encoding the wrong data).
I found the solution for this, actually when sending the base64 encoded data from client '+' symbols in the encoded data are being replaced by a space. So i tried replacing space with '+' sign on server side. That solves this issue.

JSP to insert image into DB, display it to client

I want to insert an image to database and display it in another page. I am using the PostgreSQL database.
My guide suggests that I insert the image with its file path in the database. When displaying, in place of the src attrib of img tag put the path from database. So can I get any help for this .
Please guide me for this or give me link for similar kind of problem.
(I'm a final year student, and feel that this project requirement is difficult).
Your guide is entirely correct. Part of what you are supposed to be learning is problem solving: how to break a big problem down into many smaller, simpler problems you can solve piece by piece. It sounds like it's hinting at this, but expects you to be able to do that yourself, which is pretty reasonable.
You need to break this down into steps, and do each step in isolation. That's how anything but the most trivial programming task must be done.
(It isn't clear if you want to store the image data in the DB, or just a file system path, by the way, so I'm assuming you want to write the file to the local file system and just store the path in the DB).
Anyway, this should be fairly simple JSP. To display:
One JSP that:
Examines the query parameters for the image ID
Uses JDBC to fetch the associated path of the image on the file system from the database (a simple SELECT using the image ID as a query parameter)
Opens the image on the file system as a binary stream; also stats it to get its size
Sends appropriate HTTP headers eg Content-Type: image/jpeg and Content-Length: image-length-in-bytes to the client
Copies the raw image data from the image input stream to the output stream that sends to the client
Another JSP that generates the HTML and has an <img src="/the/image/jsp?imageid=blah"> link in it.
If you're required to submit just one JSP file, you can combine the two by having the JSP show a HTML page if it doesn't receive any query parameters, and send an image if it does receive an image id as a query parameter.
To insert:
One JSP that displays a HTML form with a file upload link if it doesn't get called with any HTTP POST data
If the JSP does get called with HTTP POST data:
** Issue a JDBC INSERT to create a record for the file in the database, but do not commit
** Access and decode the POST data using the methods provided in JSP
** Extract the desired file name from the form data and open a binary output stream to a file on the filesystem with that name
** Copy the image bytes into that output stream, url-decoding if required (the HTTP POST form handling code in JSP is likely to decode it to a byte stream for you, though)
** Flush and close the output stream
** Commit the transaction with the JDBC INSERT.
You should be able to find numerous examples of both with a quick Google search. If you can't, adapting examples from other programming languages should be easy enough.
For inserting you must think carefully about the error cases. That's a large part of proper programming.
I am intentionally not showing you code examples. You should be able to do this yourself if you're a final year student. You won't know everything you need, but by now you should know how to find out what you don't know when you need to know it. Tutorials. Documentation. Google. Writing test programs to figure things out. Method name autocomplete in NetBeans / Eclipse. Adapting sample code. You've got lots of options.

How to convert an image string into a stream in Ruby

I use Ruby Gem curb to fetch image by the method of body_str of Curl::Easy instance, then want to use RMagick to process the image, however Magick::Image.read needs a file name to read, but what i get is the content string of the image. Yes, i know i can firstly convert the image content string into a file, then pass the file name to Magick::Image.read method. But that will add one more IO operation.
So i want to know whether it's possible to convert an image content string into stream directly, so that i can use RMagick to read it directly.
Thank you in advance.
Check other class methods of Image class, particularly from_blob. Sounds like what you need.

Resources