当前位置: 动力学知识库 > 问答 > 编程问答 >

image - IMGKit/wkhtmltoimage in a Rails 3 app. Meta tag options are not working. Complete code example included

问题描述:

Since there are not many code samples for IMGKit, I posted mine below.

I've studied the IMGKit documentation which says you can pass options to wkhtmltoimage via meta tags. So I created a page with the following meta tag which is not working:

<!DOCTYPE html>

<html>

<head>

...

<meta name="imgkit-height" content="420" />

...

</head>

<body>

...

</body>

</html>

网友答案:

There have been 127 views over two weeks without a comment or answer so I worked my tail off to get the answer. In addition, because there is so little out there on this gem I thought I'd add my whole process and code as a sample.

Installation for Development: MAC OSX

  1. 'gem install imgkit'
  2. 'bundle install'
  3. installing wkhtmltoimage was a little trickier because I use rvm. The 'rvmsudo' method of installation didn't work for me. I wound up downloading the wkhtmltoimage-OSX-0.10.0_rc2-static.tar.bz2 for Mac OSX here, unzipped it and copied the wkhtmltoimage file to usr/local/bin. Our server utilizes Ubuntu... hoping that is going to go ok... I'll let you know.
  4. I tested wkhtmltoimage with the command wkhtmltoimage http://www.google.com test.jpg and everything worked fine.
  5. Per the IMGKit documentation I added the mime types for png and jpeg.

Installation for Production: Ubuntu 10.04 LTS

  1. 'gem install imgkit'
  2. 'bundle install'
  3. 'sudo imgkit --install-wkhtmltoimage' didn't fly. There are AMD64 and i386 versions of wkhtmltoimage. I used cat /proc/cpuinfo to find out my server CPU was Intel so installed the i386 version manually to /usr/local/bin. I changed the permissions on the wkhtmltoimage-i386 file to allow execution. Also, for IMGKit to recognize the file I had to rename the file, dropping off the "-i386".

Controller that calls IMGKit Functions.

  def email_profile
    ...
    filename = current_user.username + "_" + cur_date + '.jpg'

    kit = IMGKit.new("https://www.myapp.com/applet/?auth_token=" + token)
    img = kit.to_img(:jpg)
    file = kit.to_file(Rails.root + "public/profile_images/" + filename)
    ...
  end

"Applet" Controller

This view never gets rendered in a browser. It is exclusively for IMGKit / wkhtmltoimage to read. Note a different rails layout file is rendered:

class AppletController < ApplicationController
    def index
        render(:layout => "layouts/applet_lo")
    end
end

Layout File

The layout file has new meta tags that call wkhtmltoimage options to crop the image through the IMGKit gem. It still does not work. The author of the gem wrote back and said this feature is finicky. I am using minimagick to crop until this is fixed.

<!DOCTYPE html>
<html>
    <head>
    <meta name="imgkit-crop-h" content="990">
    <meta name="imgkit-crop-w" content="635">
    </head>
    <body>
        <%= yield %>
    </body>
</html>
分享给朋友:
您可能感兴趣的文章:
随机阅读: