signature.php 826 B

123456789101112131415161718192021222324252627282930
  1. <?php
  2. $key = '943b421c9eb07c830af81030552c86009268de4e532ba2ee2eab8247c6da0881';
  3. $salt = '520f986b998545b4785e0defbc4f3c1203f22de2374a3d53cb7a7fe9fea309c5';
  4. $keyBin = pack("H*" , $key);
  5. if(empty($keyBin)) {
  6. die('Key expected to be hex-encoded string');
  7. }
  8. $saltBin = pack("H*" , $salt);
  9. if(empty($saltBin)) {
  10. die('Salt expected to be hex-encoded string');
  11. }
  12. $resize = 'fill';
  13. $width = 300;
  14. $height = 300;
  15. $gravity = 'no';
  16. $enlarge = 1;
  17. $extension = 'png';
  18. $url = 'http://img.example.com/pretty/image.jpg';
  19. $encodedUrl = rtrim(strtr(base64_encode($url), '+/', '-_'), '=');
  20. $path = "/{$resize}/{$width}/{$height}/{$gravity}/{$enlarge}/{$encodedUrl}.{$extension}";
  21. $signature = rtrim(strtr(base64_encode(hash_hmac('sha256', $saltBin.$path, $keyBin, true)), '+/', '-_'), '=');
  22. print(sprintf("/%s%s", $signature, $path));