Skip to content

Commit

Permalink
Merge branch 'feature/layers'
Browse files Browse the repository at this point in the history
  • Loading branch information
tocker committed Jan 28, 2016
2 parents c9ddafd + cb1466f commit f482147
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 50 deletions.
58 changes: 31 additions & 27 deletions src/Cloudinary.php
Expand Up @@ -48,6 +48,8 @@ public static function userAgent()
}
}

public static function is_not_null ($var) { return !is_null($var);}

public static function config($values = NULL) {
if (self::$config == NULL) {
self::reset_config();
Expand Down Expand Up @@ -286,7 +288,7 @@ public static function generate_transformation_string(&$options=array()) {
"font_weight"=>"normal", "font_style"=>"normal", "text_decoration"=>"none", "text_align"=>NULL, "stroke"=>"none"
);

private static function process_text_options($layer, $layer_parameter) {
private static function text_style( $layer, $layer_parameter) {
$font_family = Cloudinary::option_get($layer, "font_family");
$font_size = Cloudinary::option_get($layer, "font_size");
$keywords = array();
Expand Down Expand Up @@ -316,49 +318,51 @@ private static function process_text_options($layer, $layer_parameter) {
}
array_unshift($keywords, $font_size);
array_unshift($keywords, $font_family);
return implode("_", $keywords);
return implode("_", array_filter($keywords, 'Cloudinary::is_not_null'));
}

private static function process_layer($layer, $layer_parameter) {
if (is_array($layer)) {
$resource_type = Cloudinary::option_get($layer, "resource_type");
$text = Cloudinary::option_get($layer, "text");
$type = Cloudinary::option_get($layer, "type");
$text = Cloudinary::option_get($layer, "text");
$text_style = NULL;
$public_id = Cloudinary::option_get($layer, "public_id");
$format = Cloudinary::option_get($layer, "format");

$components = array();
if ($text != NULL && $resource_type == NULL) $resource_type = "text";
if ($public_id != NULL && $format != NULL) $public_id = $public_id . "." . $format;
if ($public_id == NULL && $resource_type != "text") {
throw new InvalidArgumentException("Must supply public_id for for non-text $layer_parameter");

if ($public_id != NULL){
$public_id = str_replace("/", ":", $public_id);
if($format != NULL) $public_id = $public_id . "." . $format;
}

if ($resource_type != NULL && $resource_type != "image") array_push($components, $resource_type);
if ($type != NULL && $type != "upload") array_push($components, $type);
if ($resource_type == "text" || $resource_type == "subtitles") {
if ($public_id == NULL && $text == NULL) {
throw new InvalidArgumentException("Must supply either text or public_id in $layer_parameter");
if ($text == NULL && $resource_type != "text"){
if ($public_id == NULL) {
throw new InvalidArgumentException("Must supply public_id for $resource_type $layer_parameter");
}
$text_options = Cloudinary::process_text_options($layer, $layer_parameter);
if ($text_options != NULL) {
array_push($components, $text_options);
if($resource_type == "subtitles") {
$text_style = Cloudinary::text_style($layer, $layer_parameter);
}
if ($public_id != NULL) {
$public_id = str_replace("/", ":", $public_id);
array_push($components, $public_id);
}
if ($text != NULL) {

} else {
$resource_type = "text";
$type = NULL; // type is ignored for text layers
$text_style = Cloudinary::text_style($layer, $layer_parameter); #FIXME duplicate
if($text != NULL) {
if(!($public_id != NULL xor $text_style != NULL)) {
throw new InvalidArgumentException("Must supply either style parameters or a public_id when providing text parameter in a text $layer_parameter");
}
$text = Cloudinary::smart_escape($text);
$text = str_replace("%2C", "%252C", $text);
$text = str_replace("/", "%252F", $text);
array_push($components, $text);
}
} else {
$public_id = str_replace("/", ":", $public_id);
array_push($components, $public_id);
}
$layer = implode(":", $components);
if($resource_type != "image") array_push($components, $resource_type);
if($type != "upload") array_push($components, $type);
array_push($components, $text_style);
array_push($components, $public_id);
array_push($components, $text);
$layer = implode(":", array_filter($components, 'Cloudinary::is_not_null'));
}
return $layer;
}
Expand Down Expand Up @@ -749,7 +753,7 @@ public static function api_sign_request($params_to_sign, $api_secret) {
foreach ($params_to_sign as $param => $value) {
if (isset($value) && $value !== "") {
if (!is_array($value)) {
$params[$param] = $value;
$params[$param] = $value;
} else if (count($value) > 0) {
$params[$param] = implode(",", $value);
}
Expand Down
1 change: 1 addition & 0 deletions src/Uploader.php
Expand Up @@ -139,6 +139,7 @@ public static function rename($from_public_id, $to_public_id, $options = array()

public static function explicit($public_id, $options = array())
{
$options["public_id"] = $public_id;
$params = Uploader::build_upload_params($options);
return Uploader::call_api("explicit", $params, $options);
}
Expand Down
78 changes: 55 additions & 23 deletions tests/CloudinaryTest.php
Expand Up @@ -745,46 +745,78 @@ public function test_upload_tag(){
cl_upload_tag('image', array("html" => array('class' => 'classy'))));
}

public function test_overlay_options() {
$tests = array(
array("public_id"=>"logo"),"logo",
array("public_id"=>"folder/logo"),"folder:logo",
array("public_id"=>"logo","type"=>"private"),"private:logo",
array("public_id"=>"logo","format"=>"png"),"logo.png",
array("resource_type"=>"video","public_id"=>"cat"),"video:cat",
array("text"=>"Hello World, Nice/ to meet you?", "font_family"=>"Arial", "font_size"=>"18"),"text:Arial_18:Hello%20World%252C%20Nice%252F%20to%20meet%20you%3F",
array("text"=>"Hello World, Nice to meet you?", "font_family"=>"Arial", "font_size"=>"18", "font_weight"=>"bold", "font_style"=>"italic", "letter_spacing"=>4, "line_spacing"=>5),"text:Arial_18_bold_italic_letter_spacing_4_line_spacing_5:Hello%20World%252C%20Nice%20to%20meet%20you%3F",
array("resource_type"=>"subtitles","public_id"=>"sample_sub_en.srt"),"subtitles:sample_sub_en.srt",
array("resource_type"=>"subtitles","public_id"=>"sample_sub_he.srt", "font_family"=>"Arial", "font_size"=>"40"),"subtitles:Arial_40:sample_sub_he.srt"
);
public function layers_options() {
return array(
"public_id" => array(array("public_id"=>"logo"),"logo"),
"public_id with folder" => array(array("public_id"=>"folder/logo"),"folder:logo"),
"private" => array(array("public_id"=>"logo","type"=>"private"),"private:logo"),
"format" => array(array("public_id"=>"logo","format"=>"png"),"logo.png"),
"video" => array(array("resource_type"=>"video","public_id"=>"cat"),"video:cat"),
"text" => array(array("public_id"=>"logo","text"=>"Hello World, Nice to meet you?"),"text:logo:Hello%20World%252C%20Nice%20to%20meet%20you%3F"),
"text with slash" => array(array("text"=>"Hello World, Nice/ to meet you?", "font_family"=>"Arial", "font_size"=>"18"),"text:Arial_18:Hello%20World%252C%20Nice%252F%20to%20meet%20you%3F"),
"text with font family and size" => array(array("text"=>"Hello World, Nice to meet you?", "font_family"=>"Arial", "font_size"=>"18"),"text:Arial_18:Hello%20World%252C%20Nice%20to%20meet%20you%3F"),
"text with style" => array(array("text"=>"Hello World, Nice to meet you?", "font_family"=>"Arial", "font_size"=>"18", "font_weight"=>"bold", "font_style"=>"italic", "letter_spacing"=>4),"text:Arial_18_bold_italic_letter_spacing_4:Hello%20World%252C%20Nice%20to%20meet%20you%3F"),
"subtitles" => array(array("resource_type"=>"subtitles","public_id"=>"sample_sub_en.srt"),"subtitles:sample_sub_en.srt"),
"subtitles with font family and size" => array(array("resource_type"=>"subtitles","public_id"=>"sample_sub_he.srt", "font_family"=>"Arial", "font_size"=>"40"),"subtitles:Arial_40:sample_sub_he.srt")
);
}

/**
* @dataProvider layers_options
*/
public function test_overlay_options($options, $expected) {
$reflector = new ReflectionClass('Cloudinary');
$process_layer = $reflector->getMethod('process_layer');
$process_layer->setAccessible(true);
$i = 0;
while ($i < count($tests)) {
$options = $tests[$i];
$expected = $tests[$i + 1];
$i += 2;
$result = $process_layer->invoke(NULL, $options, "overlay");
$this->assertEquals($result, $expected);
}
$this->assertEquals($expected, $result);
}

public function test_ignore_default_values_in_overlay_options() {
$options = array("public_id"=>"logo","type"=>"upload", "resource_type"=>"image");
$expected= "logo";
$reflector = new ReflectionClass('Cloudinary');
$process_layer = $reflector->getMethod('process_layer');
$process_layer->setAccessible(true);
$result = $process_layer->invoke(NULL, $options, "overlay");
$this->assertEquals($expected, $result);
}


/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Must supply either style parameters or a public_id when providing text parameter in a text overlay
*/
public function test_text_require_public_id_or_style(){
$options = array("overlay"=>array("text"=>"text"));
Cloudinary::cloudinary_url("test", $options);
}

/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Must supply font_family for text in overlay
*/
public function test_overlay_error_1() {
public function test_overlay_style_requires_font_family() {
$options = array("overlay"=>array("text"=>"text", "font_style"=>"italic"));
Cloudinary::cloudinary_url("test", $options);
}

public function resource_types() {
return array(
"image" => array("image"),
"video" => array("video"),
"raw" => array("raw"),
"subtitles" => array("subtitles")
);
}

/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Must supply public_id for for non-text underlay
* @expectedExceptionMessageRegExp #Must supply public_id for .* underlay#
* @dataProvider resource_types
*/
public function test_overlay_error_2() {
$options = array("underlay"=>array("resource_type"=>"video"));
public function test_underlay_require_public_id_for_non_text($resource_type) {
$options = array("underlay"=>array("resource_type"=>$resource_type));
Cloudinary::cloudinary_url("test", $options);
}

Expand Down

0 comments on commit f482147

Please sign in to comment.