Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 865
Manish Shrivastava Avatar asked Dec 16 '22 05:12

Manish Shrivastava


2 Answers

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

like image 84
Oleksandr Skrypnyk Avatar answered Mar 10 '23 04:03

Oleksandr Skrypnyk


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.

like image 31
Derek Lucas Avatar answered Mar 10 '23 06:03

Derek Lucas