在对应的主题functions.php文件中加入以下代
define("LOCAL_HOST","http://www.cooljun.cn"); define("CDN_HOST","http://img.cooljun.cn"); define("CDN_NAME","qiniu"); define("QN_HOST","http://wp.cooljun.cn"); add_action('wp_loaded','coojun_ob_start'); function coojun_ob_start() { add_action('generate_rewrite_rules','remote_image_generate_rewrite_rules'); add_filter('query_vars', 'remote_image_query_vars'); add_action('template_redirect', 'remote_image_template_redirect', 5); add_filter('the_content', 'cdn_content',1); ob_start('qiniu_cdn_replace'); }
function qiniu_cdn_replace($html){ $local_host = 'http://www.cooljun.cn'; //博客域名 $qiniu_host = 'http://img.cooljun.cn'; //七牛域名 $cdn_exts = 'js|css|png|jpg|jpeg|gif|ico'; //扩展名(使用|分隔) $cdn_dirs = 'wp-content|wp-includes'; //目录(使用|分隔) $cdn_dirs = str_replace('-', '\-', $cdn_dirs); if ($cdn_dirs) { $regex = '/' . str_replace('/', '\/', $local_host) . '\/((' . $cdn_dirs . ')\/[^\s\?\\\'\"\;\>\<]{1,}.(' . $cdn_exts . '))([\"\\\'\s\?]{1})/'; $html = preg_replace($regex, $qiniu_host . '/$1$4', $html); } else { $regex = '/' . str_replace('/', '\/', $local_host) . '\/([^\s\?\\\'\"\;\>\<]{1,}.(' . $cdn_exts . '))([\"\\\'\s\?]{1})/'; $html = preg_replace($regex, $qiniu_host . '/$1$3', $html); } return $html; }
// 远程图片的 Rewrite 规则,第三方插件需要 flush rewrite function remote_image_generate_rewrite_rules($wp_rewrite){ $new_rules[CDN_NAME.'/([^/]+)/image/([^/]+)\.([^/]+)?$'] = 'index.php?p=$matches[1]&'.CDN_NAME.'_image=$matches[2]&'.CDN_NAME.'_image_type=$matches[3]'; $wp_rewrite->rules = $new_rules + $wp_rewrite->rules; } // 远程图片的 Query String 变量 function remote_image_query_vars($public_query_vars) { $public_query_vars[] = CDN_NAME.'_image'; $public_query_vars[] = CDN_NAME.'_image_type'; return $public_query_vars; }
// 远程图片加载模板 function remote_image_template_redirect(){ $remote_image = get_query_var(CDN_NAME.'_image'); $remote_image_type = get_query_var(CDN_NAME.'_image_type'); if($remote_image && $remote_image_type){ global $post; $post = get_post(get_query_var('p')); $md5 = get_query_var('qiniu_image'); $type = get_query_var('qiniu_image_type'); if(!$post){ wp_die('该日志不存在','该日志不存在',array( 'response' => 404 )); } $preg = preg_match_all('|<img.*?src=[\'"](.*?)[\'"].*?>|i', do_shortcode($post->post_content), $matches); $url = ''; if ($preg) { foreach ($matches[1] as $image_url) { if($md5 == md5($image_url)){ $url = $image_url; break; } } } if(!$url){ wp_die('该日志没有图片','该日志没有图片',array( 'response' => 404 )); } if(isset($_GET['url']) && $_GET['url']){ echo $url; }else{ if($url){ switch ($type) { case 'jpg': header('Content-Type: image/jpeg'); $img = imagecreatefromstring(file_get_contents($url)); imagejpeg($img,null,100); break; case 'png': header("Content-Type: image/png"); $img = imagecreatefrompng($url); $background = imagecolorallocate($img, 0, 0, 0); imagecolortransparent($img, $background); imagealphablending($img, false); imagesavealpha($img, true); imagepng($img); break; case 'gif': header('Content-Type: image/gif'); $img = imagecreatefromgif($url); imagegif($img); break; default: # code... break; } }else{ wp_die('该日志没有图片','该日志没有图片',array( 'response' => 404 )); } } exit; } }
function cdn_content($content){ $cdn_content = preg_replace_callback('|<img.*?src=[\'"](.*?)[\'"].*?>|i','cdn_content_image',$content); return $cdn_content; } function cdn_content_image($matches){ $img_url = trim($matches[1]); if(empty($img_url)) return; $remote_img_url = get_content_remote_img_url($img_url); $result = str_replace($img_url, $remote_img_url, $matches[0]); return $result; } // 获取远程图片 function get_content_remote_img_url($img_url){ if(strpos($img_url, QN_HOST) === false&&strpos($img_url, LOCAL_HOST) === false && strpos($img_url, CDN_HOST) === false ){ // 默认不去抓取远程图片,要第三方 CDN 启用才会 $img_type = strtolower(pathinfo($img_url, PATHINFO_EXTENSION)); // if($img_type != 'gif'){ $img_type = ($img_type == 'png')?'png':'jpg'; $img_url = CDN_HOST.'/'.CDN_NAME.'/'.get_the_ID().'/image/'.md5($img_url).'.'.$img_type; // } } $img_url = str_replace(LOCAL_HOST, CDN_HOST, $img_url); return $img_url; }
文件下载:
转载请注明:cooljun小窝 » wordpress代码实现七牛云加速和远程图片云加速