Carrierwave and mini_magick finding widths & height - ruby-on-rails

After a bit of investigation I decided to use Carrierwave and mini_magick on my new rail3 app.
I've set it up and it works perfectly. However I have a one question. I'd like to be able to access the width and height, so I can form the html correctly. However, there is no default data from which to get this information. Because of the way it stores the data I'm I cannot think of any way that I can add it to the database.
Can anyone suggest any tips or ideas? Is it even possible?

class Attachment
mount_uploader :file, FileUploader
def image
#image ||= MiniMagick::Image.open(file.path)
end
end
And use it like this:
Attachment.first.image['width'] # => 400
Attachment.first.image['height'] # => 300

Just for record, I have used a similar solution, however using files with Mongo GridFS, here it goes:
def image
#image ||= MiniMagick::Image.read(Mongo::GridFileSystem.new(Mongoid.database).open(file.path, 'r'))
end

Disadvantage of calculating Image height / width using RMagick or MiniMagick in run time:
Its CPU intensive
It requires Internet to get image and calculate the dimensions.
Its a slow process
FYI You can also calculate the Image Height, Width after the
Image is fully loaded by using the load event associated with the
<img> tag with the help of jQuery.
For Example:
$(document).ready(function(){
var $image = $('.fixed-frame img');
$image.load(function(){
rePositionLogo($image);
});
if($image.prop('complete')){
rePositionLogo($image);
}
});
function rePositionLogo($image){
var height = $image.height();
var width = $image.width();
if (width > height) {
$image.parents('.header').addClass('landscape');
var marginTop = (105 - $image.height())/2;
$image.css('margin-top', marginTop + 'px')
}else{
$image.parents('.header').addClass('portrait');
}
}
Be careful, because load() will not trigger when an image is already loaded. This can happens easily when an image is in the user's browser cache.
You can check if an image is already loaded using $('#myImage').prop('complete'), which returns true when an image is loaded.

I think the best way is to store the image dimensions in the model (database).
In my case, the model name is attachment. Then I created a migration:
rails g migration add_dimensions_to_attachments image_width:integer image_height:integer
After that, run the migration:
rake db:migrate
In my Image Uploader file app/uploaders/image_uploader.rb, I have:
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
process :store_dimensions
private
def store_dimensions
if file && model
model.image_width, model.image_height = ::MiniMagick::Image.open(file.file)[:dimensions]
end
end
end
With this, the image dimensions is saved in the upload step.
To get the dimensions, I simply run attachment.image_width or attachment.image_height
See the reference here.

Related

Find PDF Form Field position

