コード例 #1
0
ファイル: Functions.php プロジェクト: renaatdemuynck/less.php
 public function datauri($mimetypeNode, $filePathNode = null)
 {
     $filePath = $filePathNode ? $filePathNode->value : null;
     $mimetype = $mimetypeNode->value;
     $args = 2;
     if (!$filePath) {
         $filePath = $mimetype;
         $args = 1;
     }
     $filePath = str_replace('\\', '/', $filePath);
     if (Less_Environment::isPathRelative($filePath)) {
         if (Less_Parser::$options['relativeUrls']) {
             $temp = $this->currentFileInfo['currentDirectory'];
         } else {
             $temp = $this->currentFileInfo['entryPath'];
         }
         if (!empty($temp)) {
             $filePath = Less_Environment::normalizePath(rtrim($temp, '/') . '/' . $filePath);
         }
     }
     // detect the mimetype if not given
     if ($args < 2) {
         /* incomplete
         			$mime = require('mime');
         			mimetype = mime.lookup(path);
         
         			// use base 64 unless it's an ASCII or UTF-8 format
         			var charset = mime.charsets.lookup(mimetype);
         			useBase64 = ['US-ASCII', 'UTF-8'].indexOf(charset) < 0;
         			if (useBase64) mimetype += ';base64';
         			*/
         $mimetype = Less_Mime::lookup($filePath);
         $charset = Less_Mime::charsets_lookup($mimetype);
         $useBase64 = !in_array($charset, array('US-ASCII', 'UTF-8'));
         if ($useBase64) {
             $mimetype .= ';base64';
         }
     } else {
         $useBase64 = preg_match('/;base64$/', $mimetype);
     }
     if (file_exists($filePath)) {
         $buf = @file_get_contents($filePath);
     } else {
         $buf = false;
     }
     // IE8 cannot handle a data-uri larger than 32KB. If this is exceeded
     // and the --ieCompat flag is enabled, return a normal url() instead.
     $DATA_URI_MAX_KB = 32;
     $fileSizeInKB = round(strlen($buf) / 1024);
     if ($fileSizeInKB >= $DATA_URI_MAX_KB) {
         $url = new Less_Tree_Url($filePathNode ? $filePathNode : $mimetypeNode, $this->currentFileInfo);
         return $url->compile($this);
     }
     if ($buf) {
         $buf = $useBase64 ? base64_encode($buf) : rawurlencode($buf);
         $filePath = '"data:' . $mimetype . ',' . $buf . '"';
     }
     return new Less_Tree_Url(new Less_Tree_Anonymous($filePath));
 }
コード例 #2
0
 public function datauri($mimetypeNode, $filePathNode = null) {
   $filePath = ($filePathNode ? $filePathNode->value : null);
   $mimetype = $mimetypeNode->value;
   $args = 2;
   if (!$filePath) {
     $filePath = $mimetype;
     $args = 1;
   }$filePath = str_replace('\\', '/', $filePath);
   if (Less_Environment::isPathRelative($filePath)) {
     if (Less_Parser::$options['relativeUrls']) {
       $temp = $this->currentFileInfo['currentDirectory'];
     } else {
       $temp = $this->currentFileInfo['entryPath'];
     }if (!empty($temp)) {
       $filePath = Less_Environment::normalizePath(rtrim($temp, '/') . '/' . $filePath);
     }
   }if ($args < 2) {
     $mimetype = Less_Mime::lookup($filePath);
     $charset = Less_Mime::charsets_lookup($mimetype);
     $useBase64 = !in_array($charset, array('US-ASCII', 'UTF-8'));
     if ($useBase64) {
       $mimetype.=';base64';
     }
   } else {
     $useBase64 = preg_match('/;base64$/', $mimetype);
   }if (file_exists($filePath)) {
     $buf = @file_get_contents($filePath);
   } else {
     $buf = false;
   }$DATA_URI_MAX_KB = 32;
   $fileSizeInKB = round(strlen($buf) / 1024);
   if ($fileSizeInKB >= $DATA_URI_MAX_KB) {
     $url = new Less_Tree_Url(($filePathNode ? $filePathNode : $mimetypeNode), $this->currentFileInfo);
     return $url->compile($this);
   }if ($buf) {
     $buf = $useBase64 ? base64_encode($buf) : rawurlencode($buf);
     $filePath = '"data:' . $mimetype . ',' . $buf . '"';
   }return new Less_Tree_Url(new Less_Tree_Anonymous($filePath));
 }