Skip to content

Commit 512b775

Browse files
author
stlc-bot
committed
feat(transformation): add saturation and encode negative numbers with the N prefix in URL generation
Stainless-Generated-From: 66ff0c4
1 parent 7a9b990 commit 512b775

4 files changed

Lines changed: 69 additions & 7 deletions

File tree

‎Gemfile.lock‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ GIT
1111
PATH
1212
remote: .
1313
specs:
14-
imagekitio (4.11.0)
14+
imagekitio (4.12.0)
1515
cgi
1616
connection_pool
1717
standardwebhooks
@@ -228,4 +228,4 @@ DEPENDENCIES
228228
yard
229229

230230
BUNDLED WITH
231-
2.7.1
231+
4.0.21

‎lib/imagekitio/helpers/helper.rb‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ def build_transformation_string(transformations)
209209
value = value.to_i if value == value.to_i
210210
end
211211

212+
value = encode_negative_number(value) if %w[rt x y e-saturation].include?(transform_key)
213+
212214
parsed_transform_step << "#{transform_key}#{Imagekitio::TransformationUtils.get_transform_key_value_delimiter}#{value}"
213215
end
214216
# rubocop:enable Metrics/BlockLength
@@ -410,6 +412,16 @@ def remove_trailing_slash(str)
410412
str.chomp("/")
411413
end
412414

415+
# Encode a negative number with the N prefix, because "-" separates a key from its value in the URL.
416+
# For example, -40 becomes "N40". Other values are returned as is.
417+
def encode_negative_number(value)
418+
return value unless value.is_a?(Numeric) && value.negative?
419+
420+
magnitude = value.abs
421+
magnitude = magnitude.to_i if magnitude == magnitude.to_i
422+
"N#{magnitude}"
423+
end
424+
413425
# Remove leading slash from string
414426
def remove_leading_slash(str)
415427
return str unless str.is_a?(String) && str.start_with?("/")
@@ -633,17 +645,19 @@ def add_overlay_properties(parts, overlay)
633645
# Add position properties
634646
position = safe_get(overlay, :position)
635647
if position
648+
# Read raw values: the typed accessors raise for strings that are not numeric, such as "N20" or "bw_mul_0.4".
649+
position = position.to_h if position.respond_to?(:to_h)
636650
x = safe_get(position, :x)
637651
y = safe_get(position, :y_)
638652
x_center = safe_get(position, :x_center)
639653
y_center = safe_get(position, :y_center)
640654
focus = safe_get(position, :focus)
641655
anchor_point = safe_get(position, :anchor_point)
642656

643-
parts << "lx-#{x}" if x
644-
parts << "ly-#{y}" if y
645-
parts << "lxc-#{x_center}" if x_center
646-
parts << "lyc-#{y_center}" if y_center
657+
parts << "lx-#{encode_negative_number(x)}" if x
658+
parts << "ly-#{encode_negative_number(y)}" if y
659+
parts << "lxc-#{encode_negative_number(x_center)}" if x_center
660+
parts << "lyc-#{encode_negative_number(y_center)}" if y_center
647661
parts << "lfo-#{focus}" if focus
648662
parts << "lap-#{anchor_point}" if anchor_point
649663
end

‎lib/imagekitio/helpers/transformation_utils.rb‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ module TransformationUtils
8585
"color_replace" => "cr",
8686
"colorReplace" => "cr",
8787
"colorize" => "e-colorize",
88+
"saturation" => "e-saturation",
8889
"distort" => "e-distort",
8990

9091
# Other flags & finishing

‎test/imagekitio/custom-tests/url-generation/advanced_url_generation_test.rb‎

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ def test_should_generate_the_correct_url_with_many_transformations_including_vid
436436
color_replace: "FF0000_50_0000FF",
437437
distort: "p-100_50_200_150_300_250_400_350",
438438
colorize: "co-FF0000_in-15",
439+
saturation: 50,
439440
original: true,
440441
page: "2_4",
441442
raw: "h-200,w-300,l-image,i-logo.png,l-end"
@@ -451,7 +452,7 @@ def test_should_generate_the_correct_url_with_many_transformations_including_vid
451452
)
452453
)
453454

