I'm using paperclip to upload a pdf. Once the file is uploaded I need to split every page into a png. This is the command I think I need to use
convert -size 640x300 fileName.pdf slide.png
Now if I run that command from terminal it works fine, but I need a way of getting each slides name so I can add it into a model.
What's the best way to achieve this?
You should be able to have Paperclip do this conversion for you at the time of the upload, like this:
has_attached_file :pdfupload, :styles => { :pinged => ["640x300", :png] }
Then you can show the PNG version like so:
<%= image_tag #mymodel.pdfupload.url(:pinged) %>
(Obviously the name of the model and file will need to be changed to match yours.)
use `command` to execute system commads
(`-quotes)
`convert -size 640x300 fileName.pdf slide.png`
Related
Currently I'm trying to display .HEIC images in Rails 6. I'm using ActiveStorage ImageMagic to use variant to display jpg. Basically I'm trying to use
mogrify -format jpg myimage.heic
In the image to display jpg.
I added Rails.application.config.active_storage.variant_processor into application.rb to be able to use the variant. However it seems to break in the following line:
<%= image_tag post.image.variant(format: 'jpg'), class: "card-home__img" %>
Why is not working?
You can only call variant on an image that returns true when you call variable? on it.
Internally, ActiveStorage checks if ActiveStorage.variable_content_types contains your image's type. The default supported values are:
image/png
image/gif
image/jpg
image/jpeg
image/pjpeg
image/tiff
image/bmp
image/vnd.adobe.photoshop
image/vnd.microsoft.icon
image/webp
So it seems that currently .HEIC images are not supported.
You can instead apply a format transformation before attaching the image to a model or storing it, it might solve your use case.
Raised when ActiveStorage::Blob#variant is called on a blob that isn't variable. Use ActiveStorage::Blob#variable? to determine whether a blob is variable.
Source: https://edgeapi.rubyonrails.org/classes/ActiveStorage/InvariableError.html
I am using carrierwave and mini_magick gems to use images inside rails_admin. When I upload an image it fails with this error:
Failed to manipulate with MiniMagick, maybe it is not an image?
Original Error: `identify C:/Users/Zeke/AppData/Local/Temp/mini_magick20161027-21132-xdongz.png` failed with error:
identify.exe: RegistryKeyLookupFailed `CoderModulesPath' # error/module.c/GetMagickModulePath/662.
identify.exe: no decode delegate for this image format `PNG' # error/constitute.c/ReadImage/501.
And this doesn't happen when I don't include the following lines in my uploader.rb
# Process files as they are uploaded:
process resize_to_fit: [800, 600]
# Create different versions of your uploaded files:
version :thumb do
process resize_to_fill: [40, 30]
end
I require thumbnails, and how do I do it?
Here's What I'm sure of:
ImageMagick has been installed and is working for sure. I am able to convert png to jpg and jpg to png, identify images...
identify C:/Users/Zeke/AppData/Local/Temp/mini_magick20161027-21132-xdongz.png executes successfully when run in cmd (without admin priv, if that matters)
identify -list format gives a huuuuge list that almost contains every image format I can think of. And yes, it includes JPG, JPEG, PNG and all that I need.
convert -version does include jpeg png delegates
What am I doing wrong?
Both identify and convert I tested were working fine except for the fact that they were not actually used by rails, which used another installation of imagemagick which was very old and threw this translation missing error at first which I managed to solve by adding a few lines in en.yml
It is notable that System Environment Variables can be overridden by set path that only lasts long for that instance of cmd
Making rails use the new version of ImageMagick solved the problem.
Credits: Mark Setchell
In my Rails app, I have a form that allows users to upload images. My app is supposed to resize the images with the following controller method. (POST to this method, params[:file] contains the ActionDispatch::Http::UploadedFile that was uploaded:
def resize_and_store
file = params[:file]
# resize image
Magick::Image.read(file.tempfile).first
newimg = image.resize(100,100)
#etc... Store newimg
end
I get the following error, on the line that says Image.read:
Magick::ImageMagickError (no decode delegate for this image format `0xb9f6052c>' # error/constitute.c/ReadImage/544):
Testing this with an uploaded PNG file, it seems RMagick doesn't pick up that the temporary file is a PNG file. The code above does work if I read a locally stored PNG file, so it can't be that I'm missing the PNG decoder. How can I fix this and why does this happen?
You can do from_blob on a ActionDispatch::Http::UploadedFile param (this is how a file comes in):
images = Magick::Image.from_blob(params[:file].read)
Storing the file temporarily will solve the problem:
open('temp.png', 'wb') do |file|
file << uploaded.tempfile.read
end
images=Magick::Image.read('temp.png')
Probably wise to check input size as well.
Alternatively, parse the image from a blob.
Using the answer by #joost (or similar approach) really helped to point me in the right direction but it didn't work on the second attempt with the same temp file - my use case was creating multiple image types from the tempfile source. This is what I've used, wrapping in a File.open block so we don't leak the file descriptor:
File.open(tempfile, "rb") do |f|
img = Magick::Image::from_blob(f.read).first
resized = img.resize_to_fit(w, h)
resized.write(dest)
resized.destroy!
img.destroy!
end
Maybe there's something wrong with the form? You can consult with Rails Guide here:
Rails Guides: Uploading Files
I think that you may have multipart: true missing in your form declaration.
Also, I would strongly advise to use Carrierwave to handle file uploads. Among several things, it will help you to organize your file transformations (putting logic out of the controllers). Here's a railscast about it:
RailsCasts: CarrierWave File Uploads.
Good luck!
I have an Attachment model, which is using Paperclip to handle uploaded files. The file can be anything an image, a txt, doc, pdf, rar, zip, tar etc.
I want to create thumbnails only if the file uploaded is an image.
How to create thumbnails in Paperclip conditionally based upon file content_type
This is a nice solution:
before_post_process :image?
def image?
!(data_content_type =~ /^image.*/).nil?
end
You can also use the image? method in your views to either render an image_tag, or something else...
It looks like ImageMagick does not always convert a single favicon.ico file to a predictable single png file - for some favicon's, it generates a bunch of other favicon-01.png, favicon-02.png, etc... Is there a way to figure out which one's the actual converted favicon you want - or figure out how many got generated, to delete the unwanted ones?
I came across with the same problem while I was trying to convert blogger's favicon and I solved it by using -flatten parameter of Imagemagick like this:
convert "favicon.ico" -thumbnail 16x16 -alpha on -background none -flatten "favicon.png"
This likely happens because there are multiple images in the icon file - this is to provide differet resolutions for different contexts. Presumably you'd want to run a search in the target directory for favicon*.png, then check the dimensions of each one to find the one you wanted (deleting the others as you go).
I guess some of these are animated gifs. You can take the first one as described here:
http://www.imagemagick.org/script/command-line-processing.php
i.e.:
$magick> convert 'images.gif[0]' image.png
I don't have ImageMagic installed, but you might try the above for all favicon.ico, it might work fine.
Otherwise, you would probably need to write a script to check for favicon-01.png and, if it exists, rename it to favicon.png and delete favicon-*.png (provided you don't have anything else named like that in the working folder).