function imgix_replace_content_cdn($content) { global $imgix_options; $slink = ensure_valid_url($imgix_options['cdn_link']); $auto_format = $imgix_options['auto_format']; $auto_enhance = $imgix_options['auto_enhance']; if (!empty($slink)) { // 1) Apply imgix host // img src tags $content = str_replace('src="' . home_url('/') . 'wp-content/', 'src="' . $slink . 'wp-content/', $content); $content = str_replace('src=\'' . home_url('/') . 'wp-content/', 'src=\'' . $slink . 'wp-content/', $content); // img href tags $content = str_replace('href="' . home_url('/') . 'wp-content/', 'href="' . $slink . 'wp-content/', $content); $content = str_replace('href=\'' . home_url('/') . 'wp-content/', 'href=\'' . $slink . 'wp-content/', $content); $data_w_h = imgix_extract_img_details($content); // 2) Handle Auto options $autos = array(); $auto_params = ''; if ($auto_format === "1") { array_push($autos, "format"); } if ($auto_enhance === "1") { array_push($autos, "enhance"); } if (sizeof($autos) > 0) { $auto_params = 'auto=' . implode(',', $autos); } // 3) Apply the h/w img params and any that already existed (text html edits) foreach ($data_w_h as $k => $v) { $extra = strlen($v['extra']) > 0 ? '&' . $v['extra'] : ''; $to_replace = $v['raw']; $new_url = '.' . $v['type'] . '?h=' . $v['h'] . '&w=' . $v['w'] . $extra; $content = str_replace($to_replace, $new_url, $content); } // 4) Apply the auto_params $imgs = imgix_extract_imgs($content); foreach ($imgs as $k => $v) { if (strlen($v['params']) > 0) { $new_url = $v['url'] . '?' . $auto_params . '&' . $v['params']; $to_replace = $v['url'] . '?' . $v['params']; } else { $to_replace = $v['url']; $new_url = $v['url'] . '?' . $auto_params; } $to_replace .= '"'; $new_url .= '"'; if (strlen($to_replace) > 0) { $content = str_replace($to_replace, $new_url, $content); } } return $content; } return $content; }
function test_imgix_extract_img_details_with_h_and_w_params() { $content = "<img src=\"https://google.com/cats-400x300.gif?h=999&w=333\" />"; $expected = array(array('raw' => '-400x300.gif?h=999&w=333', 'w' => '400', 'h' => '300', 'type' => 'gif', 'extra' => 'h=999&w=333')); $result = imgix_extract_img_details($content); $this->assertEquals($expected, $result); }
function test_imgix_extract_img_details_img_tag_double_quote() { $content = "<img src=\"https://google.com/cats-400x300.gif\" />"; $expected = array(array('raw' => '-400x300.gif', 'w' => '400', 'h' => '300', 'type' => 'gif', 'extra' => '')); $result = imgix_extract_img_details($content); $this->assertEquals($expected, $result); }
function imgix_replace_non_wp_images($content) { list($content, $match) = replace_host($content, true); if ($match) { //Apply image-tag-encoded params for every image in $content. foreach (imgix_extract_img_details($content) as $img) { $to_replace = $img['raw']; $extra_params = $img['extra'] ? '&' . $img['extra'] : ''; $new_url = '.' . $img['type'] . '?h=' . $img['h'] . '&w=' . $img['w'] . $extra_params; $content = str_replace($to_replace, $new_url, $content); } // Apply global parameters. $g_params = get_global_params_string(); foreach (imgix_extract_imgs($content) as $img_url) { $content = apply_parameters_to_url($img_url, $g_params, $content); } } return $content; }