rails paperclip creating thumb image with smaller image - ruby-on-rails

I am using paperclip in rails to create different size images as follows
has_attached_file :upload,styles: { medium: ["500x300>",:jpg], thumb: ["150x100>",:jpg] }
Ill show my original image and also created thumbnail image here for better understanding
My original image is this
My thumb image created is
I am unable to understand what is happening but this works fine with large size images. What wrong happens here. How to correct it.

From the ImageMagick docs:
Use > to shrink an image only if its dimension(s) are larger
than the corresponding width and/or height arguments.
As a result, your original 256x256 image won't create your medium size, which defines 500x300
I'm not sure if this has anything do with your problem, but I guess that the sizes and operators messed up something. I bet that playing around with other arguments, such as ! and ^ will do.

Related

Force user to crop/upload an image to a certain size? Preferably using filepicker

I have users uploading images using filepicker, but I want them to have to upload an image of a certain size (and crop if the image is too big). I could cut it myself, but then it won't look good. Ideally, the user would crop it themselves.
I've tried this page: https://www.filepicker.com/documentation/file-ingestion/widgets/pick?v=v2 and I've tried various options but nothing seems to work quite well.
data-fp-image-min doesn't prevent users from uploading smaller images. data-fp-crop-force along with data-fp-crop-max and data-fp-crop-min doesn't do the trick either.
I'm open to using other image uploading libraries, but I like using filepicker. Seems like this is something other people would have run into.
I'm using rails btw.
From the docs:
data-fp-image-min - Images smaller than the specified dimensions will be upscaled to the minimum size.
So it doesn't really prevent users from uploading smaller images.
data-fp-crop-max and data-fp-crop-min specifies the maximum and minimum dimensions of the crop area so it won't give you specific dimensions.
I would recommend you to:
Set data-fp-crop-ratio - Specify the crop area height to width ratio. User will be able to adjust the crop area for each photo with desired ratio.
Set data-fp-crop-force="true" - User could not skip cropping image.
Then resize image to specific height or width.
This will result, you will always get the image with the desired dimensions.
Example for 150 x 200 image output:
Html widget:
<input type="filepicker"
data-fp-crop-ratio="3/4"
data-fp-crop-force="true"
mimetype="image/*"
onchange="window.upload(event)"
data-fp-apikey="APUGwDkkSvqNr9Y3KD4tAz" />
Javascript:
window.upload = function(event){
console.log(JSON.stringify(event.fpfile));
var listElem = document.createElement("li");
var image = document.createElement("img");
/*
set w=150 (width) conversion option
so all images would be 150x200
and crop_first option to make sure the image is cropped
before any other conversion parameters are executed.
*/
image.setAttribute('src', event.fpfile.url + '&w=150&crop_first=true');
listElem.appendChild(image);
document.getElementById('results').appendChild(listElem);
};
Here is working solution: http://jsfiddle.net/krystiangw/9o9ebddL/

Resizing image with ImageMagick and Ruby on Rails

I have been trying to use Imagemagick to resize images uploaded by a user as a square.
Currently, I am using the ! like so - 640x640!
This works fine if the image i feed it is a resolution of 640x640 or bigger - it resizes and makes it into a square as expected.
The problem is that if either the height or width of the image is smaller than 640, then it wont square it out. For instance if the image is 480x600, it wont do anything to the image. Similarly if the image is 680x456 then it will resize it to 640x456
How can i make it so that it will always square the image to a maximum size of 640x640? If the image is greater than 640x640, then i want it to resize to 640x640. If the image is smaller than 640x640, i.e. 480x600, i want it to resize to 480x480
I'm doing it in rails, within the paperclip attachment definition, like this:
has_attached_file :avatar, :styles => { :medium => "640x640!", :thumb => "150x150!" }, :default_url => "/images/:style/missing.png"
First, Require the library
require 'rubygems'
require 'mini_magick'
Second, You have to get the image first
image = MiniMagick::Image.open("PathOfTheImage")
Next, resize it
image.resize "640x640!"
finally, save the image
image.write "output.png"
and use the output image afterwards.
Making it always square may loose images's aspect ratio. Here are couple of ways to resize the image.
resize_to_limit
Resize the image to fit within the specified dimensions while retaining the original aspect ratio. Will only resize the image if it is larger than the specified dimensions. The resulting image may be shorter or narrower than specified in the smaller dimension but will not be larger than the specified values.
resize_to_fit
Resize the image to fit within the specified dimensions while retaining the original aspect ratio. The image may be shorter or narrower than specified in the smaller dimension but will not be larger than the specified values.
resize_to_fill
Resize the image to fit within the specified dimensions while retaining the aspect ratio of the original image. If necessary, crop the image in the larger dimension.
This one is Image Magick Way
http://www.imagemagick.org/discourse-server/viewtopic.php?t=26196#p115047

Paperclip - Wrong image orientation

I'm using Paperclip 4.1.1 for images uploading. I store three sizes, original, medium and thumbnail. When I try to show the original images, some of them appear rotated to the left, and it happens randomly, because some others are displayed correctly. This does not happen with medium and thumbnail sizes. The funny fact is that if I open any original image url on a browser, it always displays correctly. Any thoughts?
EDIT:
According to this comment in the Paperclip Railscast, this behavior doesn't occur on processed images. As a workaround, I added a new Paperclip style big enough to meet my requirements.
Check this How to set Paperclip to process original image? this will help.
Adding original: {convert_options: '-strip'} in styles help

Carrierwave - Processed image too big in size

I got a Carrierwave uploader and process images like this:
version :thumbnail do
process :resize_to_model
process :quality => 90
end
def resize_to_model
thumbs_size = model.thumbnail_size
resize_to_fill thumbs_size[:width], thumbs_size[:height]
end
However, after processing an image which was 1024x724px and is 214x151px afterwards the file size only went down from 2,1mb to 1,8mb. I think 1,8mb really is a lot for that size. Can I do something about that? Even with 90% quality the image should be like maybe 100kb or not?
Before someone asks, the rest works perfect. No errors, the size in px is right and everything else is also fine.
Edit: I forgot to mention I Use rmagick(resize_to_fill). Is that a reason maybe?
The difference between 100% and 90% quality is so small and the storage space savings is negligible. If you are truly just using this version as a thumbnail you should look at using a much lower quality, say 60% or 40%.
If you are concerned about making sure the quality is still "good enough" then you could also look at different compression techniques. The process used to provide #2x images for Retina displays can be used in this instance. A great resource is available in the Filament Group's article Compressive Images.
The tl;dr version is basically, use the original (or near original) size of the image but drastically reduce the image quality (to 0-20%). Then, when using the reduced quality image be sure to provide width and height attributes in the <img> element to resize it down to the thumbnail size. Because the image will be scaled down you will not see the reduction in quality of the "thumbnail" image.

Resize lots of images (with different proportions) so each image fit in a predefined rectangle

I have about 60 images uploaded to my site. I'd like to resize them all so they fit in a 150px × 100px box. No cropping, just scaling, but it should preserve the original proportions.
I'd prefer a simple solution using, say the ImageMagick convert command. A solution for a single arbitrary image is perfectly fine. (I know how to loop or use find in bash.)
The images are of different types (eps, jpg, ps etc) so a solution that at the same time rasterizes the image would be awesome.
Ok, it seems it was easier than I expected:
convert image.eps -scale "150x100>" file_resized.jpg
did the trick. Reference page.

Resources