【发布时间】:2012-12-27 13:52:28
【问题描述】:
我有这个代码:
function toDataUri( $html )
{
# convert css URLs to data URIs
$html = preg_replace_callback( "#(url\([\'\"]?)([^\"\'\)]+)([\"\']?\))#", 'create_data_uri', $html );
return $html;
}
// callback function
private function create_data_uri( $matches )
{
$filetype = explode( '.', $matches[ 2 ] );
$filetype = trim(strtolower( $filetype[ count( $filetype ) - 1 ] ));
// replace ?whatever=value from extensions
$filetype = preg_replace('#\?.*#', '', $filetype);
$datauri = $matches[ 2 ];
$data = get_file_contents( $datauri );
if (! $data) return $matches[ 0 ];
$data = base64_encode( $data );
//compile and return a data: URI with the encoded image data
return $matches[ 1 ] . "data:image/$filetype;base64,$data" . $matches[ 3 ];
}
它基本上在 HTML 文件中搜索格式为 url(path) 的 URL,并用 base 64 Data URIS 替换它们。
问题是,如果输入的 html 是几公斤,例如 10kb,则返回最终响应需要很长时间。在这种情况下我们可以做任何优化吗?或者您有任何其他解决方案,当给定 html 时,它会搜索 url(path) 匹配并将它们转换为数据 uris?
【问题讨论】:
-
问题中的代码仍然引用了很多问题中没有的代码。你的代码中有
get_file_contents(或者看起来你可能有),这将到目前为止比 preg_match 调用花费更长的时间。您应该分析您的代码,以便您知道时间在哪里,而不是假设它是 preg_match 调用(不太可能)
标签: php html performance optimization