I realize this question has been asked a lot, but I could not find any information about how to do this in RoR. I am filling a PDF with form text fields using pdf-forms but this does not support adding images, and I need to be able to add an image of a customer's signature into the PDF. I have used prawn to render the image on the existing PDF, but I need to know the exact location to add the image on the signature line. So my question is how can I look at an arbitrary PDF and find the exact position of the "Signature" form field?
I ended up using pdf2json to find the x,y position of the form field. I generate a JSON file of the original pdf using this command:
%x{ pdf2json -f "#{form_path}" }
The JSON file is generated in the same directory as form_path. I find the field I want using these commands:
jsonObj = JSON.parse(File.read(json_path))
signature_fields = jsonObj["formImage"]["Pages"].first["Fields"].find_all do |f|
f["id"]["Id"] == 'signature'
end
I can use prawn to first create a new PDF with the image. Then using pdf-forms, I multistamp the image pdf onto the original PDF that I want to add the image to. But multistamp applies each page of the stamp PDF to the corresponding page of the input PDF so make sure your image PDF has the correct number of pages or else your image will get stamped on every page. I only want the image stamped onto the first page, so I do the following:
num_pages = %x{ #{Rails.configuration.pdftk_path} #{form_path} dump_data | grep "NumberOfPages" | cut -d":" -f2 }.to_i
signaturePDF = "/tmp/signature.pdf"
Prawn::Document.generate(signaturePDF) do
signature_fields.each do |field|
image Rails.root.join("signature.png"), at: [field["x"], field["y"]],
width: 50
end
[0...num_pages - 1].each{|p| start_new_page }
end
outputPDF = "/tmp/output.pdf"
pdftk.multistamp originalPDF, signaturePDF, outputPDF
You can use this gem 'wicked_pdf. You just write html, and this gem automatically convert it to pdf
Read more https://github.com/mileszs/wicked_pdf
Here's a pure ruby implementation that will return the field's name, page, x, y, height, and width using Origami https://github.com/gdelugre/origami
require "origami"
def pdf_field_metadata(file_path)
pdf = Origami::PDF.read file_path
field_to_page = {}
pdf.pages.each_with_index do |page, page_index|
(page.Annots || []).each do |annot|
field_to_page[annot.refno] = page_index
end
end
field_metas = []
pdf.fields.each do |field|
field_metas << {
name: field.T,
page_index: field_to_page[field.no],
x: field.Rect[0].to_f,
y: field.Rect[1].to_f,
h: field.Rect[3].to_f - field.Rect[1],
w: field.Rect[2].to_f - field.Rect[0]
}
end
field_metas
end
pdf_field_metadata "<path to pdf>"
I haven't tested it particularly thoroughly but the snippet can hopefully get you most of the way there.
Also -- keep in mind the above coordinates calculated are in points from the bottom left of the pdf page rather than the top right (and are not in pixels). I believe there's always 72 points per inch, and you can get the total page points by calling page.MediaBox in the pdf.pages loop above. If you're looking for pixel coordinates, you need to know the DPI of the resulting rendered document.

ImageScalr + Grails + AWS plugin

Im trying to implement some pics upload to my webapp.
The problem im having is that im trying to use Imagescalr to create thumbs of the original photos, and Im using AWS Plugin to upload them to my bucket.
So, the code i've is the following: (deleted validations and things that dont influence the question/posible answer)
def uploadPic() {
def f = request.getFile('file')
.
.
.
def s3file = f.inputStream.s3upload(filename) { //this is for the normal photo
path "POI/ID/"
}
def imageIn = ImageIO.read(???); //Dont know if I can put the f file here as parameter... do I have to store it somewhere first, call the s3 file, or I can resize on - the - fly?
BufferedImage scaledImage = Scalr.resize(imageIn, 150);
//Here I should upload the thumb. How can I call something like what is done for the normal photo?
So the problems/questions are explained along the code, hope someone knows how to do this. Thanks in advance.
in Grails, request.getFile() doesn't return a java.io.File object. You could use the input stream to write out a file but I'd probably do something like this, although I'd use services and break things up a little more. But this should get you started in the right direction. Consider this more of a pseudo code workflow suggestion.
def uploadPic() {
def f = request.getFile('file')
def tempFile = new File('/some/local/dir/myImage.png')
f.transferTo(tempFile)
// upload the original image to S3
whateverApi.s3Upload(tempFile)
def bufferedImage = ImageIO.read(tempFile)
def scaledBufferedImage = Scalr.resize(bufferedImage, 150)
// write the scaledImg to disk
def scaledImage = new File('/some/local/dir/myImage-150.png');
ImageIO.write(scaledBufferedImage, "png", scaledImage);
//upload scaled image to S3
whateverApi.s3Upload(scaledImage)
// clean up
tempFile.delete()
scaledImage.delete()
}

Rmagick composite image without source

I have a Gallery model and a Post model. Each Post has an image, and I would like to create a composite image for each Gallery based on the posts in that gallery. I have a method for galleries that returns urls to the top 4 images in the gallery:
gallery.images_for_preview # => returns array of 4 image urls (200x200 images)
I'm using Carrierwave + RMagick to generate a composite image. I'm trying to follow along here: http://railscasts.com/episodes/374-image-manipulation but it seems my use case is slightly different. I have:
class GalleryImageUploader < CarrierWave::Uploader::Base
...
def store_dir
"galleries/#{model.obfuscated_id}"
end
version("full") { process :full_image }
def full_image
images = model.images_for_preview
puts images
manipulate! format: "png" do
image0 = Magick::Image.read(images[0]).first
image1 = Magick::Image.read(images[1]).first
image2 = Magick::Image.read(images[2]).first
image3 = Magick::Image.read(images[3]).first
underlay = Magick::Image.new(406, 406) { self.background_color = "#333333" }
underlay.composite!(image0, 2, 2, Magick::OverCompositeOp).composite!(image1, 204, 2, Magick::OverCompositeOp).composite!(image2, 2, 204, Magick::OverCompositeOp).composite!(image3, 204, 204, Magick::OverCompositeOp)
end
end
The full_image operation doesn't require a source file, but the only way I can seem to get it to generate the composite image is to do something like:
gallery.remote_image_url = "path/to/image"
which generates the image I need at /galleries/:id/full_image.png but also processes and generates an image at /galleries/:id/image.png
Is it possible to skip the "source" image and just generate a composite, then upload that as the ImageUploader's primary image?
Yes, just process the data on the go, no need for the version itself:
Replace this line:
version("full") { process :full_image }
With this one
process :full_image

MiniMagick (+Rails): How to display number of scenes in an image

I have a Rails app that uploads images for image processing, and I want to be able to 1) See how many pages/frames/scenes there are in an image, and 2) split multi-page images into single-page jpegs.
I'm having no trouble converting image types for single-scene images, but I can't quite puncture the ImageMagick documentation to understand exactly what I'm to do. The doc page I'm using is here:
http://www.imagemagick.org/www/escape.html
For the most part, I would like the code to be as simple as
def multiPage?( image )
img = MiniMagick::Image.open(image.path)
numPages = img.format("%n") #This returns Nil
count > 1 ? true : false
end
Does anyone have a better idea of what to do than I do? Thanks in advance!
Ok, well this is a bit of a hack, but when I did:
numPages = img[:n]
I would get numPages resulting in a string of the letter 'n' as many times as there are pages in an image, so:
#img -> 4-page image
numPages = img[:n] # => 'nnnn'
Probably not the best answer, but at least it works.
UPDATE:
Found a better way
numPages = Integer(img["%n"])

Saving images with Carrierwave inside a ruby worker

I'm trying to write a method to store an image from a given url, inside a ruby worker. It comes along my Rails app in which I display the object image.
Here is what I've come up with :
def store(url)
#object = Object.find(1)
#object[:image] = CarrierWave::Uploader.store!(image_url)
end
It doesn't seem to work at all.
Any clues?
Is there another way around?
[EDIT]
Here is the current situation :
def store
#object = Object.find(1)
my_uploader = ImageUploader.new
image = open("http://twitpic.com/show/iphone/xxxx.jpg")
# or for a local file:
image = File.open(Rails.root.join('xxxx.png'))
#object[:image] = my_uploader.store!(image)
#object.save!
end
The filename in the [:image] attibute is still wrong. It gives "[:store_versions!]". How do I get the filename right?
[EDIT2]
Got the filename right by adding #artwork[:image] = my_uploader.filename before save.
But #object = Object.find(1) won't work. How do I access the Object class, which is inside my rails app, from the worker?
#object.image.store!(image) finally did the job!
You'll want to create a new uploader object and point it to your file
image = File.open(Rails.root.join('path', 'to', 'file.png'))
#object[:image] = YourUploader.new(image)

Resources