forked from cloudinary/cloudinary_php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelpers.php
More file actions
249 lines (210 loc) · 10.9 KB
/
Copy pathHelpers.php
File metadata and controls
249 lines (210 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
namespace {
function cl_upload_url($options = array())
{
if (!@$options["resource_type"]) $options["resource_type"] = "auto";
$endpoint = array_key_exists('chunk_size', $options) ? 'upload_chunked' : 'upload';
return Cloudinary::cloudinary_api_url($endpoint, $options);
}
function cl_upload_tag_params($options = array())
{
$params = Cloudinary\Uploader::build_upload_params($options);
if (Cloudinary::option_get($options, "unsigned")) {
$params = array_filter($params,function($v){ return !is_null($v) && ($v !== "" );});
} else {
$params = Cloudinary::sign_request($params, $options);
}
return json_encode($params);
}
function cl_unsigned_image_upload_tag($field, $upload_preset, $options = array())
{
return cl_image_upload_tag($field, array_merge($options, array("unsigned"=>TRUE, "upload_preset"=>$upload_preset)));
}
function cl_image_upload_tag($field, $options = array())
{
return cl_upload_tag($field, $options);
}
function cl_upload_tag($field, $options = array())
{
$html_options = Cloudinary::option_get($options, "html", array());
$classes = array("cloudinary-fileupload");
if (isset($html_options["class"])) {
array_unshift($classes, Cloudinary::option_consume($html_options, "class"));
}
$tag_options = array_merge($html_options, array("type" => "file", "name" => "file",
"data-url" => cl_upload_url($options),
"data-form-data" => cl_upload_tag_params($options),
"data-cloudinary-field" => $field,
"class" => implode(" ", $classes),
));
if (array_key_exists('chunk_size', $options)) $tag_options['data-max-chunk-size'] = $options['chunk_size'];
return '<input ' . Cloudinary::html_attrs($tag_options) . '/>';
}
function cl_form_tag($callback_url, $options = array())
{
$form_options = Cloudinary::option_get($options, "form", array());
$options["callback_url"] = $callback_url;
$params = Cloudinary\Uploader::build_upload_params($options);
$params = Cloudinary::sign_request($params, $options);
$api_url = Cloudinary::cloudinary_api_url("upload", $options);
$form = "<form enctype='multipart/form-data' action='" . $api_url . "' method='POST' " . Cloudinary::html_attrs($form_options) . ">\n";
foreach ($params as $key => $value) {
$form .= "<input " . Cloudinary::html_attrs(array("name" => $key, "value" => $value, "type" => "hidden")) . "/>\n";
}
$form .= "</form>\n";
return $form;
}
// Examples
// cl_image_tag("israel.png", array("width"=>100, "height"=>100, "alt"=>"hello") # W/H are not sent to cloudinary
// cl_image_tag("israel.png", array("width"=>100, "height"=>100, "alt"=>"hello", "crop"=>"fit") # W/H are sent to cloudinary
function cl_image_tag($source, $options = array()) {
$source = cloudinary_url_internal($source, $options);
if (isset($options["html_width"])) $options["width"] = Cloudinary::option_consume($options, "html_width");
if (isset($options["html_height"])) $options["height"] = Cloudinary::option_consume($options, "html_height");
$client_hints = Cloudinary::option_consume($options, "client_hints", Cloudinary::config_get("client_hints"));
$responsive = Cloudinary::option_consume($options, "responsive");
$hidpi = Cloudinary::option_consume($options, "hidpi");
if (($responsive || $hidpi) && !$client_hints) {
$options["data-src"] = $source;
$classes = array($responsive ? "cld-responsive" : "cld-hidpi");
$current_class = Cloudinary::option_consume($options, "class");
if ($current_class) array_unshift($classes, $current_class);
$options["class"] = implode(" ", $classes);
$source = Cloudinary::option_consume($options, "responsive_placeholder", Cloudinary::config_get("responsive_placeholder"));
if ($source == "blank") {
$source = Cloudinary::BLANK;
}
}
$html = "<img ";
if ($source) $html .= "src='$source' ";
$html .= Cloudinary::html_attrs($options) . "/>";
return $html;
}
function fetch_image_tag($url, $options = array()) {
$options["type"] = "fetch";
return cl_image_tag($url, $options);
}
function facebook_profile_image_tag($profile, $options = array()) {
$options["type"] = "facebook";
return cl_image_tag($profile, $options);
}
function gravatar_profile_image_tag($email, $options = array()) {
$options["type"] = "gravatar";
$options["format"] = "jpg";
return cl_image_tag(md5(strtolower(trim($email))), $options);
}
function twitter_profile_image_tag($profile, $options = array()) {
$options["type"] = "twitter";
return cl_image_tag($profile, $options);
}
function twitter_name_profile_image_tag($profile, $options = array()) {
$options["type"] = "twitter_name";
return cl_image_tag($profile, $options);
}
function cloudinary_js_config() {
$params = array();
foreach (Cloudinary::$JS_CONFIG_PARAMS as $param) {
$value = Cloudinary::config_get($param);
if ($value) $params[$param] = $value;
}
return "<script type='text/javascript'>\n" .
"$.cloudinary.config(" . json_encode($params) . ");\n" .
"</script>\n";
}
function cloudinary_url($source, $options = array()) {
return cloudinary_url_internal($source, $options);
}
function cloudinary_url_internal($source, &$options = array()) {
if (!isset($options["secure"])) {
$options["secure"] = ( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' )
|| ( isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https' );
}
return Cloudinary::cloudinary_url($source, $options);
}
function cl_sprite_url($tag, $options = array()) {
if (substr($tag, -strlen(".css")) != ".css") {
$options["format"] = "css";
}
$options["type"] = "sprite";
return cloudinary_url_internal($tag, $options);
}
function cl_sprite_tag($tag, $options = array()) {
return "<link rel='stylesheet' type='text/css' href='" . cl_sprite_url($tag, $options) . "'>";
}
function default_poster_options() { return array( 'format' => 'jpg', 'resource_type' => 'video' ); }
function default_source_types() { return array('webm', 'mp4', 'ogv'); }
# Returns a url for the given source with +options+
function cl_video_path($source, $options = array()) {
$options = array_merge( array('resource_type' => 'video'), $options);
return cloudinary_url_internal($source, $options);
}
# Returns an HTML <tt>img</tt> tag with the thumbnail for the given video +source+ and +options+
function cl_video_thumbnail_tag($source, $options = array()) {
return cl_image_tag($source, array_merge(default_poster_options(),$options));
}
# Returns a url for the thumbnail for the given video +source+ and +options+
function cl_video_thumbnail_path($source, $options = array()) {
$options = array_merge(default_poster_options(), $options);
return cloudinary_url_internal($source, $options);
}
# Creates an HTML video tag for the provided +source+
#
# ==== Options
# * <tt>source_types</tt> - Specify which source type the tag should include. defaults to webm, mp4 and ogv.
# * <tt>source_transformation</tt> - specific transformations to use for a specific source type.
# * <tt>poster</tt> - override default thumbnail:
# * url: provide an ad hoc url
# * options: with specific poster transformations and/or Cloudinary +:public_id+
#
# ==== Examples
# cl_video_tag("mymovie.mp4")
# cl_video_tag("mymovie.mp4", array('source_types' => 'webm'))
# cl_video_tag("mymovie.ogv", array('poster' => "myspecialplaceholder.jpg"))
# cl_video_tag("mymovie.webm", array('source_types' => array('webm', 'mp4'), 'poster' => array('effect' => 'sepia')))
function cl_video_tag($source, $options = array()) {
$source = preg_replace('/\.(' . implode('|', default_source_types()) . ')$/', '', $source);
$source_types = Cloudinary::option_consume($options, 'source_types', array());
$source_transformation = Cloudinary::option_consume($options, 'source_transformation', array());
$fallback = Cloudinary::option_consume($options, 'fallback_content', '');
if (empty($source_types)) {
$source_types = default_source_types();
}
$video_options = $options;
if (array_key_exists('poster', $video_options)) {
if (is_array($video_options['poster'])) {
if (array_key_exists('public_id', $video_options['poster'])) {
$video_options['poster'] = cloudinary_url_internal($video_options['poster']['public_id'], $video_options['poster']);
} else {
$video_options['poster'] = cl_video_thumbnail_path($source, $video_options['poster']);
}
}
} else {
$video_options['poster'] = cl_video_thumbnail_path($source, $options);
}
if (empty($video_options['poster'])) unset($video_options['poster']);
$html = '<video ';
if (!array_key_exists('resource_type', $video_options)) $video_options['resource_type'] = 'video';
$multi_source = is_array($source_types);
if (!$multi_source){
$source .= '.' . $source_types;
}
$src = cloudinary_url_internal($source, $video_options);
if (!$multi_source) $video_options['src'] = $src;
if (isset($video_options["html_width"])) $video_options['width'] = Cloudinary::option_consume($video_options, 'html_width');
if (isset($video_options['html_height'])) $video_options['height'] = Cloudinary::option_consume($video_options, 'html_height');
$html .= Cloudinary::html_attrs($video_options ) . '>';
if ($multi_source) {
foreach($source_types as $source_type) {
$transformation = Cloudinary::option_consume($source_transformation, $source_type, array());
$transformation = array_merge($options, $transformation);
$src = cl_video_path($source . '.' . $source_type, $transformation);
$video_type = (($source_type == 'ogv') ? 'ogg' : $source_type);
$mime_type = "video/$video_type";
$html .= '<source '. Cloudinary::html_attrs(array('src' => $src, 'type' => $mime_type)) . '>';
}
}
$html .= $fallback;
$html .= '</video>';
return $html;
}
}