Coder Social home page Coder Social logo

igorkasyanchuk / omg_image Goto Github PK

View Code? Open in Web Editor NEW
28.0 3.0 2.0 276 KB

Generate PNG previews for HTML snippets (html/css/js). Any complexity.

Home Page: https://www.railsjazz.com/

License: MIT License

Ruby 63.57% JavaScript 4.22% CSS 3.09% HTML 29.12%
rails chrome-headless screenshot ruby

omg_image's Introduction

omg_image

RailsJazz https://www.patreon.com/igorkasyanchuk Listed on OpenSource-Heroes.com

Let's start with sample of what this gem can do (below are the images of generated previews):

Demo: https://www.youtube.com/watch?v=Lso-B_fayhw

Sample

If you need to generate complex images, previews, charts or basically represent any HTML snippet - this gem could help.

It's using a Chrome(headless) to convert any HTML to PNG.

If you want to try, you just need a 5 min to see how it works. Gem is comming with an examples which you can modify and use in your real app.

Usage

  • add this gem to Gemfile - gem "omg_image"
  • make sure you have chrome installed (google-chrome --version)
  • execute rails g omg in app
  • execute rake db:migrate
  • edit sample template app/omg/simple.html.erb
  • open any view, for example you have app/views/home/index.html.erb and put:
  <%= image_tag OmgImage::Processor.new('entity', key: 'xxx', title: "OMG,<br/>this looks interesting!", tags: ['This', 'is', 'a', 'sample'], description: "Change me please", size: '600,300').cached_or_new %>
  <br/>
  <%= image_tag OmgImage::Processor.new('entity', title: "OMG,<br/>this looks interesting!", description: "Small version", size: '400,200').cached_or_new(regenerate: true) %>
  <br/>
  <%= image_tag OmgImage::Processor.new('square', title: "Real-time generation", size: '200,200').cached_or_new %>
  • refresh page and see images
  • edit/create new previews and use them in any place of your app.

Also, for example you can do it on generate images directly in the models:

class Post < ApplicationRecord
  has_one_attached :preview
  # just an example
  def Post.create_new_preview
    processor = ::OmgImage::Processor.new('entity', title: "OMG,<br/>this is created from model", description: "Small version", size: '400,200')
    processor.with_screenshot do |screenshot|
      post = Post.new
      post.preview.attach(io: File.open(screenshot.path), filename: "image.png", content_type: "image/png")
      post.save!
    end
  end
end

To create a new template - basically create a new file in app/omg/<name>.html.erb. Put any HTML/CSS into it. And use as described above.

Requirements

  • Rails App 5+ and Active Storage
  • Google chrome (headless)

Installation

Add this line to your application's Gemfile:

gem 'omg_image'

And then execute:

$ bundle

Features

  • you can generate images with any complexity (you just need to know html/css)
  • store cached versions of preview by key and you can obtain them by OmgImage::Image.find_by(key: key)
  • generate images in background jobs or console applications
  • caching for previews by key
  • ability to refresh preview by key

Options

  • .cached_or_new(regenerate: true) pass this option with true value if you want to regenerate image

Google Chrome Installation

  • sudo apt install gdebi-core
  • wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
  • sudo gdebi google-chrome-stable_current_amd64.deb
  • verify chrome is installed google-chrome --version

More about Chrome

Issues

  • if you process too many requests and because of timeouts dead processes may appear. To kill them parents_of_dead_kids=$(ps -ef | grep [d]efunct | awk '{print $3}' | sort | uniq | egrep -v '^1$'); echo "$parents_of_dead_kids" | xargs kill

TODO

  • ability to configure app (path to chrome for example)
  • more samples (with layout)
  • wiki pages with documentation and samples
  • ability to preview templates directly by URL
  • tests/specs
  • options to delay rendering (could be useful if use JS libraries or external assets)
  • support remote URL's to render (so it would possible to capture screenshots for example)

Contributing

You are welcome to contribute.

Inspirations

I've noticed that in a past that articles on dev.to site without images when you sending a link in skype or sharing in FB have have nice preview, so I've implemented similar solution :)

License

The gem is available as open source under the terms of the MIT License.

omg_image's People

Contributors

igorkasyanchuk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

sachiotomita

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.