Ruby on Rails Paperclip Image dimention validator - ruby-on-rails

Please help me to make validation for maximum width and height of image.
I have tried like this.
validates :picture, dimensions: { width: 612, height: 792 }
But I got Unknown validator: 'DimensionsValidator' message.
Kind Regards.

Ensure you have added gem 'paperclip-dimension-validator' to your Gemfile

Related

Rails 6 Axlsx Excel Gem - Images not displaying

I don't know if it's my fault or if it just doesn't work with Rails 6, but my images in the file are not displaying:
I tried:
img = File.expand_path(Rails.root+'app/assets/images/logo.jpg')
and also like described in the examples:
img = File.expand_path('../logo.jpg', __FILE__)
sheet.add_image(:image_src => img, :noSelect => true, :noMove => true) do |image|
image.start_at 5, 20
image.end_at 7, 22
end
The image is found (at least I don't get an error for this) but the only thing I see in the generated file (the rest of the creation works fine) is:
Could it be the image size not fitting in the cells? Or is it resizing automatically?
Any suggestions or ideas? I don't know what's wrong or what else I could try
Did you try setting height and width for the image? Have a look at this old answer https://stackoverflow.com/a/47982814/1796645 (potential duplicate question)
If you found a bug, then you could report it in https://github.com/caxlsx/caxlsx/issues

Prawn pdf 2 and Rmagick

i've got trouble for croping a picture with Rmagick and then display with Prawn
Here is the code :
First a method to instantiate the image with RMagick :
def build_magick_image(path)
if path.instance_of?(StringIO)
Magick::Image.from_blob(path.read)
else
Magick::Image.read(path)
end.first
end
In my case i have an instance of StringIO.
Then i crop the picture :
img = build_magick_image(picture_path)
cropped_img = img.resize_to_fill(1735, 1560)
But now i can't find how to display it in my Prawn pdf.
My last try is :
pdf.image cropped_img, at: [image_x, image_y], height: image_height.mm , width: inner_page_width.mm
But obviously it doesn't work.
Can someone help me?
Thanks :)
I found your question when I was trying to figure out the same thing 😬 Here's the solution I came up with:
Wrap your image blob in a StringIO object, which will give Prawn the interface it needs. In your specific case:
cropped_img_sio = StringIO.open(cropped_img.to_blob)
pdf.image cropped_img_sio, at: [image_x, image_y], height: image_height.mm, width: inner_page_width.mm

How can I display a svg in prawn without saving a .svg file before?

For this moment, I generate a svg file and then I read it and I display it in my pdf:
qr = Barby::QrCode.new('test')
outputter = Barby::CairoOutputter.new(qr).to_svg
File.open('myfile.svg', 'wb'){|f| f.write outputter }
pdf.svg IO.read('myfile.svg'), width: 50, height: 50
My question is: how can I display my svg without saving a .svg file before. I know there is a css property that allow that but I can't use css with prawn...
Any ideas?
It's working like that:
pdf.svg outputter, width: 50, height: 50
thanks #Mark Thomas

rails axlsx format table header diagonally

I am using the ruby on rails axlsx gem to render my excel.
Now I want to style the text so that it shows up diagonally as shown in the sample image attached.
I am not sure where or how to set this alignment feature in the code.
Any help is appreciated!
alignment: {:textRotation => 60}
That did it for me.
I managed to find a similar issue from the axlsx github page:
https://github.com/randym/axlsx/issues/110
I wasn't able to locate it in the docs.
The complete code can be implemented as follows:
my_style {
alignment: {:textRotation => 60, horizontal: :center, vertical: :center, wrap_text: true},
}
sheet.add_row(headers, style: styles[:my_style], height: height)

Pre-compiling issues using the Cloudinary gem

I'm using Cloudinary in my application and it works like a dream in my dev environment. However, when I deploy the app to Heroku and visit a page that has cl_image_tag, it gives me the "something went wrong" page.
When I look at the log it gives me this error:
ActionView::Template::Error ( isn't precompiled)
This is the line causing the problems:
<%= cl_image_tag(t.image, width: 175, height: 175, crop: :fill, gravity: :faces, border: { color: '#CCC' }) unless t.image.nil? %>
Any help is greatly appreciated.
This error is thrown when the image is nil. Maybe the image attribute of t is nil in this case?

Resources