Created
November 18, 2020 07:48
-
-
Save bsingr/5c309e2a8107159366c7ed4ff9a85f69 to your computer and use it in GitHub Desktop.
cloudinary signing urls
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function urlSafeBse64Encode($string) { | |
$data = base64_encode($string); | |
# base64 strings can end in several = chars. These need to be translated into a number | |
$no_of_eq = substr_count($data, "="); | |
$data = str_replace("=", "", $data); | |
$data = $data.$no_of_eq; | |
# then replace all non-url safe characters | |
$data = str_replace(array('+','/'), array('-','_'), $data); | |
return $data; | |
} | |
// see https://cloudinary.com/documentation/advanced_url_delivery_options#generating_delivery_url_signatures | |
function cloudinarySignature($payload) { | |
$CLOUDINARY_SIGNING_SECRET = 'SECRET'; | |
if (!$CLOUDINARY_SIGNING_SECRET || $CLOUDINARY_SIGNING_SECRET == "") { | |
return; | |
} | |
var_dump($payload); | |
var_dump(sha1($payload, true)); | |
return 's--' . substr(base64_encode(sha1($payload . $CLOUDINARY_SIGNING_SECRET, true)), 0, 8) . '--'; | |
} | |
var_dump(cloudinarySignature("c_scale,q_auto,dpr_auto,f_auto,w_auto:breakpoints_480_3840_128_8/local-env/2020-11/crop-sample_6.jpg")); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'base64' | |
require 'digest/sha1' | |
transformation = "c_scale,q_auto,dpr_auto,f_auto,w_auto:100:330" | |
public_id = "local-env/2020-11/crop-sample-2x_uhl18c.jpg" | |
account_name = "ACCOUNT_NAME" | |
secret = "SECRET" | |
to_sign = ([transformation, public_id]).join("/") | |
p to_sign | |
sh1 = Digest::SHA1.digest(to_sign + secret) | |
p sh1 | |
signature = 's--' + Base64.urlsafe_encode64(sh1)[0,8] + '--' | |
url = 'https://res.cloudinary.com/' + account_name + '/image/upload/' + ([signature, to_sign]).join("/") | |
p url | |
p signature |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment