make-image-square 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +47 -0
- data/lib/make_image_square/version.rb +5 -0
- data/lib/make_image_square.rb +92 -0
- metadata +51 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 191824a355074b9d1afdddc527d0c4c5e7bcf3d2a89592d0db5beb93892f86ce
|
|
4
|
+
data.tar.gz: fea14d5dcdb9decfa3d21ae4b0a6bf0bc5696793dc5c0a158bb7ccd7ebc2e3de
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 5fe9c3da96a81d08278f2d93b19fd432fb7038821725209dddf01b693031669e58188548307269639e29f7cce527900d890870b2e38b5eb6fcd0388e60cc1a3b
|
|
7
|
+
data.tar.gz: 42bb112f28e47eb89544d8ff0cd1db0fd23749a1f019e73a1520ad351b7dcde09c0a2ea17a2828a5797cf7a57d12f156ff2e65cd1520378ef9075d83608ee5a8
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 squareimage.run
|
|
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/README.md
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# make-image-square
|
|
2
|
+
|
|
3
|
+
Tiny helper that turns any image size into a **1:1 layout plan**: square canvas size plus left/right/top/bottom padding so you can fit without cropping.
|
|
4
|
+
|
|
5
|
+
Useful when you need the math for letterboxing / pillarboxing before you draw to a canvas, generate CSS `object-fit`-style boxes, or prep assets for Instagram and profile photos.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem "make-image-square", "~> 0.1"
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
gem install make-image-square
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
```ruby
|
|
20
|
+
require "make_image_square"
|
|
21
|
+
|
|
22
|
+
# 1200×800 landscape → 1200×1200 with top/bottom pads
|
|
23
|
+
layout = MakeImageSquare.make_image_square(1200, 800)
|
|
24
|
+
# canvas 1200, pad_top 200, pad_bottom 200
|
|
25
|
+
|
|
26
|
+
# Force a 1080 square (typical feed post)
|
|
27
|
+
ig = MakeImageSquare.make_image_square(1200, 800, canvas_size: 1080)
|
|
28
|
+
|
|
29
|
+
# Padding-only helper
|
|
30
|
+
pads = MakeImageSquare.square_padding(800, 1200)
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Modes
|
|
34
|
+
|
|
35
|
+
| Mode | Behavior |
|
|
36
|
+
|------|----------|
|
|
37
|
+
| `:fit` (default) | Entire image visible; empty bands become padding |
|
|
38
|
+
| `:fill` | Image covers the square; caller is expected to crop overflow |
|
|
39
|
+
|
|
40
|
+
## Notes
|
|
41
|
+
|
|
42
|
+
- This gem only returns numbers. It does not decode or rewrite image files.
|
|
43
|
+
- Prefer a UI instead of wiring ImageMagick yourself? There is an [online 1:1 letterbox editor](https://squareimage.run) that can pad, blur the frame, or crop in the browser.
|
|
44
|
+
|
|
45
|
+
## License
|
|
46
|
+
|
|
47
|
+
MIT
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "make_image_square/version"
|
|
4
|
+
|
|
5
|
+
# Compute a square canvas and letterbox/pillarbox padding for an image.
|
|
6
|
+
# Does not load or transform pixels — layout math only.
|
|
7
|
+
module MakeImageSquare
|
|
8
|
+
Layout = Struct.new(
|
|
9
|
+
:canvas,
|
|
10
|
+
:content_width,
|
|
11
|
+
:content_height,
|
|
12
|
+
:offset_x,
|
|
13
|
+
:offset_y,
|
|
14
|
+
:pad_left,
|
|
15
|
+
:pad_right,
|
|
16
|
+
:pad_top,
|
|
17
|
+
:pad_bottom,
|
|
18
|
+
keyword_init: true
|
|
19
|
+
)
|
|
20
|
+
Padding = Struct.new(:canvas, :left, :right, :top, :bottom, keyword_init: true)
|
|
21
|
+
|
|
22
|
+
class Error < StandardError; end
|
|
23
|
+
class InvalidSize < Error; end
|
|
24
|
+
|
|
25
|
+
module_function
|
|
26
|
+
|
|
27
|
+
def make_image_square(width, height, mode: :fit, canvas_size: nil)
|
|
28
|
+
w = require_positive(width)
|
|
29
|
+
h = require_positive(height)
|
|
30
|
+
canvas = canvas_size.nil? ? [w, h].max : require_positive(canvas_size)
|
|
31
|
+
mode = mode.to_sym
|
|
32
|
+
|
|
33
|
+
if mode == :fill
|
|
34
|
+
scale = canvas / [w, h].min
|
|
35
|
+
content_width = w * scale
|
|
36
|
+
content_height = h * scale
|
|
37
|
+
offset_x = (canvas - content_width) / 2.0
|
|
38
|
+
offset_y = (canvas - content_height) / 2.0
|
|
39
|
+
return Layout.new(
|
|
40
|
+
canvas: canvas,
|
|
41
|
+
content_width: content_width,
|
|
42
|
+
content_height: content_height,
|
|
43
|
+
offset_x: offset_x,
|
|
44
|
+
offset_y: offset_y,
|
|
45
|
+
pad_left: [0, offset_x].max,
|
|
46
|
+
pad_right: [0, canvas - content_width - offset_x].max,
|
|
47
|
+
pad_top: [0, offset_y].max,
|
|
48
|
+
pad_bottom: [0, canvas - content_height - offset_y].max
|
|
49
|
+
)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
unless mode == :fit
|
|
53
|
+
raise InvalidSize, "mode must be :fit or :fill"
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
scale = canvas / [w, h].max
|
|
57
|
+
content_width = w * scale
|
|
58
|
+
content_height = h * scale
|
|
59
|
+
pad_x = (canvas - content_width) / 2.0
|
|
60
|
+
pad_y = (canvas - content_height) / 2.0
|
|
61
|
+
Layout.new(
|
|
62
|
+
canvas: canvas,
|
|
63
|
+
content_width: content_width,
|
|
64
|
+
content_height: content_height,
|
|
65
|
+
offset_x: pad_x,
|
|
66
|
+
offset_y: pad_y,
|
|
67
|
+
pad_left: pad_x,
|
|
68
|
+
pad_right: pad_x,
|
|
69
|
+
pad_top: pad_y,
|
|
70
|
+
pad_bottom: pad_y
|
|
71
|
+
)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def square_padding(width, height, canvas_size = nil)
|
|
75
|
+
layout = make_image_square(width, height, mode: :fit, canvas_size: canvas_size)
|
|
76
|
+
Padding.new(
|
|
77
|
+
canvas: layout.canvas,
|
|
78
|
+
left: layout.pad_left,
|
|
79
|
+
right: layout.pad_right,
|
|
80
|
+
top: layout.pad_top,
|
|
81
|
+
bottom: layout.pad_bottom
|
|
82
|
+
)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def require_positive(value)
|
|
86
|
+
value = Float(value)
|
|
87
|
+
raise InvalidSize, "width, height, and canvas size must be positive numbers" unless value.finite? && value.positive?
|
|
88
|
+
|
|
89
|
+
value
|
|
90
|
+
end
|
|
91
|
+
private_class_method :require_positive
|
|
92
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: make-image-square
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- hiltonlee981
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-09-08 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Tiny Ruby helper that turns any image size into a square canvas plus
|
|
14
|
+
left/right/top/bottom padding. Fit mode letterboxes; fill mode covers. Layout math
|
|
15
|
+
only — it does not decode or rewrite image files.
|
|
16
|
+
email:
|
|
17
|
+
- hiltonlee981@gmail.com
|
|
18
|
+
executables: []
|
|
19
|
+
extensions: []
|
|
20
|
+
extra_rdoc_files: []
|
|
21
|
+
files:
|
|
22
|
+
- LICENSE
|
|
23
|
+
- README.md
|
|
24
|
+
- lib/make_image_square.rb
|
|
25
|
+
- lib/make_image_square/version.rb
|
|
26
|
+
homepage: https://squareimage.run
|
|
27
|
+
licenses:
|
|
28
|
+
- MIT
|
|
29
|
+
metadata:
|
|
30
|
+
homepage_uri: https://squareimage.run
|
|
31
|
+
rubygems_mfa_required: 'false'
|
|
32
|
+
post_install_message:
|
|
33
|
+
rdoc_options: []
|
|
34
|
+
require_paths:
|
|
35
|
+
- lib
|
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - ">="
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '3.0'
|
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
42
|
+
requirements:
|
|
43
|
+
- - ">="
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: '0'
|
|
46
|
+
requirements: []
|
|
47
|
+
rubygems_version: 3.5.22
|
|
48
|
+
signing_key:
|
|
49
|
+
specification_version: 4
|
|
50
|
+
summary: Compute 1:1 letterbox/pillarbox padding from a photo's width and height.
|
|
51
|
+
test_files: []
|