why Getting error on uploading the file with Webp-ffi gem? - ruby-on-rails

Unsupported color conversion request
*** WebP::EncoderError Exception: Cannot read input picture file
got this error on certain jpg file. while converting into jpg again from google does the job but why is this?
'''webp_path = "#{filename.ext}.webp"
# Encode (convert) image to webp format with passed options
WebP.encode(path, webp_path, options)
# HACK: Changing of this two instance variables is the only way
# I found to make CarrierWave save new file that was created
# by encoding original image.
#filename = webp_path.split('/').pop
#file = CarrierWave::SanitizedFile.new(
tempfile: webp_path,
filename: webp_path,
content_type: 'image/webp'
)'''

This is what I use successfully, obviously need MiniMagick installed on the dev computer and server:
class ImagesUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
def store_dir
"uploads/story/#{model.created_at.strftime('%Y').to_s.underscore}/#{model.created_at.strftime('%m').to_s.underscore}/#{model.created_at.strftime('%d').to_s.underscore}"
end
def default_url(*args)
"/fallback.webp"
end
process :resize_to_fill => [1200, 677]
process convert_to_webp: [{ quality: 80 }]
version :preview do
process convert_to_webp: [{ quality: 80, resize_w: 470, resize_h: 265 }]
end
def extension_allowlist
%w(jpg jpeg gif png webp)
end
private
def convert_to_webp(options = {})
# Build path for new file
webp_path = "#{path}.webp"
# Encode (convert) image to webp format with passed options
WebP.encode(path, webp_path, options)
# HACK: Changing of this two instance variables is the only way
# I found to make CarrierWave save new file that was created
# by encoding original image.
#filename = webp_path.split('/').pop.split('.').first + '.webp'
#file = CarrierWave::SanitizedFile.new(
tempfile: webp_path,
filename: webp_path,
content_type: 'image/webp'
)
end
end

Related

Carrierwave: Move version name to end of filename, instead of front

Currently with Carrierwave, after uploading a file like foo.png when creating different versions like so:
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :fog
def store_dir
"#{model.class.to_s.underscore}/#{model.id}"
end
version :thumb do
process :resize_to_fit => [500, 500]
end
end
that results in the files being uploaded as:
thumb_foo.png
foo.png
I want to move "thumb" to the end of the filename for SEO reasons. Based on their docs here I added:
def full_filename(for_file)
if parent_name = super(for_file)
extension = File.extname(parent_name)
base_name = parent_name.chomp(extension)
[base_name, version_name].compact.join("_") + extension
end
end
def full_original_filename
parent_name = super
extension = File.extname(parent_name)
base_name = parent_name.chomp(extension)
[base_name, version_name].compact.join("_") + extension
end
The docs say this should result in:
foo_thumb.png
foo.png
However, I end up actually getting the following:
thumb_foo_thumb.png
foo.png
Any idea what I'm doing wrong?
Simply use #full_filename under the version block:
class AvatarUploaer < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :file
version :thumb do
process resize_to_fill: [50, 50]
def full_filename(for_file = model.logo.file)
parts = for_file.split('.')
extension = parts[-1]
name = parts[0...-1].join('.')
"#{name}_#{version_name}.#{extension}"
end
end
end
The result will be following:
/Users/user/app/uploads/1x1.gif
/Users/user/app/uploads/1x1_thumb.gif
More information in Wiki: How to: Migrate from one model to another
MVP example: https://gist.github.com/itsNikolay/2394f84f31db33d4c3dc6634068b0259
If you have a lot of versions, the accepted answer can get a little tedious.
I ended up overriding full_filename for everything instead of in each individual version definition. It works fine. This is for Carrierwave 1.0
photo_uploader.rb
# Override the filename of the uploaded files:
def full_filename(name)
"#{File.basename(name, '.*')}_#{version_name || 'original'}#{File.extname(name)}"
end
I'm using the built in File.basename and File.extname methods instead of doing it manually as seen in the accepted answer (although that's where I started and that code works fine too).
Note: I wanted to add "original" to the unversioned upload just so my directory listing looked cleaner. That part could be removed fairly easily.
foo_mobile.jpg
foo_original.jpg
foo_square.jpg
In the current version of CarrierWave if you have an uploader defined like this:
class LogoUploader < CarrierWave::Uploader::Base
# ...
def filename
"original_#{model.logo.file.extension}" if original_filename
end
version :small do
process :resize_to_fit => [190, 190]
process :convert => 'png'
end
version :icon do
process :resize_to_fill => [50, 50]
process :convert => 'png'
end
# ...
end
and attach a file name somefile.jpg, you will end up with files named original.jpg, original_small.png and original_icon.png respectively.

How can i retrieve uploaded image (base64) full url from Carrierwave upload file to Rackspace?

