In Rails 3, how can I pass thumbnail image as Byte Array to JSON?
For converting images to text you could convert it to base64.
Check out:
ruby:
http://ruby-doc.org/stdlib-1.9.3/libdoc/base64/rdoc/Base64.html
jquery:
http://archive.plugins.jquery.com/project/base64
UPDATE:
How to encode media in base64 given URL in Ruby
Related
The Api is working when I'm using the form-data but the same is not working when I'm using raw JSON. How can we convert form data to raw json with boundary?enter image description here
I am making an API call and reading the response in JSON
r = requests.get(query_url, headers=headers)
pprint(r.json())
But the 'content' is not in text format
'content': 'CmRlbGV0ZSBmcm9tICB7eyBwYXJhbXMuY3VyYXRlZF9kYXRhYmFzZV9uYW1l\n'
'IH19LmNybS5BRkZJTElBVEVfUFJJQ0lORzsKCklOU0VSVCBJTlRPICB7eyBw\n'
'TkcKICB3aGVyZSAxID0gMQogIAogIDsKICAKICAKCg==\n'
How do I convert the 'content' to text
For full context, I am trying to download code from the GitHub repo as text to store in our Database
Are you using python and in particular the requests package? If so, I think you could use r.text.
docs: https://pypi.org/project/requests/
Thanks to the article #CherryDT pointed me to, I was able to get the text
import base64
r_dict = r.json()
content = r_dict['content']
base64.b64decode(content)
I'm currently working on a Rails 6 application using ActiveStorage where I'm trying to render images as jpg. I'm using Cloudinary to render the images. I'm trying to support .HEIC images in my web app. A user can upload HEIC images to Cloudinary but I would like for my application to render the image as jpg
When I render the image I see that the browser is rendering the HEIC image which is not supported by browsers.
ActiveStorage uploads the image to the cloud:
Redirected to http://res.cloudinary.com/XXXXXXXXX/image/upload/xxxxxxxxxxxq3r4.HEIC
Completed 302 Found in 24ms (ActiveRecord: 16.1ms | Allocations: 2588)
[ActiveJob] [ActiveStorage::AnalyzeJob] [ac0d5880-xxxxxxxxxxxxxxxxxxxxxxxxxx] Cloudinary Storage (338.6ms) Downloaded file from key: kjpith3bxxxxxxxxxxxxxxxx
[ActiveJob] [ActiveStorage::AnalyzeJob] [ac0d5880-a243-4fef-xxxxxxxxxxxxxxxxxxxx] Skipping image analysis because ImageMagick doesn't support the file
However, I try to render the image from the views as jpg using the following.
<%= cl_image_tag(url_for(post.image), :format => :jpg , class: "card-home__img") %>
But the image is still calling the HEIC image format from this url:
https://res.cloudinary.com/artsyspace/image/upload/v1584732132/wbnknx9ighl6p4ok072u7kd8r5og.heic
Instead of calling the jpg
https://res.cloudinary.com/artsyspace/image/upload/v1584732132/wbnknx9ighl6p4ok072u7kd8r5og.jpg
How can I configure Cloudinary and ActiveStorage to render images or convert images to jpg?
Weirdly, the documentation in one place says the argument is fetch_format, while another shows an example using format.
Worst case, if you're having trouble with the cl_image_tag helper, you can write your own to construct the URL with a .png extension.
https://res.cloudinary.com/artsyspace/image/upload/v1584732132/wbnknx9ighl6p4ok072u7kd8r5og.jpg
It looks like you are sending the full url to the cl_image_tag method.
The cl_image_tag needs just the public id to generate the url.
So the call should be:
<%= cl_image_tag("wbnknx9ighl6p4ok072u7kd8r5og", :format => :png , class: "card-home__img") %>
Of course, make sure to change the hardcoded public id above to the variable holding the public id.
You can get the public id of the resource in the response of the upload.
And one note on the difference between Cloudinary's format and fetch_format:
format would change the extension of the resource i.e
Cloudinary::Utils.cloudinary_url('sample', :format => "png")
will produce https://res.cloudinary.com/demo/image/upload/sample.png
while using fetch_foramt will change the format using the relevant flag, i.e
Cloudinary::Utils.cloudinary_url('sample', :fetch_format => "png")
which will produce https://res.cloudinary.com/demo/image/upload/f_png/sample
In this specific case, both would produce the same png image, but using fetch_format would allow using one of Cloudinary's best features which is optimizing the image automatically using :fetch_format => "auto": https://res.cloudinary.com/demo/image/upload/f_auto/sample
In Rails 6, .key will return the Cloudfare's image ID.
<%= cl_image_tag(post.image.key, :format => :jpg , class: "card-home__img") %>
I have a requirement wherein I need to display details of users after searching from LDAP using PUMA API.
I'm having troubling displaying the jpegPhoto of the user.
Here's what I'm doing:
First I'm querying the user by using:
PumaLocator.findUsersByAttribute(uid, user);
After that we get a User list Object.
For each user, we fetch all the attributes which is in the form of a Map.
I'm getting the following value for while retrieving the jpegPhoto:
map.get("jpegPhoto") --> [B#7a2f8a54
It seems that the Puma API returns a Binary string. Does anyone know how to display this in the portlet?
Any help would be greatly appreciated. Thank you
I think it more likely this is a byte[] array than a string.
You can probably base64 encode this binary into an encoded string and use it in an HTML image tag.
byte[] photoBytes = (byte[]) map.get("jpegPhoto");
String encodedPhoto = org.apache.commons.codec.binary.Base64.encodeBase64(photoBytes);
Then later, perhaps in a JSP (example assumes JSTL variable in scope named encodedPhoto):
<img src="data:image/jpeg;base64,${encodedPhoto}"/>
A way of doing this is to access the image through the portal service servlet instead of using your own servlet: /wps/um/secure/users/profiles/[oid]/jpegPhoto, in which you replace [oid] with the ObjectID of the user. This ID string can be obtained using IdentificationMgr.getIdentification().serialize(user.getObjectID())
The photo of the current user you can access using: /wps/um/secure/currentuser/profile/jpegPhoto
Portal is giving you data as byte array. It will never give you as URL.
You can write a servlet which will write this byte array to output stream.
Use that servlet URL as src of tag. It will start rendering on browser.
FYI, you can't print byte array to browser and expect it to treat as image.
Image or any other files has to come as a resource not as content.
I have a resizing function that does it's primary job right, but doesn't save
the exif data that comes with the original image file. Everything seems to be
correct until I call ->writeImage(); function, which seems to strip the exif
data of the image.
$resizeimage= new Imagick($image);
$exifDataArray = $resizeimage->getImageProperties("exif:*");
$resizeimage->thumbnailImage($width,$height);
$resizeimage->setImageCompression(imagick::COMPRESSION_JPEG);
$resizeimage->setImageCompressionQuality(90);
echo $resizeimage->setImageProperty('Exif:Make', 'BLABLABLA');
$resizeimage->writeImage();
$updated= new Imagick($image);
echo $updated->getImageProperty('Exif:Make');
Note that If I print_r($exifDataArray); it shows the exif data
To make a change I put 'Exif:Make', 'BLABLABLA' and see if the
new exif data is written on the image, and it is written correctly
but after I resizeimage->writeImage();
No more exif data is present on the image file.
Can anybody help? Is there any way to keep the exif data on the image?
It seems the error isn't there for the first time.
A workaround I use at the moment:
exiftool -overwrite_original_in_place -tagsFromFile $source_file $converted_file
You should not use the thumbnailImage image method if you want to preserve exif data. The following is from the documentation of ImageMagick (http://www.imagemagick.org/script/command-line-options.php#thumbnail):
-thumbnail geometry
This is similar to -resize, except it is optimized for speed and any image
profile, other than a color profile, is removed to reduce the thumbnail size.
You should use the method resizeImage instead (http://www.php.net/manual/en/imagick.resizeimage.php)