Manish Shrivastava
Manish Shrivastava

Reputation: 32050

How to create an Image in rails ? ( from as imagecreate () in php)

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?

Upvotes: 4

Views: 4366

Answers (3)

Oleksandr Skrypnyk
Oleksandr Skrypnyk

Reputation: 2820

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

Upvotes: 10

Derek Lucas
Derek Lucas

Reputation: 488

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.

Upvotes: 3

Anand Shah
Anand Shah

Reputation: 14913

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

Upvotes: 2

Related Questions