PageRenderTime 33ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/application/libraries/scaffold/extensions/Embed/Embed.php

https://github.com/hgmnz/Bogota-Conf-Website
PHP | 95 lines | 49 code | 11 blank | 35 comment | 7 complexity | d1336363e0340b8f72d06576c750db21 MD5 | raw file
  1. <?php
  2. /**
  3. * Scaffold_Extension_Embed
  4. *
  5. * Uses the Function extension to create a new CSS function called embed().
  6. * This lets you embed images and other url() data in the CSS as base64 encoded strings.
  7. *
  8. * @package Scaffold
  9. * @author Anthony Short <anthonyshort@me.com>
  10. * @copyright 2009-2010 Anthony Short. All rights reserved.
  11. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  12. * @link https://github.com/anthonyshort/csscaffold/master
  13. */
  14. class Scaffold_Extension_Embed extends Scaffold_Extension
  15. {
  16. /**
  17. * Used to decide which mime type to use
  18. * @var array
  19. */
  20. private $_extensions = array
  21. (
  22. 'images' => array('gif','jpg','jpeg','png'),
  23. 'fonts' => array('otf','ttf','woff')
  24. );
  25. /**
  26. * Parses embed() functions within the CSS.
  27. * @access public
  28. * @param $source
  29. * @param $scaffold
  30. * @return string
  31. */
  32. public function process($source,$scaffold)
  33. {
  34. // We can only embed for file sources
  35. if($source->type != 'file') return;
  36. foreach($scaffold->helper->css->find_functions('embed',$source->contents) as $found)
  37. {
  38. // Get the full path to the file relative to the source
  39. $path = $source->find($url);
  40. // The file doesn't exist
  41. if($path === false)
  42. continue;
  43. $id = 'embeds/'.md5($path);
  44. $ext = pathinfo($path, PATHINFO_EXTENSION);
  45. $mod_time = filemtime($path);
  46. $mime = $this->_get_mime($ext);
  47. // If it's a file we can actually use
  48. if($mime === false)
  49. continue;
  50. // Try and load it from the cache
  51. $data = $scaffold->cache->get($id);
  52. // If the cached version has expired
  53. if($data === false OR $mod_time > $data->last_modified)
  54. {
  55. $data = base64_encode(file_get_contents($path));
  56. $scaffold->cache->set($id,$data,$mod_time,false);
  57. }
  58. else
  59. {
  60. $data = $data->contents;
  61. }
  62. $string = "url(data:$mime;base64,$data)";
  63. $source->contents = str_replace($found['string'],$string,$source->contents);
  64. }
  65. }
  66. /**
  67. * Get file MIME-type
  68. * @access private
  69. * @param $ext
  70. * @return mixed
  71. */
  72. private function _get_mime($ext)
  73. {
  74. if(in_array($ext,$this->_extensions['images']))
  75. {
  76. return 'image/'.$ext;
  77. }
  78. if(in_array($ext,$this->_extensions['fonts']))
  79. {
  80. return 'application/octet-stream';
  81. }
  82. return false;
  83. }
  84. }