cloudflare_image_resizing 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +22 -0
- data/README.md +81 -0
- data/Rakefile +8 -0
- data/app/helpers/cloudflare_image_resizing/helper.rb +68 -0
- data/lib/cloudflare_image_resizing/configuration.rb +11 -0
- data/lib/cloudflare_image_resizing/engine.rb +12 -0
- data/lib/cloudflare_image_resizing/version.rb +5 -0
- data/lib/cloudflare_image_resizing.rb +10 -0
- data/lib/tasks/cloudflare_image_resizing_tasks.rake +4 -0
- metadata +66 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 7660eee22969e295f57dd1dd76a1e0dded9d693fdfe484eade7e77262019e36c
|
4
|
+
data.tar.gz: 54d286cd434e83f9a6c10d6a9df5710abc107ddd30bd52f0abf9f96fd45d5bea
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2d14c16811c56ed3bb5de587d021d7fd3b9cc0199499a8dd61d351e2d93cb85d31916e569c9458474e76fbac4beafee2bb81c1b7a830c74ab2bbaeb1e075528f
|
7
|
+
data.tar.gz: 55fedb0c72ea3537c13f463ae4a10e85fcfba8c0458aef31afe951997c63cb7bf1d3c3e6225eed16b43e266860b40650e9b8425327e1eced64702a8de3d32883
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 Collective Idea
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
[](https://rubygems.org/gems/cloudflare_image_resizing)
|
2
|
+
[](https://github.com/collectiveidea/cloudflare_image_resizing/actions/workflows/ci.yml)
|
3
|
+
[](https://github.com/testdouble/standard)
|
4
|
+
|
5
|
+
# CloudflareImageResizing
|
6
|
+
|
7
|
+
[Cloudflare's Image Resizing](https://developers.cloudflare.com/images/image-resizing) is a great alternative to resizing images in your Rails app. You get to resize images on-the-fly without adding any load to your server.
|
8
|
+
|
9
|
+
This gem make it easy to pass cloudflare's [resizing settings](https://developers.cloudflare.com/images/image-resizing/url-format/) via a pair of helpers:
|
10
|
+
|
11
|
+
|
12
|
+
### `resized_image_tag`
|
13
|
+
|
14
|
+
Helper to use `resized_image` and also build an `image_tag`.
|
15
|
+
|
16
|
+
Automatically adds a `srcset` with the same image in 2x and 3x (unless you provide a `srcset`.) See: https://developers.cloudflare.com/images/image-resizing/responsive-images/#srcset-for-high-dpi-displays
|
17
|
+
|
18
|
+
Example usage:
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
<%= resized_image_tag(@user.avatar, {width: 100, fit: "crop"}, alt: "Cropped avatar" %>
|
22
|
+
```
|
23
|
+
|
24
|
+
### `resized_image`
|
25
|
+
|
26
|
+
Helper return a URL with the applied [resizing settings](https://developers.cloudflare.com/images/image-resizing/url-format/)
|
27
|
+
|
28
|
+
Example usage:
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
<%= image_tag resized_image(@user.avatar, width: 100, fit: "crop"), alt: "Cropped avatar" %>
|
32
|
+
```
|
33
|
+
|
34
|
+
### Extra option: Preload
|
35
|
+
|
36
|
+
If you pass `preload: true` with the resize options, then add `<%= yield(:cloudflare_image_resizing_preload) %>` to your `<head>`, you will get `<link rel="preload">` for the images.
|
37
|
+
|
38
|
+
### Outside of controllers/views
|
39
|
+
|
40
|
+
You can use the helpers outside of a controller or view, simply `include CloudflareImageResizing::Helper` where you need it.
|
41
|
+
|
42
|
+
### Resizable formats
|
43
|
+
|
44
|
+
Not all formats are resizable. See Cloudflare's docs on [format limitations](https://developers.cloudflare.com/images/image-resizing/format-limitations/)
|
45
|
+
|
46
|
+
tl;dr: these helpers only try to resize `image/jpeg`, `image/gif`, `image/png`, and `image/webp`. Other formats are passed through without resizing.
|
47
|
+
|
48
|
+
## Prerequisites
|
49
|
+
|
50
|
+
* You are using Cloudflare and have Image Resizing enabled.
|
51
|
+
* You're using Rails
|
52
|
+
* You have images either in ActiveStorage or referenced by a URL.
|
53
|
+
|
54
|
+
## Installation
|
55
|
+
Add this line to your application's Gemfile:
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
gem "cloudflare_image_resizing"
|
59
|
+
```
|
60
|
+
|
61
|
+
## Configuration
|
62
|
+
|
63
|
+
You can use an initializer to configure the `enabled` flag (defaults to `true` for non-local environments, checking `Rails.env.local?`.)
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
Rails.application.config.cloudflare_image_resizing.enabled = false
|
67
|
+
```
|
68
|
+
|
69
|
+
***Note:*** Image resizing doesn't work from `localhost`, as Cloudflare needs to be able to proxy the request. For that reason, we only enable resizing in production. When disabled, the helpers just return the image path without resize options.
|
70
|
+
|
71
|
+
|
72
|
+
## Related Reading
|
73
|
+
* [Active Storage and Cloudflare’s R2](https://discuss.rubyonrails.org/t/active-storage-and-cloudflares-r2/80819)
|
74
|
+
|
75
|
+
## Contributing
|
76
|
+
Open a PR. Be kind. Make the world a better place.
|
77
|
+
|
78
|
+
We built this for our own use cases and many have not run into yours. Let us know of omissions, but be prepared to do the work yourself.
|
79
|
+
|
80
|
+
## License
|
81
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CloudflareImageResizing
|
4
|
+
module Helper
|
5
|
+
RESIZABLE_CONTENT_TYPES = %w[image/jpeg image/gif image/png image/webp].freeze
|
6
|
+
|
7
|
+
# Helper to use resized_image and also build an image_tag
|
8
|
+
# Automatically adds a srcset with the same image in 2x and 3x (unless a srcset is provided.)
|
9
|
+
# See: https://developers.cloudflare.com/images/image-resizing/responsive-images/#srcset-for-high-dpi-displays
|
10
|
+
# Example usage:
|
11
|
+
# <%= resized_image_tag @user.avatar, resize: {width: 100, fit: "crop"}, alt: "Cropped avatar" %>
|
12
|
+
def resized_image_tag(image, options = {})
|
13
|
+
resize_options = options.delete(:resize) || {}
|
14
|
+
image_tag(
|
15
|
+
resized_image(image, resize_options),
|
16
|
+
options.reverse_merge(srcset: "#{resized_image(image, resize_options.merge(dpr: 1))},
|
17
|
+
#{resized_image(image, resize_options.merge(dpr: 2))} 2x,
|
18
|
+
#{resized_image(image, resize_options.merge(dpr: 3))} 3x".gsub(/\s+/, " "))
|
19
|
+
)
|
20
|
+
end
|
21
|
+
|
22
|
+
# Helper to use Cloudflare Image Resizing
|
23
|
+
# https://developers.cloudflare.com/images/image-resizing/url-format/
|
24
|
+
# Example usage:
|
25
|
+
# <%= image_tag resized_image(@user.avatar, width: 100, fit: "crop"), alt: "Cropped avatar" %>
|
26
|
+
def resized_image(image, options)
|
27
|
+
preload = options.delete(:preload)
|
28
|
+
|
29
|
+
path = if image.is_a?(ActiveStorage::Attachment) || image.is_a?(ActiveStorage::Attached) || image.is_a?(ActionText::Attachment)
|
30
|
+
rails_public_blob_url(image)
|
31
|
+
else
|
32
|
+
image_path(image)
|
33
|
+
end
|
34
|
+
|
35
|
+
if resizable?(image)
|
36
|
+
if ::Rails.application.config.cloudflare_image_resizing.enabled
|
37
|
+
path = "/" + path unless path.starts_with?("/") # Direct R2 URLs don't have a leading /
|
38
|
+
path = "/cdn-cgi/image/" + options.to_param.tr("&", ",") + path
|
39
|
+
end
|
40
|
+
|
41
|
+
if preload
|
42
|
+
# Allow us to add a <link rel="preload"> to the head
|
43
|
+
# to speed up loading resized images.
|
44
|
+
content_for(:cloudflare_image_resizing_preload) do
|
45
|
+
preload_link_tag(path, as: :image)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
path
|
51
|
+
end
|
52
|
+
|
53
|
+
# Is the image resizable with Cloudflare Image Resizing?
|
54
|
+
# https://developers.cloudflare.com/images/image-resizing/format-limitations/
|
55
|
+
# If we try and it isn't, we get a 415
|
56
|
+
# Someday (supposedly) setting onerror=redirect will make this unnecessary.
|
57
|
+
def resizable?(image)
|
58
|
+
if image.is_a?(ActiveStorage::Attachment) || image.is_a?(ActiveStorage::Attached)
|
59
|
+
image.content_type.in?(RESIZABLE_CONTENT_TYPES)
|
60
|
+
elsif image.is_a?(String)
|
61
|
+
extension = File.extname(image)[1..]
|
62
|
+
Mime::Type.lookup_by_extension(extension).to_s.in?(RESIZABLE_CONTENT_TYPES)
|
63
|
+
else
|
64
|
+
false
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module CloudflareImageResizing
|
2
|
+
class Engine < ::Rails::Engine
|
3
|
+
isolate_namespace CloudflareImageResizing
|
4
|
+
engine_name "cloudflare_image_resizing"
|
5
|
+
|
6
|
+
config.cloudflare_image_resizing = Configuration.new
|
7
|
+
|
8
|
+
config.to_prepare do
|
9
|
+
::ApplicationController.helper(CloudflareImageResizing::Helper)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cloudflare_image_resizing
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Daniel Morrison
|
8
|
+
bindir: bin
|
9
|
+
cert_chain: []
|
10
|
+
date: 2025-01-29 00:00:00.000000000 Z
|
11
|
+
dependencies:
|
12
|
+
- !ruby/object:Gem::Dependency
|
13
|
+
name: rails
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - ">="
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: 7.1.0
|
19
|
+
type: :runtime
|
20
|
+
prerelease: false
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
requirements:
|
23
|
+
- - ">="
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: 7.1.0
|
26
|
+
description: Easily resize images in Rails using Cloudflare's image resizing service.
|
27
|
+
email:
|
28
|
+
- daniel@collectiveidea.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- MIT-LICENSE
|
34
|
+
- README.md
|
35
|
+
- Rakefile
|
36
|
+
- app/helpers/cloudflare_image_resizing/helper.rb
|
37
|
+
- lib/cloudflare_image_resizing.rb
|
38
|
+
- lib/cloudflare_image_resizing/configuration.rb
|
39
|
+
- lib/cloudflare_image_resizing/engine.rb
|
40
|
+
- lib/cloudflare_image_resizing/version.rb
|
41
|
+
- lib/tasks/cloudflare_image_resizing_tasks.rake
|
42
|
+
homepage: https://github.com/collectiveidea/cloudflare_image_resizing
|
43
|
+
licenses:
|
44
|
+
- MIT
|
45
|
+
metadata:
|
46
|
+
homepage_uri: https://github.com/collectiveidea/cloudflare_image_resizing
|
47
|
+
source_code_uri: https://github.com/collectiveidea/cloudflare_image_resizing
|
48
|
+
changelog_uri: https://github.com/collectiveidea/cloudflare_image_resizing/blob/main/CHANGELOG.md
|
49
|
+
rdoc_options: []
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: 2.7.0
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
requirements: []
|
63
|
+
rubygems_version: 3.6.2
|
64
|
+
specification_version: 4
|
65
|
+
summary: Easily resize images in Rails using Cloudflare's image resizing service.
|
66
|
+
test_files: []
|