I need to be able to quickly convert an image (inside a rails controller) so that the hosting company using managing our application can quickly test at any time to ensure that rmagick is not only successfully installed, but can be called throgh the rails stiack, what is the quickest clean code I can use to do this?
我需要能够快速转换图像(在rails控制器内),以便托管公司使用管理我们的应用程序可以随时快速测试以确保rmagick不仅成功安装,而且可以称为throgh the rails stiack,我可以使用什么最快的清洁代码来做到这一点?
4 个解决方案
#1
14
require 'RMagick'
image = Magick::Image.new(110, 30){ self.background_color = 'white' }
image.write('/tmp/test.jpg')
#2
4
I wanted to do this so that I can easily hit it with a web browser, as I'm deployng to managed servers, which I do not have shell access onto (for increased security).
我想这样做,以便我可以轻松地使用Web浏览器,因为我已经部署到托管服务器,我没有shell访问(为了提高安全性)。
So this is what I did
所以这就是我所做的
class DiagnosticsController < ApplicationController
require 'RMagick'
def rmagick
images_path = "public/images"
file_name = "rmagick_generated_thumb.jpg"
file_path = images_path + "/"+ file_name
File.delete file_path if File.exists? file_path
img = Magick::Image.read("lib/sample_images/magic.jpg").first
thumb = img.scale(0.25)
@path = file_name
thumb.write file_path
end
end #------
and then in rmagick.html.erb
然后在rmagick.html.erb中
<%= image_tag @path %>
Now I can hit the controller, and if I see an image, I know rmagic is installed.
现在我可以点击控制器,如果我看到图像,我知道安装了rmagic。
#3
0
I'd log on to the server and try out your code in script/console. This will still go through the rails stack, but will allow you to quickly check that your code works the way you expect and that RMagick and ImageMagick are correctly installed without having to deploy anything.
我登录到服务器并在脚本/控制台中试用你的代码。这仍将通过rails堆栈,但允许您快速检查您的代码是否按预期方式工作,并且无需部署任何内容即可正确安装RMagick和ImageMagick。
When the time comes to write your actual code, I'd suggest putting the image conversion code inside a model, so you can call it outside the context of a controller.
在编写实际代码的时候,我建议将图像转换代码放在模型中,这样你就可以在控制器的上下文之外调用它。
#4
0
Use script/console, and call code in a model or a controller that does something like the following:
使用脚本/控制台,并在模型或控制器中调用代码,执行类似以下操作:
require 'RMagick'
include Magick
img = ImageList.new('myfile.jpg')
img.crop(0,0,10,10) # or whatever
img.write('mycroppedfile.jpg')
#1
14
require 'RMagick'
image = Magick::Image.new(110, 30){ self.background_color = 'white' }
image.write('/tmp/test.jpg')
#2
4
I wanted to do this so that I can easily hit it with a web browser, as I'm deployng to managed servers, which I do not have shell access onto (for increased security).
我想这样做,以便我可以轻松地使用Web浏览器,因为我已经部署到托管服务器,我没有shell访问(为了提高安全性)。
So this is what I did
所以这就是我所做的
class DiagnosticsController < ApplicationController
require 'RMagick'
def rmagick
images_path = "public/images"
file_name = "rmagick_generated_thumb.jpg"
file_path = images_path + "/"+ file_name
File.delete file_path if File.exists? file_path
img = Magick::Image.read("lib/sample_images/magic.jpg").first
thumb = img.scale(0.25)
@path = file_name
thumb.write file_path
end
end #------
and then in rmagick.html.erb
然后在rmagick.html.erb中
<%= image_tag @path %>
Now I can hit the controller, and if I see an image, I know rmagic is installed.
现在我可以点击控制器,如果我看到图像,我知道安装了rmagic。
#3
0
I'd log on to the server and try out your code in script/console. This will still go through the rails stack, but will allow you to quickly check that your code works the way you expect and that RMagick and ImageMagick are correctly installed without having to deploy anything.
我登录到服务器并在脚本/控制台中试用你的代码。这仍将通过rails堆栈,但允许您快速检查您的代码是否按预期方式工作,并且无需部署任何内容即可正确安装RMagick和ImageMagick。
When the time comes to write your actual code, I'd suggest putting the image conversion code inside a model, so you can call it outside the context of a controller.
在编写实际代码的时候,我建议将图像转换代码放在模型中,这样你就可以在控制器的上下文之外调用它。
#4
0
Use script/console, and call code in a model or a controller that does something like the following:
使用脚本/控制台,并在模型或控制器中调用代码,执行类似以下操作:
require 'RMagick'
include Magick
img = ImageList.new('myfile.jpg')
img.crop(0,0,10,10) # or whatever
img.write('mycroppedfile.jpg')