get image size before download - google-cloud-storage

I use Potoswipe in my project. Potoswipe requires the definition of the images size. In a time when I used PHP that was a simple, nowadays in the serverless time it looks like an issue.
How can I get an image size on a client? I’ve checked few cloud storages providers they all are offering resizing but it is not exactly what I’m looking for, I need a full image property before upload image when I initialize the App.

The easiest thing is to find a hosting service with an API that provides the metadata you need.
Failing that, you can work out the dimensions of an image by looking at just the first few dozen bytes. All modern image formats contain dimensions in a file header. Examples in three languages are linked to in the [Photoswipe FAQ][1].
So, you need to download enough bytes (this varies per file format). To do this you need to use the Range header in the HTTP request. For example:
curl http://i.imgur.com/z4d4kWk.jpg -i -H "Range: bytes=0-1023"
... will get the first kilobyte of that image. Whatever HTTP client you use, it will have a way to set a request header.

Related

Save image as base64 in mongoDB

I looking for the best way to upload an image from mobile phone to my server. I am currently using html5 to open the camera and take the picture, then I convert the file into a base64 string, then I send to the server, then save it in MongoDB.
I am expecting around 1000 to 1500 user request per day ( upload image ) , so I have the following question :
Is it a good way to do it?
Should I compress the base64, if yes how?
Should use a specific server to handle this task?
My backend is node express and the front end is ReactJS.
Thanks
It all depends on your situation. Reading and writing images from a cdn via i.e. streams is usually faster than reading and writing binary representations of images i.e. base64 from a database. However, your speed if reading from a cdn will obviously be effected by what service you use. Today, companies like Amazon can offer storage to a very cheap price so if you are not building a hobby app for like a student project you can usually afford it. Storing binary representation of images actually end up a little bit bigger in size than storing the image itself. You don't compress the base64, you compress the image before converting it. However, if you can't afford a storage account and if you know your users won't upload that many images it is usually enough to store binary representations of the images in a database. Mongo Atlas, for example, offers 512 mb for free on their database clusters. Dividing tasks of your app such as database requests and cdn services from your main application is usually a good choice if possible. This way you will divide the cpu, memory, etc. of your hardware and it will lead to faster reading and writing tasks for the user.
There are a lot of different modules for doing this in node. JIMP is a pretty nice one with loads of built in functions like resizing images and converting them to binary, either as Buffer or base64.

Up-/Download of big files via REST

Simple question: I want to upload/download large files around via REST. What is the best practice to do that? Are there any chunk-patterns, do I use multipart on the transport layer, what do you recommend?
Use case: we have an API where you can upload payments (e.g. 500mb) and download large account statement files. I am aware that other protocols exist to do that but how is it done with REST?
see the answers here. They might help with your problem:
REST design for file uploads
Large file upload though html form (more than 2 GB)
In conclusion:
With REST you can simply use HTTP header fields to specify the content size, e.g use the Content-Type multipart/form-data in your request for files up to the server limit (usually 2GB - 4GB) and for files larger than that you will have to split the request in multiple parts.
Also check out this answer to see if byte-serving or chunked encoding makes sense in your application:
Content-Length header versus chunked encoding

What is the best way to transfer images from a server to an iPhone app?

What is the best way to transfer images from a server to an iPhone app? Is sending a base64 string faster or sending a link to the image source and then downloading from this source?
Is sending a base64 string faster
Definitely not - base64 is, on average, 1.4x larger than binary. (http://en.wikipedia.org/wiki/Base64)
or sending a link to the image source and then downloading from this source?
I'm not sure what you mean here - but two requests when you could simply make one (the request for the image) is also not a good idea.
Simply download the image like normal is the best approach.
You could make sure you're making effective use of caching and GZIP (standard HTTP stuff).
The second option is better than first,it would be better to have http link.
just make sure you load the images just once.

RESTful processing function/resource: GET or POST?

I am creating a RESTful web service and some of the resources are computing or processing functions. For instance, it is possible for a user to scale and convert images through the API by submitting an image and receiving the scaled or converted image back.
According to the RESTful Web Services Cookbook, section 2.5, I should use GET:
Treat the processing function as a resource, and use HTTP GET to fetch a
representation containing the output of the processing function. Use query
parameters to supply inputs to the processing function.
This is clear for cases where the inputs are simple (such as the long/lat coordinates of a point). However, should I follow the same advice for larger inputs such as images? As far as I know it is not possible to send this much data as a query parameter.
Use POST. In effect you are doing an Image Upload and processing on the server. Can't think of another way to do it unless the image is already stored on the server.
The image is a resource. Use PUT to put the resource on the server, then GET the resource, supplying parameters indicating your desired size.
Due to protocol limitations on HTTP I advice against it. This is a very valid very viable example of an exception that should be made to this rule.
Check out this link http://support.microsoft.com/default.aspx?scid=KB;en-us;q208427. It says that the maximum URL for IE is 2083 characters

Check whether image exists on remote URL - iphone SDK3.0

In my iPhone I need to show a number of images from remote URL. Some of the URL may download some amount of data and but it may not contain actual image data. So what I need to do to check whether the downloaded data is an image data or not?
Thnx in advance,
Syam S
Do you mean that you're given the actual URLs of the images (e.g. http://sstatic.net/so/img/logo.png), and need to determine whether they're valid images, or do you mean that you're given the URL of a page which may or may not contain images?
Assuming the former (which seems more likely, and is easier than the other case), you'll want to look at the Content-type header returned by the server when you request the URL. Its value will generally be something self-explanatory, like text/html, image/png, image/gif, and so on. If you want image data, it should suffice to check whether the string starts with image/ — although you might want to refine that method if it turns out not to be accurate enough for your needs.
To get this header in an iPhone app, you'll probably want to use the methods described in Apple's Communicating with HTTP Servers guide on their iPhone developer site. If you're only interested in the headers, you might consider using CFReadStreamCreateForStreamedHTTPRequest (instead of what the guide suggests, which is CFReadStreamCreateForHTTPRequest) to read the response. This will avoid buffering all the response data, and will let you download what you need.