Google Apps Script : how to get image from blob? - google-app-engine

I get an image on a remote server with :
var response = UrlFetchApp.fetch(fileURL);
var fileBlob = response.getBlob();
How to get the image from the blob ? I would like to use this image in order to get its size (with getHeight() and getWidth()) and do different other operations on it.
Thanks

You want to directly retrieve the width and height from the blob of image data using Google Apps Script.
If my understanding is correct, how about this answer?
Pattern 1:
In this pattern, the width and height are retrieved from the blob of image data using Google Apps Script.
Unfortunately, in the current stage, there are no methods for directly retrieving the width and height from the blob of image data. So I created the script for retrieving by analyzing the binary level as a GAS library. This is also mentioned by Cooper's comment. Using this library, you can directly retrieve the width and height from the blob of image data using Google Apps Script.
Sample script:
About the install of the library, please check here.
var response = UrlFetchApp.fetch(fileURL);
var fileBlob = response.getBlob();
var res = ImgApp.getSize(fileBlob);
Logger.log(res)
Result:
{
identification : ### BMP, GIF, PNG and JPG ###,
width : ### pixel ###,
height : ### pixel ###,
filesize : ### bytes ###
}
In this library, the width and height can be retrieved from BMP, GIF, PNG and JPG.
Pattern 2:
In this pattern, the width and height are retrieved using a Google Document. In this case, please enable Drive API at Advanced Google services.
Sample script:
function getSize_doc(blob) {
var docfile = Drive.Files.insert({
title: "temp",
mimeType: "application/vnd.google-apps.document",
}).getId();
var img = DocumentApp.openById(docfile).insertImage(0, blob);
Drive.Files.remove(docfile);
return {width: img.getWidth(), height: img.getHeight()};
}
function main() {
var response = UrlFetchApp.fetch(fileURL);
var fileBlob = response.getBlob();
var res = getSize_doc(fileBlob);
Logger.log(res)
}
In this script, at first, Google Document is created as a temporal file, and the width and height are retrieved from the inserted image from the blob. Then, the temporal file is deleted.
Pattern 3:
In this pattern, the width and height are retrieved by creating the blob as a file. In this case, please enable Drive API at Advanced Google services.
Sample script:
var response = UrlFetchApp.fetch(fileURL);
var fileBlob = response.getBlob();
var res = Drive.Files.insert({title: "temp"}, fileBlob);
Drive.Files.remove(res.id);
Logger.log(res.imageMediaMetadata.width)
Logger.log(res.imageMediaMetadata.height)
In this script, at first, a file is created from the blob as a temporal file, and the width and height are retrieved from the created file. Then, the temporal file is deleted.
References:
ImgApp
Advanced Google services
Class InlineImage of Document service
Files of Drive API
If this answer was not the direction you want, I apologize.

Related

How to use Network Image in PDF Flutter

I have a database document where there is links available for multiple images. I want to display this images in pdf.
I am using pdf: ^2.1.0 to edit the pdf file and flutter_pdfview: ^1.0.1 to view the pdf.
I am currently displaying images like this but this can only display images from the asset
final profileImage = pw.MemoryImage(
(await rootBundle.load('assets/images/image.png')).buffer.asUint8List(),
);
//Inside Widgets
pw.Image(profileImage)
I want to show images using a link but its not being possible.
This will help you
var response = await http.get(Uri.parse(imageUrl));
var data = response.bodyBytes;
Image(pdfW.MemoryImage(data),)

How to Embed the Blob Video file in Quill JS

I am trying to append the recorded video which is blob object into quill editor but the video which is appended in the editor is not playable.
Able to see only the blob object getting printed in the quill editor. If i try to open the contents in the browser it is working fine. Any suggestions?
enter image description here
well,according to this issue,
I think you maybe need to overwrite the video module's sanitize method to make it work,for image it can work like this:
var Image = Quill.import('formats/image')
Image.sanitize = function(url) {
return url
}
so as I guess, following things maybe useful:
var Video = Quill.import('formats/video')
Video.sanitize = function(url) {
return url
}
and you may need provide blob url to make it work

Read KML From Database