I just need to get the url of my image (base64) that just uploaded to Rackspace server via Carrierwave.
This is my controller now.
def update_with_image
user = current_user
uploader = PictureUploader.new
uploader.store!(user_update_params[:profile_image]) // base64 image like this 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2w...'
// How can i update user_update_params[:profile_image] with the successfully uploaded profile_image url?
if user.update_attributes(user_update_params)
# Handle a successful update.
render json: user, status: 200 ,serializer: UserSerializer
else
render json: { errors: user.errors }, status: 422
end
end
So after uploader.store!(user_update_params[:profile_image]) how can i get the url of that file?
Thanks!
You mean this ?
uploader = AvatarUploader.new
uploader.store!(my_file) # size: 1024x768
uploader.url # => '/url/to/my_file.png' # size: 800x600
uploader.thumb.url # => '/url/to/thumb_my_file.png' # size: 200x200
calling url method on the uploader should get you the URL. github
Update: Quoting from carrier wave GitHub
You can optionally include your CDN host name in the configuration. This is highly recommended, as without it every request requires a lookup of this information.
config.asset_host = "http://c000000.cdn.rackspacecloud.com"
In your uploader, set the storage to :fog
class AvatarUploader < CarrierWave::Uploader::Base
storage :fog
end
That's it! You can still use the CarrierWave::Uploader#url method to return the url to the file on Rackspace Cloud Files.

Attachment_fu rotate image in S3 storage Rails

