Cropping image with ImageScience - ruby

ImageScience is cool and light. I am using it in my sinatra app. But I can't understand how can I crop image with not square form and how can I make thumbnail with two dimensions.
As I found on ImageScience site:
ImageScience.with_image(file) do |img|
img.cropped_thumbnail(100) do |thumb|
thumb.save "#{file}_cropped.png"
end
img.thumbnail(100) do |thumb|
thumb.save "#{file}_thumb.png"
end
img.resize(100, 150) do |img2|
img2.save "#{file}_resize.png"
end
end
I can crop thumb and resize thumb only with ONE dimension but I want to use two, as in RMagick. For example I want to crop 100x200px box from image, or I want to make thumbnail with width or height not bigger then 300 (width) or 500 (height) pixels.

Use Devil instead:
Devil.with_image("horse.png") do |img|
img.crop(0, 0, 100, 100)
img.resize2(500, 500)
img.save("horse_resized.jpg", :quality => 85)
end

Wow, I've looked into ImageScience sources and found great method with_crop(left, top, right, bottom) which helped me with my problem.
http://seattlerb.rubyforge.org/image_science/ImageScience.html

Related

MiniMagick Resize Image

I'm trying to use MiniMagick to resize 2 images and overlay one on top of the other. Heres the code I am using
require "mini_magick"
first_image = MiniMagick::Image.new("spider.jpg")
first_image = first_image.resize("250x250")
second_image = MiniMagick::Image.new("q.png")
second_image = second_image.resize("250x250")
result = first_image.composite(second_image) do |c|
c.compose "Over" # OverCompositeOp
c.gravity "center"
# c.resize("250x250")
end
result.write "output.jpg"
This overlays the images but neither is resized and the overlay image ends up awkwardly cropped. Ive tried making both the same size, making the bigger overlay image smaller and the smaller image bigger, but none seem to work. Any advice would be highly appreciated.

how to scale image by percentage by carrierwave

I want to generate images in 25%, 50%, 75% size from original image, but seems carrierwave's resize_to_fill/fit not support percentage. Anyone knows how to do like this?
Thanks.
After some research, i found one solution:
process :store_dimensions
version :r_3x do
process :resize_to_fit_by_percentage => 0.75
end
private
def resize_to_fit_by_percentage(percentage)
resize_to_fit model.width*percentage, nil
end
def store_dimensions
if file && model
model.width, model.height = ::MiniMagick::Image.open(file.file)[:dimensions]
end
end
Firstly get the dimension of the uploaded image, then define a custom resize method(here is resize_to_fi_by_percentage), and resize image in this method like the code does.

Rails + Carrierwave + RMagick : Crop only if image is large

I am using carrier-wave to upload images. On upload I am creating thumbnails for the image which is done using Rmagick method, resize_to_fill like below.
version :thumb do
process :resize_to_fill=> [150, 150]
end
Here is output of all the RMagick methods carrierwave supports (none of which I want):
:resize_to_fill => [150,150]
This works fine on larger images but my smaller images are enlarged to 150 x 150.
:resize_to_fit => [150,150]
Again it was resized, I want it left alone!
:resize_to_limit => [150,150]
This one leaves it as is, but larger images are not cropped. They are resized to keep the aspect ratio.
Here is the result I want and how my small and larger images should look.
How do this? I want smaller images to be left alone and crop only larger images to 150 x 150. Is there another method or options I can pass to resize_to_fill?
I solved it by modifying :resize_to_fill carrierwave method as described in their code here.
I just made a new method with the same code with a check to see if the uploaded image is smaller. Here is the new method:
def resize_to_fill_modfied(width, height, gravity=::Magick::CenterGravity)
manipulate! do |img|
img.crop_resized!(width, height, gravity) unless (img.columns <= width && img.rows <= height)
img = yield(img) if block_given?
img
end
end
Does exactly what I want now.

Wordpress 3.2.1 Featured Image Size and Crop

I need help finding where and how to change the size and crop of a featured image within a post. I've looked in functions.php and init.php, but with no success. I'm using a child theme of the inLine theme.
The current height is set for 130px.
I've tried changing the css, but that only stretches the image.
Do this in functions.php:
add_image_size( 'name-of-your-image-size', 600, 300, true );
Or if you'd rather use a plugin-
http://wordpress.org/extend/plugins/additional-image-sizes-zui/
And then in your post, retrieve it like so in your single.php or loop:
<?php the_post_thumbnail('name-of-your-image-size'); ?>
In your functions.php, paste one of these and adjust pixel width and height to your liking:
set_post_thumbnail_size( 100, 100 ); // 100 pixels wide by 100 pixels tall, box resize mode
OR:
set_post_thumbnail_size( 100, 100, true ); // 100 pixels wide by 100 pixels tall, hard crop mode

rmagick auto scaling with proportions

Is there a way for me to scale and image in rmagick where i set the width, and the height auto scales so the image contain the same proportions?
I use the resize_to_fit method, which will use the parameters supplied as the maximum width/height, but will keep the aspect ration. So, something like this:
#scaled = #image.resize_to_fit 640 640
That will ensure that either the width or height is no greater than 640, but will not stretch the image making it look funny. So, you might end up with 640x480 or 480x640. There is also a resize_to_fit! method that converts in place
If you want to resize to a given width without respect to a bounding box, you will have to write a helper function. Something like this:
#img = Magick::Image::read(file_name).first
def resize_by_width image new_width
#new_height = new_width * image.x_resolution.to_f / image.y_resolution.to_f
new_image = image.scale(new_width, new_height)
return new_image
end
#resized = resize_by_width #img 1024
Hope that helps!

Resources