I'm actually struggeling with a problem handling some kml files with google map in my Javascript application.
I wrote a method with that I'm reading a KML file from an URL or my local file system and storing the content as a String in a Database. Now i would like to activate layers that are stored in my db by clicking a button. Everything is fine up to here.
In every example i can find they are only using the url-attribute of a KmlLayer by passing an url to a KML-File.
like here:
var ctaLayer = new google.maps.KmlLayer({
url: 'http://googlemaps.github.io/js-v2-samples/ggeoxml/cta.kml',
map: map
});
But since my files are stored as Strings in my db I don't have an url to a file, only the content. I can't find a way to only pass the XML-String as content.
Somebody here who can help?
Maybe someday somebody will struggle with a similar problem. The solution was a little bit tricky. I needed to create a Blob with the content of my String. With the blob I created a file and packed it into an URL. This URL you can pass to your kml parser. I used https://github.com/geocodezip/geoxml3 for that.
vm.activeLayers.forEach(function(value, key) {
var file = new Blob([value], {type: 'kml'})
var url = URL.createObjectURL(file);
var myParser = new geoXML3.parser({
map : map
});
myParser.parse(url);
})

Is it possible to resend image url received from server back to it as an image after processing

I have a remote server which has profile data and an image and when I retrieve it, I get the image url, which I can display using the <img> tag, by using the url.
When I have to modify the details, I send a new image, for which I upload a new image using a small AngularJS function and send the whole data using formData and http request.
But, out of curiosity, I was wondering whether there is any way to send an object of the same image using the url I received from the server.
Another reason I thought of it was that I was thinking of maybe changing its dimensions,etc.
Just in case if I ever needed to generate a thumbnail of the image of a smaller size in future.
Solved it myself by looking around and generating a canvas from the url, through dataURI by using the function :
var byteString = atob(dataURI.split(',')[1]);
var ab = new ArrayBuffer(byteString.length);
var ia = new Uint8Array(ab);
for (var i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
var blob= new Blob([ab], { type: 'image/jpeg' });
to create a blob to send the image back after scaling(was required).

make a copy of an image in blobstore

I have an image in blob store which is uploaded by users(their profile pic). I want to make a copy of the same and and re-size the copy so that it can be displayed as a thumbnail. I want to make a copy of the same instead of using the ImageService because this would be used more often compared to the profile image.
What I am doing here is this:
reader = profile_image.open() #get binary data from blob
data = reader.read()
file_name = files.blobstore.create(mime_type=profile_image.content_type)#file to write to
with files.open(file_name, 'a') as f:
f.write(data)
files.finalize(file_name)
blob_key = files.blobstore.get_blob_key(file_name)
image = images.Image(blob_key = blob_key)
image.resize(width=32, height=32)
entity.small_profile_pic = <MyImageModel>(caption=<caption given by user>,
picture=str(blob_key))
This is giving me error:
BadValueError: Image instance must have a complete key before it can be stored as a reference.
I think this is because the blob is not saved(put()) into the datastore, but how do i do it. Doed files.blobstore.get_blob_key(file_name) not do it ?
I would also like to ask: does the blobstore also cache the dynamically transformed images images served using get_serving_url() ...
I would use the get_serving_url method. In the doc is stated that:
The get_serving_url() method allows you to generate a stable, dedicated URL for serving web-suitable image thumbnails. You simply store a single copy of your original image in Blobstore, and then request a high-performance per-image URL. This special URL can serve that image resized and/or cropped automatically, and serving from this URL does not incur any CPU or dynamic serving load on your application (though bandwidth is still charged as usual). Images are served with low latency from a highly optimized, cookieless infrastructure.
Also the code you posted doesn't seem to follow the exampled posted in the docs. I would use something like this
img = images.Image(blob_key=original_image_key)
img.resize(width=32, height=32)
thumbnail = img.execute_transforms(output_encoding=images.JPEG)
file_name = files.blobstore.create(mime_type='image/jpeg')#file to write to
with files.open(file_name, 'a') as f:
f.write(thumbnail)
files.finalize(file_name)
blob_key = files.blobstore.get_blob_key(file_name)

Resources