Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to crop image using mini_magick

Tags:

minimagick

I am using mini_magick as the interface of utilizing ImageMagick to manipulate some image. The resize works fine. But when I want to crop some image:

img = MiniMagick::Image.open(url)
img.crop('200x800!')

It constantly prompts No such file

No such file or directory - /var/folders/c4/mqlwdx_d3kj6hqcnvbjr9mn80000gn/T/mini_magick20120504-11111-16kayc1.png
like image 324
nil Avatar asked May 04 '12 11:05

nil


2 Answers

Ah, I was searching with wrong key phrase I guess. The right answer come to me when I search minimagick instead of mini_magick. Undefined Method crop! Using Carrierwave with MiniMagick on rails 3.1.3 especially this answer https://stackoverflow.com/a/9961434/179691

I did know the stuff about mini_magick is just a wrapping of mogrify and so. The cause of my problem is that -crop accepts full formatted geometry only. so I changed the expression to:

img.crop('200x800+0+0')

and that works.

like image 80
nil Avatar answered Oct 25 '22 00:10

nil


Just in case if anyone is using Carrierwave to crop and upload image directly to Amazon S3, the correct way to do for me is the following:

image_uploader.rb

url=model.remote_image_url 
crop_params="#{w}x#{h}+#{x}+#{y}"
manipulate! do |img|
  img = MiniMagick::Image.open(url)
  img.crop(crop_params)
  img = yield(img) if block_given?
  img
end

The reason I'm adding img = MiniMagick::Image.open(url) is because if I don't specify my own image, it would throw me the following error:

mogrify.im6: geometry does not contain image `/tmp/mini_magick20150811-15523-ac8go1.jpeg'

I think it's some default temporary path that mini_magick would try to find the image, but since the image is held remotely in S3, it couldn't find it.

like image 30
Antonio Jha Avatar answered Oct 25 '22 02:10

Antonio Jha