Skip to content

Commit 46546ac

Browse files
shedarconst-cloudinary
authored andcommitted
Add support of "auto" value for start_offset transformation parameter (#123)
* Add support of "auto" value for `start_offset` transformation parameter
1 parent 724eb4c commit 46546ac

File tree

2 files changed

+41
-22
lines changed

2 files changed

+41
-22
lines changed

src/Cloudinary.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,11 +481,11 @@ public static function generate_transformation_string(&$options = array())
481481
$dpr = Cloudinary::option_consume($options, "dpr", Cloudinary::config_get("dpr"));
482482

483483
$duration = Cloudinary::norm_range_value(Cloudinary::option_consume($options, "duration"));
484-
$start_offset = Cloudinary::norm_range_value(Cloudinary::option_consume($options, "start_offset"));
484+
$start_offset = Cloudinary::norm_auto_range_value(Cloudinary::option_consume($options, "start_offset"));
485485
$end_offset = Cloudinary::norm_range_value(Cloudinary::option_consume($options, "end_offset"));
486486
$offset = Cloudinary::split_range(Cloudinary::option_consume($options, "offset"));
487487
if (!empty($offset)) {
488-
$start_offset = Cloudinary::norm_range_value($offset[0]);
488+
$start_offset = Cloudinary::norm_auto_range_value($offset[0]);
489489
$end_offset = Cloudinary::norm_range_value($offset[1]);
490490
}
491491

@@ -856,6 +856,14 @@ private static function norm_range_value($value)
856856
return $matches['value'] . $modifier;
857857
}
858858

859+
private static function norm_auto_range_value($value)
860+
{
861+
if ($value == 'auto') {
862+
return $value;
863+
}
864+
return self::norm_range_value($value);
865+
}
866+
859867
private static function process_video_codec_param($param)
860868
{
861869
$out_param = $param;

tests/CloudinaryTest.php

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ class CloudinaryTest extends TestCase
1212
const DEFAULT_UPLOAD_PATH = 'http://res.cloudinary.com/test123/image/upload/';
1313
const VIDEO_UPLOAD_PATH = 'http://res.cloudinary.com/test123/video/upload/';
1414

15+
private $range_test_pairs = [
16+
// integer values
17+
["200", "200"], [200, "200"], [0, "0"],
18+
// float values
19+
["200.0", "200.0"], [200.0, "200.0"], [200.123, "200.123"], [200.123000, "200.123"], [0.0, "0.0"],
20+
//percent values
21+
["20p", "20p"], ["20P", "20p"], ["20%", "20p"], ["20.5%", "20.5p"],
22+
// invalid values
23+
["p", null], ["", null], [null, null], ["non_auto", null],
24+
];
25+
1526
private $original_user_platform;
1627

1728
public function setUp()
@@ -834,26 +845,20 @@ public function test_norm_range_value()
834845
{
835846
$method = new ReflectionMethod('Cloudinary', 'norm_range_value');
836847
$method->setAccessible(true);
837-
// should parse integer range values
838-
$this->assertEquals($method->invoke(null, "200"), "200");
839-
$this->assertEquals($method->invoke(null, 200), "200");
840-
$this->assertEquals($method->invoke(null, 0), "0");
841-
// should parse float range values
842-
$this->assertEquals($method->invoke(null, "200.0"), "200.0");
843-
$this->assertEquals($method->invoke(null, 200.0), "200.0");
844-
$this->assertEquals($method->invoke(null, 200.00), "200.0");
845-
$this->assertEquals($method->invoke(null, 200.123), "200.123");
846-
$this->assertEquals($method->invoke(null, 200.123000), "200.123");
847-
$this->assertEquals($method->invoke(null, 0.0), "0.0");
848-
// should parse a percent range value
849-
$this->assertEquals($method->invoke(null, "20p"), "20p");
850-
$this->assertEquals($method->invoke(null, "20P"), "20p");
851-
$this->assertEquals($method->invoke(null, "20%"), "20p");
852-
$this->assertEquals($method->invoke(null, "20.5%"), "20.5p");
853-
// should handle invalid input
854-
$this->assertNull($method->invoke(null, "p"));
855-
$this->assertNull($method->invoke(null, ""));
856-
$this->assertNull($method->invoke(null, null));
848+
foreach ($this->range_test_pairs as $pair) {
849+
$this->assertEquals($method->invoke(null, $pair[0]), $pair[1]);
850+
}
851+
$this->assertNull($method->invoke(null, "auto"), "Shouldn't support 'auto' value");
852+
}
853+
854+
public function test_norm_auto_range_value()
855+
{
856+
$method = new ReflectionMethod('Cloudinary', 'norm_auto_range_value');
857+
$method->setAccessible(true);
858+
foreach ($this->range_test_pairs as $pair) {
859+
$this->assertEquals($method->invoke(null, $pair[0]), $pair[1]);
860+
}
861+
$this->assertEquals($method->invoke(null, "auto"), "auto", "Should support 'auto' value");
857862
}
858863

859864
public function test_video_codec()
@@ -954,6 +959,12 @@ public function test_start_offset()
954959
array('resource_type' => 'video', 'start_offset' => '35%'),
955960
CloudinaryTest::VIDEO_UPLOAD_PATH . "so_35p/video_id"
956961
);
962+
// should support auto select of a suitable frame from the first few seconds of a video
963+
$this->cloudinary_url_assertion(
964+
"video_id",
965+
array('resource_type' => 'video', 'start_offset' => 'auto'),
966+
CloudinaryTest::VIDEO_UPLOAD_PATH . "so_auto/video_id"
967+
);
957968
}
958969

959970
public function test_end_offset()

0 commit comments

Comments
 (0)