How to rotate image in S3 storage, I'm using Attachment_fu and Amazon S3 , I want to implement a feature that lets users edit existing images by rotating it 90 degrees. I was wondering if anyone had played with this, or had any ideas. I'm currently using rmagick
to rotate the image in Rails directory. But in AWS S3 storage cannot rotate the image.
require 'RMagick'
def rotate
photo = Photo.find(params[:id])
image = Magick::ImageList.new(photo.file)
image = image.rotate(90)
image.write(photo.file)
end
I'm using this gem:
gem 'pothoven-attachment_fu'
gem 'aws-s3'
gem 'rmagick'
I solved this problem.
def update_photo
require 'RMagick'
require 'base64'
# get the image data in Photo table
#photo = Photo.where(id: params[:photo_id]).first
# get image path in S3 url and encode to Base64 binary
img_url = #photo.s3_url()
img_uri = URI.parse(img_url)
base64_image = Base64.encode64(open(img_uri) { |io| io.read })
image_file_name = File.basename(img_uri.path,File.extname(img_uri.path))
filename_path = "#{Rails.root.to_s}/tmp/#{image_file_name}"
# create tempfile in rails root/tmp folder and decode the Base64 binary into image file, then processing RMagick to rotate the image into desire image rotation.
#tempfile = Tempfile.new(filename_path)
#tempfile.binmode
#tempfile.write Base64.decode64(base64_image)
#tempfile.close
# rotate image using RMagick
Magick::Image.read(img_uri).first.rotate!(params[:photo_degree].to_i).write(#tempfile.path)
# for security we want the actual content type, not just what was passed in
# get mime type of image and passed into ActionDispatch type
content_type = `file --mime -b #{#tempfile.path}`.split(";")[0]
# we will also add the extension ourselves based on the above
# if it's not gif/jpeg/png, it will fail the validation in the upload model
extension = content_type.match(/gif|jpeg|png/).to_s
extension = "jpg" if extension == "jpeg"
# append filename to prevent browser image caching
filename_path += "_#{params[:photo_degree]}"
filename_path += ".#{extension}" if extension
# passed the data into Rails core ActionDispatch instead of file input object
file = ActionDispatch::Http::UploadedFile.new({
tempfile: #tempfile,
type: content_type,
filename: filename_path
})
# delete the existing image
#photo.destroy
# create new image
img_rotate = Photo.new()
img_rotate.id = #photo.id
img_rotate.uploaded_data = file
img_rotate.save
ensure
clean_tempfile
end
def clean_tempfile
if #tempfile
#tempfile.close
#tempfile.unlink
end
end

Carrierwave RMagick not removing transparency in convert to jpg

I'm trying to upload a PNG and save a bunch of thumbnails. The thumbnails should all be JPG and not have any transparency. Somehow the file is saved as jpg but it has transparency...
Here's my Uploader:
# encoding: utf-8
class WinePhotoUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
include CarrierWave::RMagick
# include CarrierWave::MiniMagick
# Choose what kind of storage to use for this uploader:
storage :file
# storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"assets/wines/#{version_name}"
end
CarrierWave::SanitizedFile.sanitize_regexp = /[^[:word:]\.\-\+]/
# Provide a default URL as a default if there hasn't been a file uploaded:
def default_url
# For Rails 3.1+ asset pipeline compatibility:
ActionController::Base.helpers.asset_path("pages/wines/#{version_name}/default.png")
end
process :for_all
process :convert => 'jpg'
version :croppable, :if => :new_upload? do
process :resize_and_pad => [1200, 1200, 'white']
end
version :show, :if => :new_upload? do
process :resize_to_fit => [nil, '425']
end
version :general, :from_version => :show, :if => :new_upload? do
process :resize_and_pad => [150, 375, 'white']
end
version :thumb, :from_version => :croppable, :if => :cropping? do
process :rotate_to => [-30]
process :crop_via_model
process :resize_to_fit => [150, 150]
end
def for_all
manipulate! do |img|
img.trim
img.gravity = Magick::CenterGravity
# I only use these two because this shit isn't working...these don't seem to help!
# Neither does the flatten method...even though other posts on stackoverflow.com say
# that it works.
img.background_color = 'white'
img.alpha Magick::OpaqueAlphaChannel
img.unsharp_mask 0.3, 0.3, 5, 0
img
end
end
def extend_to(w, h)
manipulate! do |img|
img.gravity = Magick::CenterGravity
img.extent w, h
img
end
end
def rotate_to(deg)
manipulate! do |img|
img.gravity = Magick::CenterGravity
img.distort Magick::ScaleRotateTranslateDistortion, deg
#img.repage # WTF?!?!?!? No repage method?!
img
end
end
def crop_via_model
manipulate! do |img|
img.gravity = Magick::CenterGravity
img.crop model.crop_x.to_i, model.crop_y.to_i, model.crop_w.to_i, model.crop_h.to_i
img
end
end
def flatten
manipulate! do |img|
img_list = Magick::ImageList.new
img_list.from_blob img.to_blob
img_list.new_image(img_list.first.columns, img_list.first.rows) { self.background_color = "white" } # Create new "layer" with white background and size of original image
img = img_list.reverse.flatten_images
img
end
end
def new_upload? picture
!model.cropping?
end
def cropping? picture
model.cropping?
end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_white_list
%w(jpg jpeg gif png tif)
end
# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
def filename
"#{ model.get_permalink(:normalized => true) }.jpg"
end
# Remove this when https://github.com/carrierwaveuploader/carrierwave/issues/1164 is solved.
def recreate_versions!(*versions)
if versions.any?
from_versions = versions.group_by { |v| self.class.versions[v][:options][:from_version] }
from_versions.each do |from, versions|
next if from.nil?
file = CarrierWave::SanitizedFile.new(self.versions[from.to_sym].file)
store_versions!(file, versions)
end
else
super(*versions)
end
end
end
Oh...and apparently carrierwave doesn't seem to want to log anything so I can't tell what it's doing.
There appears to be a quirk with imagemagick and order of operations when converting file types.
Full github issue can be found here: https://github.com/carrierwaveuploader/carrierwave/issues/133#issuecomment-615254
Basically manipulate is called once for every process you have. manipulate opens the current file path, makes changes then writes. Because of this it seems that any process lines that are called after the format conversion are performed on the original file, not the newly converted one.
In order to fix this, you either have to do all of your process operations within a single manipulate block or make sure that the conversion is the last process to run.
The example of this from the github issue is:
process :convert_and_scale
def convert_and_scale
manipulate! do |img|
img.format 'png'
img.resize '100x32'
img
end
end

Uploading files to s3 from local machine to s3 using carrierwave in rails

I am trying to upload the files from my local machine to amazon s3 using carrierwave. Actually I want to write the migration for the above operations. I need to move the images that are stored locally to amazon. Can anybody tell me how should I perform the above operations using the methods of carrierwave.
Btw I am also using Carrierwave_direct on top of carrierwave but I don't think that would affect my storage methods.
I executed uploader.store!(/local/path/to/file) but it fails with the following error:
You tried to assign a String or a Pathname to an uploader, for
security reasons, this is not allowed.
Is there any other way I can send in the path info in the method?
I also tried executing:
new_file.asset = File.open('full/path') #asset is where my uploader is mounted
In this case, when I try new_file.save!, it successfully saves but when I try to get the url by doin new_file.asset.url it shows empty. I don't know why
Heres my uploader:
module DirectUploader
extend ActiveSupport::Concern
included do
include CarrierWave::MimeTypes
include CarrierWave::MiniMagick
include CarrierWaveDirect::Uploader
include ActiveModel::Conversion
extend ActiveModel::Naming
process :set_content_type
end
module InstanceMethods
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
# override the url to return absolute url if available and
# revert back to standard functionality if it is not available
def url
if model.absolute_url.nil?
super
else
model.absolute_url
end
end
def filename
#random = Digest::MD5.hexdigest(model.latest_time.to_s)
"#{#random}.#{File.extname(original_filename)}" if original_filename
end
def policy_doc(options={})
options[:expiration] ||= self.class.upload_expiration
options[:max_file_size] ||= self.class.max_file_size
doc = {
'expiration' => Time.now.utc + options[:expiration],
'conditions' => [
["starts-with", "$utf8", ""],
["starts-with", "$authenticity_token", ""],
["starts-with", "$key", store_dir],
{"bucket" => fog_directory},
{"acl" => acl},
["content-length-range", 1, options[:max_file_size]]
]
}
doc['conditions'] << {"success_action_redirect" => success_action_redirect} if success_action_redirect
doc
end
def policy(options={})
Base64.encode64(policy_doc(options).to_json).gsub("\n","")
end
end
end
And there is no problem in carrierwave configuration because I can upload the files using form/html. Its just that I am finding problems during migration.
Have you tried:
uploader.store!(File.new('/local/path/to/file'))
When I'm running tests, I use:
uploader.store! File.open(Rails.root.join("spec/support/file.png"))

Resources