[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Another IO.popen question ...

Adam Groves

11/16/2006 10:04:00 PM

Hi,

I'm wanting to create thumbnails of pdfs in png format and send them off
to be saved. I've come up with this so far:

IO.popen("curl #{some_uri} | convert -resize 700x700 pdf:-[0]
png:-") {|f| do_something_with_this_file}

Which works a treat. (In case you didn't know, convert is a command line
tool for ImageMagick).

What I'd really like to do though is pass on a File object to convert:

file = File.open("mypdf.pdf")

IO.popen("#{file} | convert -resize 700x700 pdf:-[0] png:-") {|f|
do_something_with_this_file}

This obviously doesn't work but I hope illustrates what I'm wanting to
acheive - namely to pass 'file' to convert it to a png which is read
back into ruby and uploaded to my file repository on S3.

By the way, this IS for a rails application, but it seems basically like
a pure ruby question to me.


Regards

Adam

--
Posted via http://www.ruby-....

12 Answers

Joel VanderWerf

11/16/2006 10:10:00 PM

0

Adam Groves wrote:
> Hi,
>
> I'm wanting to create thumbnails of pdfs in png format and send them off
> to be saved. I've come up with this so far:
>
> IO.popen("curl #{some_uri} | convert -resize 700x700 pdf:-[0]
> png:-") {|f| do_something_with_this_file}
>
> Which works a treat. (In case you didn't know, convert is a command line
> tool for ImageMagick).
>
> What I'd really like to do though is pass on a File object to convert:
>
> file = File.open("mypdf.pdf")
>
> IO.popen("#{file} | convert -resize 700x700 pdf:-[0] png:-") {|f|
> do_something_with_this_file}

would this work?

file = "mypdf.pdf"
IO.popen("cat #{file} | ...")

or simply

IO.popen("convert -resize 700x700 #{file} png:-")

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Tim Hunter

11/16/2006 10:16:00 PM

0

Adam Groves wrote:
> Hi,
>
> I'm wanting to create thumbnails of pdfs in png format and send them off
> to be saved. I've come up with this so far:
>
> IO.popen("curl #{some_uri} | convert -resize 700x700 pdf:-[0]
> png:-") {|f| do_something_with_this_file}
>
> Which works a treat. (In case you didn't know, convert is a command line
> tool for ImageMagick).
>
> What I'd really like to do though is pass on a File object to convert:
>
> file = File.open("mypdf.pdf")
>
> IO.popen("#{file} | convert -resize 700x700 pdf:-[0] png:-") {|f|
> do_something_with_this_file}
>
> This obviously doesn't work but I hope illustrates what I'm wanting to
> acheive - namely to pass 'file' to convert it to a png which is read
> back into ruby and uploaded to my file repository on S3.
>
> By the way, this IS for a rails application, but it seems basically like
> a pure ruby question to me.
>
>
> Regards
>
> Adam
>
>
Consider RMagick: http://rmagick.rub...

The Magick::Image.read method accepts an open Ruby file object as an
argument.

http://www.simplesystems.org/RMagick/doc/image1...


Adam Groves

11/16/2006 10:23:00 PM

0

Hi Joel,

thanks for the speedy reply! The problem is, I need to pass on a file
object (which has been uploaded from a web page form) to 'convert' and
not just a path. I should have made this clearer in my original post.

Any ideas?

--
Posted via http://www.ruby-....

Adam Groves

11/16/2006 10:30:00 PM

0

Timothy Hunter wrote:
> Adam Groves wrote:
>>
>>
>> By the way, this IS for a rails application, but it seems basically like
>> a pure ruby question to me.
>>
>>
>> Regards
>>
>> Adam
>>
>>
> Consider RMagick: http://rmagick.rub...
>
> The Magick::Image.read method accepts an open Ruby file object as an
> argument.
>
> http://www.simplesystems.org/RMagick/doc/image1...


hmm.. Maybe you're right Tim. I was just wanting to drop to the command
line to do this and thought there must be a way of accomplishing this
without resorting to RMagick. Not that I have anything against it.

Is there really no other way of passing on a Ruby file object
imagemagick other than through RMagick?


--
Posted via http://www.ruby-....

Tim Hunter

11/16/2006 10:36:00 PM

0

Adam Groves wrote:
> hmm.. Maybe you're right Tim. I was just wanting to drop to the command
> line to do this and thought there must be a way of accomplishing this
> without resorting to RMagick. Not that I have anything against it.
>
> Is there really no other way of passing on a Ruby file object
> imagemagick other than through RMagick?
>
Consider that the convert command only accepts string options. It knows
nothing about Ruby.

Ara.T.Howard

11/16/2006 10:40:00 PM

0

Ara.T.Howard

11/16/2006 10:48:00 PM

0

Joel VanderWerf

11/16/2006 11:03:00 PM

0

Adam Groves wrote:
> Hi Joel,
>
> thanks for the speedy reply! The problem is, I need to pass on a file
> object (which has been uploaded from a web page form) to 'convert' and
> not just a path. I should have made this clearer in my original post.
>
> Any ideas?
>

Untested, but maybe something like this?

file = File.open("mypdf.pdf")
IO.popen("convert -resize 700x700 pdf:-[0] png:-") do |f|
while (data=file.read(N))
f.write data
end
end

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Joel VanderWerf

11/16/2006 11:09:00 PM

0

Joel VanderWerf wrote:
> Adam Groves wrote:
>> Hi Joel,
>>
>> thanks for the speedy reply! The problem is, I need to pass on a file
>> object (which has been uploaded from a web page form) to 'convert' and
>> not just a path. I should have made this clearer in my original post.
>>
>> Any ideas?
>>
>
> Untested, but maybe something like this?
>
> file = File.open("mypdf.pdf")
> IO.popen("convert -resize 700x700 pdf:-[0] png:-") do |f|
^^^^^
...this bit is the problem. So Ara's r+ pipe would be necessary, unless
you can put a filename here instead.

Also, I forgot the "w" mode.

> while (data=file.read(N))
> f.write data
> end
> end
>


--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Ara.T.Howard

11/16/2006 11:44:00 PM

0