akoimeexx

Abstract Data URI class

Feb 4th, 2013
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.74 KB | None | 0 0
  1. <?php
  2.     /**
  3.      * (string) DataURI::Encode( (string) $file, (boolean) [$b64=true])
  4.      *  Parameters
  5.      *      (string) $file: path of file to be encoded
  6.      *      (boolean) $b64: base64-encoding flag
  7.      *  
  8.      *  Returns a valid data uri (string) on success, (boolean) false
  9.      *  on failure.
  10.      *  
  11.      *  Example: <img src="<?= DataURI::Encode("image.png"); ?> />
  12.      *
  13.      *
  14.      * (string) DataURI::Decode( (string) $datauri, (string) [$tmp_suffix='raw'])
  15.      *  Parameters
  16.      *      (string) $datauri: valid data uri to be decoded
  17.      *      (string) $tmp_suffix: default fallback suffix if a
  18.      *           proper filename extension cannot be determined
  19.      *  
  20.      *  Returns the path and filename (string) of the decoded file on
  21.      *  success, (boolean) false on failure.
  22.      *  
  23.      *  Example: echo DataURI::Decode('data:plaintext/txt;charset=UTF-8;base64,My4xNDE1OTI2NQ==');
  24.      *       // returns /tmp/plaintext-??????.txt, with contents of
  25.      *       // 3.14159265
  26.      */
  27.     abstract class DataURI {
  28.         public static function Encode($file, $b64=true) {
  29.             try {
  30.                 $mt = DataURI::get_mime_type($file);
  31.                 $data = file_get_contents($file);
  32.                 if($b64) {
  33.                     $b64 = ';base64';
  34.                     $data = base64_encode($data);
  35.                 }
  36.                 return sprintf("data:%s%s,%s", $mt, $b64, $data);
  37.             } catch (Exception $e) {
  38.                 error_log($e->getMessage());
  39.             }
  40.             return false;
  41.         }
  42.         public static function Decode($datauri, $tmp_suffix = 'raw') {
  43.             try {
  44.                 $metadata = explode(';', substr($datauri, 0, strpos($datauri, ',')));
  45.                 $data = substr($datauri, strpos($datauri, ',') + 1);
  46.                 if(array_search('base64', $metadata) !== false) { $data = base64_decode($data); }
  47.                
  48.                 $tmp_prefix = 'data';
  49.                 if(($mimetype = substr($metadata[0], strpos($metadata[0], ':') + 1)) != '') {
  50.                     $tmp_prefix = substr($mimetype, 0, strpos($mimetype, '/'));
  51.                     $tmp_suffix = substr($mimetype, strrpos($mimetype, '/') + 1);
  52.                 }
  53.                
  54.                 while (true) {
  55.                     $tmp_filename = tempnam(sys_get_temp_dir(), "$tmp_prefix-") . ".$tmp_suffix";
  56.                     if (!file_exists($tmp_filename)) break;
  57.                 }
  58.                 if(file_put_contents($tmp_filename, $data) !== false) {
  59.                     return $tmp_filename;
  60.                 }
  61.             } catch (Exception $e) {
  62.                 error_log($e->getMessage());
  63.             }
  64.             return false;
  65.         }
  66.         private static function get_mime_type($file, $def_mime_type = 'application/octet') {
  67.             $mimetype = $def_mime_type;
  68.             if(function_exists('finfo_file')) {
  69.                 // Use finfo_file to grab mimetype, if possible
  70.                 $finfo = finfo_open(FILEINFO_MIME_TYPE);
  71.                 $mimetype = finfo_file($finfo, $file);
  72.                 finfo_close($finfo);
  73.             } elseif(function_exists('mime_content_type')) {
  74.                 // finfo_file is unavailable, attempt to use deprecated mime_content_type
  75.                 $mimetype = mime_content_type($file);
  76.             }
  77.             return $mimetype;
  78.         }
  79.     }
  80. ?>
Advertisement
Add Comment
Please, Sign In to add comment