signature-truncated.php 955 B

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