454-
expected = "https://ik.imagekit.io/test_url_endpoint/test_path.jpg?tr=h-300,w-400,ar-4-3,q-40,c-force,cm-extract,fo-left,f-jpeg,r-50,bg-A94D34,b-5-A94D34,rt-90,bl-10,n-some_name,pr-true,lo-true,t-5,md-true,cp-true,dn-72,di-folder@@file.jpg,dpr-3,x-10,y-20,xc-30,yc-40,fl-h,o-0.8,z-2,vc-h264,ac-aac,so-5,eo-15,du-10,sr-1440_1080,e-grayscale,e-upscale,e-retouch,e-genvar,e-dropshadow,e-changebg-prompt-car,e-edit-prompt-make it vintage,e-bgremove,e-contrast,e-shadow-bl-15_st-40_x-10_y-N5,e-sharpen-10,e-usm-2-2-0.8-0.024,e-gradient-from-red_to-white,cr-FF0000_50_0000FF,e-distort-p-100_50_200_150_300_250_400_350,e-colorize-co-FF0000_in-15,orig-true,pg-2_4,h-200,w-300,l-image,i-logo.png,l-end"
455+
expected = "https://ik.imagekit.io/test_url_endpoint/test_path.jpg?tr=h-300,w-400,ar-4-3,q-40,c-force,cm-extract,fo-left,f-jpeg,r-50,bg-A94D34,b-5-A94D34,rt-90,bl-10,n-some_name,pr-true,lo-true,t-5,md-true,cp-true,dn-72,di-folder@@file.jpg,dpr-3,x-10,y-20,xc-30,yc-40,fl-h,o-0.8,z-2,vc-h264,ac-aac,so-5,eo-15,du-10,sr-1440_1080,e-grayscale,e-upscale,e-retouch,e-genvar,e-dropshadow,e-changebg-prompt-car,e-edit-prompt-make it vintage,e-bgremove,e-contrast,e-shadow-bl-15_st-40_x-10_y-N5,e-sharpen-10,e-usm-2-2-0.8-0.024,e-gradient-from-red_to-white,cr-FF0000_50_0000FF,e-distort-p-100_50_200_150_300_250_400_350,e-colorize-co-FF0000_in-15,e-saturation-50,orig-true,pg-2_4,h-200,w-300,l-image,i-logo.png,l-end"
455456
assert_equal(expected, url)
456457
end
457458

@@ -500,4 +501,50 @@ def test_should_work_with_plain_hashes_for_transformations_and_options
500501
expected = "https://ik.imagekit.io/test_url_endpoint/test_path1.jpg?tr=w-300,h-200,q-85,b-5_FF0000,e-bgremove,e-dropshadow"
501502
assert_equal(expected, url)
502503
end
504+
505+
def test_should_encode_negative_numbers_with_the_n_prefix_and_keep_n_prefixed_strings_as_is
506+
transformation = [
507+
Imagekitio::Models::Transformation.new(
508+
rotation: -40,
509+
x: -10,
510+
y_: -20.5,
511+
saturation: -50
512+
),
513+
Imagekitio::Models::Transformation.new(
514+
rotation: "N40",
515+
x: "N10",
516+
y_: 10,
517+
saturation: 0
518+
),
519+
Imagekitio::Models::Transformation.new(
520+
overlay: Imagekitio::Models::ImageOverlay.new(
521+
input: "logo.png",
522+
position: Imagekitio::Models::OverlayPosition.new(
523+
x: -10,
524+
y_: "N20",
525+
x_center: -30,
526+
y_center: -40.5
527+
),
528+
transformation: [
529+
Imagekitio::Models::Transformation.new(
530+
rotation: -15,
531+
saturation: -100
532+
)
533+
]
534+
)
535+
)
536+
]
537+
538+
url = @client.helper.build_url(
539+
Imagekitio::Models::SrcOptions.new(
540+
src: "/test_path.jpg",
541+
url_endpoint: "https://ik.imagekit.io/test_url_endpoint",
542+
transformation_position: :query,
543+
transformation: transformation
544+
)
545+
)
546+
547+
expected = "https://ik.imagekit.io/test_url_endpoint/test_path.jpg?tr=rt-N40,x-N10,y-N20.5,e-saturation-N50:rt-N40,x-N10,y-10,e-saturation-0:l-image,i-logo.png,lx-N10,ly-N20,lxc-N30,lyc-N40.5,rt-N15,e-saturation-N100,l-end"
548+
assert_equal(expected, url)
549+
end
503550
end

0 commit comments

Comments
 (0)