1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* For the full copyright and license information, please view the LICENSE |
4
|
|
|
* file that was distributed with this source code. |
5
|
|
|
* 08/08/12 15:22 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace CssEmbed; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* CssEmbed |
12
|
|
|
* |
13
|
|
|
* @author Pierre Tachoire <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class CssEmbed |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
const SEARCH_PATTERN = "%url\\(['\" ]*((?!data:|//)[^'\"#\?: ]+)['\" ]*\\)%U"; |
19
|
|
|
const URI_PATTERN = "url(data:%s;base64,%s)"; |
20
|
|
|
|
21
|
|
|
protected $root_dir; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param $root_dir |
25
|
|
|
*/ |
26
|
|
|
public function setRootDir($root_dir) |
27
|
|
|
{ |
28
|
|
|
$this->root_dir = $root_dir; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param $css_file |
33
|
|
|
* @return null|string |
34
|
|
|
* @throws \InvalidArgumentException |
35
|
|
|
*/ |
36
|
|
|
public function embedCss($css_file) |
37
|
|
|
{ |
38
|
|
|
$this->setRootDir(dirname($css_file)); |
39
|
|
|
$return = null; |
40
|
|
|
$handle = fopen($css_file, "r"); |
41
|
|
|
if ($handle === false) { |
42
|
|
|
throw new \InvalidArgumentException(sprintf('Cannot read file %s', $css_file)); |
43
|
|
|
} |
44
|
|
|
while (($line = fgets($handle)) !== false) { |
45
|
|
|
$return .= $this->embedString($line); |
46
|
|
|
} |
47
|
|
|
fclose($handle); |
48
|
|
|
|
49
|
|
|
return $return; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param $content |
54
|
|
|
* @return mixed |
55
|
|
|
*/ |
56
|
|
|
public function embedString($content) |
57
|
|
|
{ |
58
|
|
|
return preg_replace_callback(self::SEARCH_PATTERN, array($this, 'replace'), $content); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param $matches |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
protected function replace($matches) |
66
|
|
|
{ |
67
|
|
|
return $this->embedFile($this->root_dir . DIRECTORY_SEPARATOR . $matches[1]); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param $file |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
protected function embedFile($file) |
75
|
|
|
{ |
76
|
|
|
return sprintf(self::URI_PATTERN, $this->mimeType($file), $this->base64($file)); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param $file |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
protected function mimeType($file) |
84
|
|
|
{ |
85
|
|
|
if (function_exists('mime_content_type')) { |
86
|
|
|
return mime_content_type($file); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if ($info = @getimagesize($file)) { |
90
|
|
|
return($info['mime']); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return 'application/octet-stream'; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param $file |
98
|
|
|
* @return string |
99
|
|
|
* @throws \InvalidArgumentException |
100
|
|
|
*/ |
101
|
|
|
protected function base64($file) |
102
|
|
|
{ |
103
|
|
|
if (is_file($file) === false || is_readable($file) === false) { |
104
|
|
|
throw new \InvalidArgumentException(sprintf('Cannot read file %s', $file)); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return base64_encode(file_get_contents($file)); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|