如何在rails中创建图像? (来自 php 中的 imagecreate())

发布于 2024-12-25 20:00:29 字数 366 浏览 3 评论 0原文

我需要从 imagemagick/rmagick 库创建图像,我应该怎么做? 就像在 php 中一样,它是使用 GD 库完成的,如下所示。

 <?php 
      header ("Content-type: image/png"); 
      $handle = ImageCreate (130, 50) or die ("Cannot Create image"); 
      $bg_color = ImageColorAllocate ($handle, 255, 0, 0); 
      ImagePng ($handle); 
  ?> 

在输入中,我已经给出了图像颜色、区域标签的图像坐标……有什么想法吗?

I need to create an image from the imagemagick/rmagick library, How should I do that?
as in php it was done like below with GD library.

 <?php 
      header ("Content-type: image/png"); 
      $handle = ImageCreate (130, 50) or die ("Cannot Create image"); 
      $bg_color = ImageColorAllocate ($handle, 255, 0, 0); 
      ImagePng ($handle); 
  ?> 

In input, I have given image color, image co-ordinates of area tag like that..any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

下雨或天晴 2025-01-01 20:00:29

您需要在系统中安装 ImageMagickrmagick gem,然后:

image = Magick::Image.new(130, 50)
image.background_color = 'red'
image.write('someimage.png')

确保您已经编译了支持 PNG 的 ImageMagick。

要发送图像,您可以执行下一步操作:

send_data image, :type => 'image/png', :disposition => 'inline'

那么您不需要保存图像,除非您想缓存它。

您可以在此处找到有关使用情况的更多信息

You need ImageMagick installed in your system and rmagick gem, then:

image = Magick::Image.new(130, 50)
image.background_color = 'red'
image.write('someimage.png')

Make sure, you have ImageMagick compiled with PNG support.

To send your image, you may do next thing:

send_data image, :type => 'image/png', :disposition => 'inline'

Then you don't need to save an image unless you want to cache it.

More information about usage you can find here

知足的幸福 2025-01-01 20:00:29

这是我用来动态创建内联图像的方法:

@placeholder = Magick::Image.new(@book.width, @book.height)
@placeholder.format = "png"
@placeholder = "data:image/png;base64,#{Base64.encode64 (@placeholder.to_blob)}"

并且:

<img src="<%= @placeholder %>" />

它并不漂亮,但目前可以使用。

Here's what I used to create an inline image on the fly:

@placeholder = Magick::Image.new(@book.width, @book.height)
@placeholder.format = "png"
@placeholder = "data:image/png;base64,#{Base64.encode64 (@placeholder.to_blob)}"

And:

<img src="<%= @placeholder %>" />

It's not pretty, but it works for now.

对你的占有欲 2025-01-01 20:00:29

http://www.imagemagick.org/RMagick/doc/usage.html#reading

require 'RMagick'
include Magick
# Create a 100x100 red image.
f = Image.new(100,100) { self.background_color = "red" }
f.display
exit

http://www.imagemagick.org/RMagick/doc/usage.html#reading

require 'RMagick'
include Magick
# Create a 100x100 red image.
f = Image.new(100,100) { self.background_color = "red" }
f.display
exit
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文