Skip to content

Commit 70b1b86

Browse files
committed
fixed img srcset
1 parent 42c7a5e commit 70b1b86

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

src/Plugin/ProxifyPlugin.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,25 @@ public function onCompleted(ProxyEvent $event){
174174
// src= and href=
175175
$str = preg_replace_callback('@(?:src|href)\s*=\s*(["|\'])(.*?)\1@is', array($this, 'html_attr'), $str);
176176

177+
// img srcset
178+
$str = preg_replace_callback('/srcset=\"(.*?)\"/i', function($matches){
179+
$src = $matches[1];
180+
181+
// url_1 1x, url_2 4x, ...
182+
$urls = preg_split('/\s*,\s*/', $src);
183+
foreach($urls as $part){
184+
185+
// TODO: add str_until helper
186+
$pos = strpos($part, ' ');
187+
if($pos !== false){
188+
$url = substr($part, 0, $pos);
189+
$src = str_replace($url, proxify_url($url, $this->base_url), $src);
190+
}
191+
}
192+
193+
return 'srcset="'.$src.'"';
194+
}, $str);
195+
177196
// form
178197
$str = preg_replace_callback('@<form[^>]*action=(["\'])(.*?)\1[^>]*>@i', array($this, 'form_action'), $str);
179198

src/Proxy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function setOutputBuffering($output_buffering){
3636
}
3737

3838
private function header_callback($ch, $headers){
39-
39+
4040
$parts = explode(":", $headers, 2);
4141

4242
// extract status code

src/helpers.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,11 @@ function app_url(){
6868
}
6969

7070
function render_string($str, $vars = array()){
71-
7271
preg_match_all('@{([a-z0-9_]+)}@s', $str, $matches, PREG_SET_ORDER);
7372

7473
foreach($matches as $match){
75-
7674
extract($vars, EXTR_PREFIX_ALL, "_var");
77-
7875
$var_val = ${"_var_".$match[1]};
79-
8076
$str = str_replace($match[0], $var_val, $str);
8177
}
8278

0 commit comments

Comments
 (0)