facebook_cover_resize 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +60 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/LICENSE.txt +21 -0
- data/README.md +102 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/facebook_cover_resize.gemspec +36 -0
- data/lib/facebook_cover_resize/railtie.rb +8 -0
- data/lib/facebook_cover_resize/version.rb +3 -0
- data/lib/facebook_cover_resize/view_helpers.rb +18 -0
- data/lib/facebook_cover_resize.rb +72 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5707f679f0c85e492939ab95c2b7d3b5b625b739
|
4
|
+
data.tar.gz: 9d65c69774143a91190e3fe8e1c515f7f8fcf07c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3ee4b5916e5cd6caa1f124351bae81964629b9a05a99416c212fe6875f1ec60bba369c46fb0ff9fd22ca8cdf9ecf3083c27c00a19bfd45761f2bb524de3911a9
|
7
|
+
data.tar.gz: 9bba516e5e645d20bc629e09beb356530132d0455d2a8215a8a9656f013fd5da2941198a29275e08037b376ba77215c4f8d2958a1f9f98c24ae706ca948d4fd7
|
data/.gitignore
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
/.bundle/
|
2
|
+
/.yardoc
|
3
|
+
/Gemfile.lock
|
4
|
+
/_yardoc/
|
5
|
+
/coverage/
|
6
|
+
/doc/
|
7
|
+
/pkg/
|
8
|
+
/spec/reports/
|
9
|
+
/tmp/
|
10
|
+
=======
|
11
|
+
*.gem
|
12
|
+
*.rbc
|
13
|
+
/.config
|
14
|
+
/coverage/
|
15
|
+
/InstalledFiles
|
16
|
+
/pkg/
|
17
|
+
/spec/reports/
|
18
|
+
/spec/examples.txt
|
19
|
+
/test/tmp/
|
20
|
+
/test/version_tmp/
|
21
|
+
/tmp/
|
22
|
+
|
23
|
+
# Used by dotenv library to load environment variables.
|
24
|
+
# .env
|
25
|
+
|
26
|
+
## Specific to RubyMotion:
|
27
|
+
.dat*
|
28
|
+
.repl_history
|
29
|
+
build/
|
30
|
+
*.bridgesupport
|
31
|
+
build-iPhoneOS/
|
32
|
+
build-iPhoneSimulator/
|
33
|
+
|
34
|
+
## Specific to RubyMotion (use of CocoaPods):
|
35
|
+
#
|
36
|
+
# We recommend against adding the Pods directory to your .gitignore. However
|
37
|
+
# you should judge for yourself, the pros and cons are mentioned at:
|
38
|
+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
|
39
|
+
#
|
40
|
+
# vendor/Pods/
|
41
|
+
|
42
|
+
## Documentation cache and generated files:
|
43
|
+
/.yardoc/
|
44
|
+
/_yardoc/
|
45
|
+
/doc/
|
46
|
+
/rdoc/
|
47
|
+
|
48
|
+
## Environment normalization:
|
49
|
+
/.bundle/
|
50
|
+
/vendor/bundle
|
51
|
+
/lib/bundler/man/
|
52
|
+
|
53
|
+
# for a library or gem, you might want to ignore these files since the code is
|
54
|
+
# intended to run in multiple environments; otherwise, check them in:
|
55
|
+
# Gemfile.lock
|
56
|
+
# .ruby-version
|
57
|
+
# .ruby-gemset
|
58
|
+
|
59
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
60
|
+
.rvmrc
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2017 Guillaume Barillot
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 guillaume barillot
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
# Facebook Cover Resize
|
2
|
+
|
3
|
+
This Gem allows to display and resize Facebook events' cover image according to offset_x / offset_y
|
4
|
+
given by Facebook. Using this Gem, you can display event cover image exactly like they are on Facebook,
|
5
|
+
no more distortions, no more poor cropping.
|
6
|
+
|
7
|
+
To use this Gem, you have to grab 3 parameters from Facebook:
|
8
|
+
|
9
|
+
* offset_x (positive integer)
|
10
|
+
* offset_y (positive integer)
|
11
|
+
* cover source (the URL of the image)
|
12
|
+
|
13
|
+
WARNING: aside from these data provided by Facebook, you'll also need the dimensions of the image itself!
|
14
|
+
I use the [Fastimage](https://github.com/sdsykes/fastimage) Gem for this
|
15
|
+
and it's working perfectly fine.
|
16
|
+
|
17
|
+
## Installation
|
18
|
+
|
19
|
+
Add this line to your application's Gemfile:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
gem 'facebook_cover_resize'
|
23
|
+
```
|
24
|
+
|
25
|
+
And then execute:
|
26
|
+
|
27
|
+
$ bundle
|
28
|
+
|
29
|
+
Or install it yourself as:
|
30
|
+
|
31
|
+
$ gem install facebook_cover_resize
|
32
|
+
|
33
|
+
## Usage
|
34
|
+
|
35
|
+
This Gem contains an algorithm for image size computations, and a Rails view helper. Using the view helper
|
36
|
+
is as simple as :
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
- @event = Event.find(123)
|
40
|
+
= event_cover_tag source: @event.cover, original: @event.cover_size, offsets: @event.offsets, width: 500
|
41
|
+
```
|
42
|
+
|
43
|
+
Note: Although this Gem handles resizing, you MUST consider the original Facebook width/height ratio,
|
44
|
+
which is 1.91 (500/262). That's why you can only set the new width for your thumbnails,
|
45
|
+
height is then automatically computed.
|
46
|
+
|
47
|
+
Example: Let's say you need to display your thumbnails in a 250px wide container. You only have to
|
48
|
+
call the Gem using this width, height being automatically set to 250 / 1.91 = 131px
|
49
|
+
|
50
|
+
### Life outside of Rails
|
51
|
+
|
52
|
+
If you don't want to use Rails, you can however just use the plain Algorithm:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
FacebookCoverResize.compute(
|
56
|
+
original: [original_width, original_height],
|
57
|
+
offsets: [offst_x, offset_y],
|
58
|
+
width: width_you_want_for_final_display_in_pixels
|
59
|
+
)
|
60
|
+
```
|
61
|
+
This outputs an array containing 4 values :
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
[
|
65
|
+
offset_y,
|
66
|
+
offset_x,
|
67
|
+
image_width,
|
68
|
+
image_height
|
69
|
+
]
|
70
|
+
```
|
71
|
+
You can then build a container, surrounding an img tag, using these values like this :
|
72
|
+
|
73
|
+
```html
|
74
|
+
<div
|
75
|
+
style="
|
76
|
+
width: 500px !important; # Container width
|
77
|
+
height: 262px !important; # Container width / 1.91
|
78
|
+
overflow: hidden !important;
|
79
|
+
position: relative !important;
|
80
|
+
padding:0 !important
|
81
|
+
">
|
82
|
+
<img
|
83
|
+
style="
|
84
|
+
position: absolute !important;
|
85
|
+
top: 0px !important; # offset_y
|
86
|
+
left: 0px !important; # offset_x
|
87
|
+
width: 500px !important; # image_width
|
88
|
+
height:824px !important; # image_height
|
89
|
+
margin: 0 !important"
|
90
|
+
src="FACEBOOK_URL/event_cover.jpg" # The cover source URL
|
91
|
+
/>
|
92
|
+
</div>
|
93
|
+
```
|
94
|
+
|
95
|
+
## Contributing
|
96
|
+
|
97
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/gbarillot/facebook_cover_resize.
|
98
|
+
|
99
|
+
|
100
|
+
## License
|
101
|
+
|
102
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "facebook_cover_resize"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'facebook_cover_resize/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "facebook_cover_resize"
|
8
|
+
spec.version = FacebookCoverResize::VERSION
|
9
|
+
spec.authors = ["guillaume barillot"]
|
10
|
+
spec.email = ["gbarillot@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = "Compute width/size and offsets x/y for Facebook events cover image"
|
13
|
+
spec.description = "Compute width/size and offsets x/y for Facebook events cover image"
|
14
|
+
spec.homepage = "https://github.com/gbarillot/facebook_cover_resize"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
23
|
+
"public gem pushes."
|
24
|
+
end
|
25
|
+
|
26
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
27
|
+
f.match(%r{^(test|spec|features)/})
|
28
|
+
end
|
29
|
+
spec.bindir = "exe"
|
30
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
31
|
+
spec.require_paths = ["lib"]
|
32
|
+
|
33
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
34
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
35
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
36
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module FacebookCoverResize
|
2
|
+
module ViewHelpers
|
3
|
+
def event_cover_tag(args)
|
4
|
+
dims = FacebookCoverResize.compute(original: args[:original], offsets: args[:offsets], width: args[:width])
|
5
|
+
width = args[:width].to_i
|
6
|
+
height = (width / 1.91).ceil
|
7
|
+
source = args[:source]
|
8
|
+
|
9
|
+
content_tag(:div,
|
10
|
+
image_tag(source,
|
11
|
+
style: "position: absolute !important;top: #{dims[0]}px !important;left: #{dims[1]}px !important;width: #{dims[2]}px !important;height:#{dims[3]}px !important; margin: 0 !important"
|
12
|
+
),
|
13
|
+
style: "width: #{width}px !important; height: #{height}px !important; overflow: hidden !important;position: relative !important; padding:0 !important"
|
14
|
+
)
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require "facebook_cover_resize/version"
|
2
|
+
require 'facebook_cover_resize/railtie' if defined?(Rails)
|
3
|
+
|
4
|
+
module FacebookCoverResize
|
5
|
+
def self.compute(args)
|
6
|
+
ow = args[:width].to_f
|
7
|
+
oh = (ow / 1.91)
|
8
|
+
|
9
|
+
if args[:original].any? && args[:offsets].any?
|
10
|
+
nw = args[:original].first.to_f
|
11
|
+
nh = args[:original].last.to_f
|
12
|
+
ox = args[:offsets].first.to_f
|
13
|
+
oy = args[:offsets].last.to_f
|
14
|
+
|
15
|
+
if ox == 0 && oy == 0
|
16
|
+
w = ow
|
17
|
+
h = nh * (ow / nw)
|
18
|
+
if h < oh
|
19
|
+
h = oh
|
20
|
+
w = nw * (oh / nh)
|
21
|
+
offset_x = ow - w
|
22
|
+
offset_y = 0
|
23
|
+
else
|
24
|
+
offset_x = 0
|
25
|
+
if w < h
|
26
|
+
offset_y = 0
|
27
|
+
else
|
28
|
+
offset_y = oh - h
|
29
|
+
end
|
30
|
+
end
|
31
|
+
else
|
32
|
+
if oy > 0
|
33
|
+
w = ow
|
34
|
+
h = nh * (ow / nw)
|
35
|
+
offset_x = 0
|
36
|
+
offset_y = (oh - h) * (oy / 100.0)
|
37
|
+
if h < oh
|
38
|
+
h = oh
|
39
|
+
w = nw * (oh / nh)
|
40
|
+
offset_x = (ow - w) / 2
|
41
|
+
offset_y = 0
|
42
|
+
end
|
43
|
+
elsif ox > 0
|
44
|
+
h = oh
|
45
|
+
w = nw * (oh / nh)
|
46
|
+
offset_x = (ow - w) / 2
|
47
|
+
offset_y = 0
|
48
|
+
else
|
49
|
+
w = ow
|
50
|
+
h = nh * (ow / nw)
|
51
|
+
offset_x = 0
|
52
|
+
offset_y = 0
|
53
|
+
end
|
54
|
+
end
|
55
|
+
else
|
56
|
+
w = ow
|
57
|
+
h = oh
|
58
|
+
offset_x = 0
|
59
|
+
offset_y = 0
|
60
|
+
end
|
61
|
+
|
62
|
+
# [top, left, width, height]
|
63
|
+
out = [
|
64
|
+
offset_y.ceil,
|
65
|
+
offset_x.ceil,
|
66
|
+
w.ceil,
|
67
|
+
h.ceil
|
68
|
+
]
|
69
|
+
|
70
|
+
return out
|
71
|
+
end
|
72
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: facebook_cover_resize
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- guillaume barillot
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-04-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.13'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
description: Compute width/size and offsets x/y for Facebook events cover image
|
56
|
+
email:
|
57
|
+
- gbarillot@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".travis.yml"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- facebook_cover_resize.gemspec
|
72
|
+
- lib/facebook_cover_resize.rb
|
73
|
+
- lib/facebook_cover_resize/railtie.rb
|
74
|
+
- lib/facebook_cover_resize/version.rb
|
75
|
+
- lib/facebook_cover_resize/view_helpers.rb
|
76
|
+
homepage: https://github.com/gbarillot/facebook_cover_resize
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata:
|
80
|
+
allowed_push_host: https://rubygems.org
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.4.8
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: Compute width/size and offsets x/y for Facebook events cover image
|
101
|
+
test_files: []
|