Begin主题完美适配又拍云缩略图,兼容输出webp格式图片 加速网站访问

自然 2019年9月25日16:33:15 评论

这篇文章是对上一篇文章《Begin主题使用又拍云(Upyun)图像处理功能-替换thumbnail缩略图,提升服务器性能》的扩展与优化,本次优化意在于完美兼容Begin主题与适配全新的webp图片格式。上一篇文章中的取用方式存在一定问题,不适合小白站长操作。本文进一步完善了使用说明,让小白站长也可以直接使用又拍云的缩略图服务。

Begin主题完美适配又拍云缩略图,兼容输出webp格式图片 加速网站访问

图像处理优化参数

upyun图片处理开发文档:https://help.upyun.com/knowledge-base/image/

代码中应用到了如下几个参数:

!/both/'.zm_get_option('img_w').'x'.zm_get_option('img_h').'/format/webp/lossless/true
!/both/'.zm_get_option('grid_w').'x'.zm_get_option('grid_h').'/format/webp/lossless/true
!/both/900x350/format/webp/lossless/true

/both/900x350/   #设置图片宽x高,高宽不足时自动缩放后裁剪
/format/webp/    #输出图片为webp格式
/lossless/true   #输出图片为无损webp,仅和/format/webp/一起有效

如果你需要自行定制参数,只需要依照开发文档对以上参数进行查找修改即可。

代码修改

文件的修改是源于源文件中的thumbnail-oss.php,自然已经将代码打包放在文章底部,可以直接下载使用。

展开

<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
// 标准缩略图
function zm_thumbnail() {
	$random_img = explode(',' , zm_get_option('random_image_url'));
	$random_img_array = array_rand($random_img);
	$src = $random_img[$random_img_array];
	global $post;
	if ( get_post_meta($post->ID, 'thumbnail', true) ) {
		$image = get_post_meta($post->ID, 'thumbnail', true);
		echo '<a href="'.get_permalink().'"><img src=';
		if (zm_get_option('manual_thumbnail')) {
			echo get_template_directory_uri().'/thumbnail.php?src='.$image.'&w='.zm_get_option('img_w').'&h='.zm_get_option('img_h').'&a='.zm_get_option('crop_top').'&zc=1';
		} else {
			echo $image;
		}
		echo ' alt="'.$post->post_title .'" /></a>';
	} else {
		if ( has_post_thumbnail() ) {
			echo '<a href="'.get_permalink().'">';
			if (zm_get_option('clipping_thumbnails')) {
				$full_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
				echo '<img src="'.get_template_directory_uri().'/thumbnail.php?src='.$full_image_url[0].'&w='.zm_get_option('img_w').'&h='.zm_get_option('img_h').'&a='.zm_get_option('crop_top').'&zc=1" alt="'.get_the_title().'">';
			} else {
				the_post_thumbnail('content', array('alt' => get_the_title()));
			}
			echo '</a>';
		} else {
			$content = $post->post_content;
			preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
			$n = count($strResult[1]);
			if($n > 0){
					echo '<a href="'.get_permalink().'"><img src="'.$strResult[1][0].'!/both/'.zm_get_option('img_w').'x'.zm_get_option('img_h').'/format/webp/lossless/true" alt="'.$post->post_title .'" /></a>';			} else { 
				if ( zm_get_option('rand_img_n') ) {
					$random = mt_rand(1, zm_get_option('rand_img_n'));
				} else { 
					$random = mt_rand(1, 5);
				}
				if (zm_get_option('clipping_rand_img')) {
					echo '<a href="'.get_permalink().'"><img src="'.get_template_directory_uri().'/thumbnail.php?src='.$src.'&w='.zm_get_option('img_w').'&h='.zm_get_option('img_h').'&a='.zm_get_option('crop_top').'&zc=1" alt="'.$post->post_title .'" /></a>';
				} else { 
					echo '<a href="'.get_permalink().'"><img src="'. $src .'" alt="'.$post->post_title .'" /></a>';
				}
			}
		}
	}
}

function zm_thumbnail_h() {
	$random_img = explode(',' , zm_get_option('random_image_url'));
	$random_img_array = array_rand($random_img);
	$src = $random_img[$random_img_array];
	global $post;
	if ( get_post_meta($post->ID, 'thumbnail', true) ) {
		$image = get_post_meta($post->ID, 'thumbnail', true);
		echo '<span class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original=';
		if (zm_get_option('manual_thumbnail')) {
			echo get_template_directory_uri().'/thumbnail.php?src='.$image.'&w='.zm_get_option('img_w').'&h='.zm_get_option('img_h').'&a='.zm_get_option('crop_top').'&zc=1';
		} else {
			echo $image;
		}
		echo ' alt="'.$post->post_title .'" /></a></span>';
	} else {
		if ( has_post_thumbnail() ) {
			echo '<span class="load"><a href="'.get_permalink().'">';
			if (zm_get_option('clipping_thumbnails')) {
				$full_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
				
				echo '<img src="' . get_template_directory_uri() . '/img/loading.png" data-original='.get_template_directory_uri().'/thumbnail.php?src='.$full_image_url[0].'&w='.zm_get_option('img_w').'&h='.zm_get_option('img_h').'&a='.zm_get_option('crop_top').'&zc=1 alt="'.get_the_title().'">';
			} else {
				the_post_thumbnail('content', array('alt' => get_the_title()));
			}
			echo '</a></span>';
		} else {
			$content = $post->post_content;
			preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
			$n = count($strResult[1]);
			if($n > 0){
					echo '<span class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'.$strResult[1][0].'!/both/'.zm_get_option('img_w').'x'.zm_get_option('img_h').'/format/webp/lossless/true" alt="'.$post->post_title .'" /></a></span>';
				} else { 
				if ( zm_get_option('rand_img_n') ) {
					$random = mt_rand(1, zm_get_option('rand_img_n'));
				} else { 
					$random = mt_rand(1, 5);
				}
				if (zm_get_option('clipping_rand_img')) {
					echo '<span class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'.get_template_directory_uri().'/thumbnail.php?src='.$src.'&w='.zm_get_option('img_w').'&h='.zm_get_option('img_h').'&a='.zm_get_option('crop_top').'&zc=1" alt="'.$post->post_title .'" /></a></span>';
				} else { 
					echo '<span class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'. $src .'" alt="'.$post->post_title .'" /></a></span>';
				}
			}
		}
	}
}

// 分类宽缩略图
function zm_long_thumbnail() {
	global $post;
	if ( get_post_meta($post->ID, 'long_thumbnail', true) ) {
		$image = get_post_meta($post->ID, 'long_thumbnail', true);
		echo '<a href="'.get_permalink().'"><img src=';
		if (zm_get_option('manual_thumbnail')) {
			echo get_template_directory_uri().'/thumbnail.php?src='.$image.'&w='.zm_get_option('img_k_w').'&h='.zm_get_option('img_k_h').'&a='.zm_get_option('crop_top').'&zc=1';
		} else {
			echo $image;
		}
		echo ' alt="'.$post->post_title .'" /></a>';
	} else {
		if ( has_post_thumbnail() ) {
			if (zm_get_option('clipping_thumbnails')) {
				$full_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
				echo '<img src="'.get_template_directory_uri().'/thumbnail.php?src='.$full_image_url[0].'&w='.zm_get_option('img_k_w').'&h='.zm_get_option('img_k_h').'&a='.zm_get_option('crop_top').'&zc=1" alt="'.get_the_title().'">';
			} else {
				the_post_thumbnail('long', array('alt' => get_the_title()));
			}
		} else {
			$content = $post->post_content;
			preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
			$n = count($strResult[1]);
			if($n > 0){
				echo '<a href="'.get_permalink().'"><img src="'.$strResult[1][0].'!/both/'.zm_get_option('img_k_w').'x'.zm_get_option('img_k_h').'/format/webp/lossless/true" alt="'.$post->post_title .'" /></a>';
			} else { 
				echo '<a class="random-img" href="'.get_permalink().'"><img src="'.get_template_directory_uri().'/img/random/long.jpg" alt="'.$post->post_title .'" /></a>';
			}
		}
	}
}

function zm_long_thumbnail_h() {
	global $post;
	if ( get_post_meta($post->ID, 'long_thumbnail', true) ) {
		$image = get_post_meta($post->ID, 'long_thumbnail', true);
		echo '<span class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/long.png" data-original=';
		if (zm_get_option('manual_thumbnail')) {
			echo get_template_directory_uri().'/thumbnail.php?src='.$image.'&w='.zm_get_option('img_k_w').'&h='.zm_get_option('img_k_h').'&a='.zm_get_option('crop_top').'&zc=1';
		} else {
			echo $image;
		}
		echo ' alt="'.$post->post_title .'" /></a></span>';
	} else {
		if ( has_post_thumbnail() ) {
			echo '<span class="load"><a href="'.get_permalink().'">';
			if (zm_get_option('clipping_thumbnails')) {
				$full_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
				echo '<img src="' . get_template_directory_uri() . '/img/loading.png" data-original='.get_template_directory_uri().'/thumbnail.php?src='.$full_image_url[0].'&w='.zm_get_option('img_k_w').'&h='.zm_get_option('img_k_h').'&a='.zm_get_option('crop_top').'&zc=1 alt="'.get_the_title().'">';
			} else {
				the_post_thumbnail('long', array('alt' => get_the_title()));
			}
			echo '</a></span>';
		} else {
			$content = $post->post_content;
			preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
			$n = count($strResult[1]);
			if($n > 0){
				echo '<span class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/long.png" data-original="'.$strResult[1][0].'!/both/'.zm_get_option('img_k_w').'x'.zm_get_option('img_k_h').'/format/webp/lossless/true" alt="'.$post->post_title .'" /></a></span>';
			} else { 
				echo '<span class="load"><a class="random-img" href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/long.png" data-original="'.get_template_directory_uri().'/img/random/long.jpg" alt="'.$post->post_title .'" /></a></span>';
			}
		}
	}
}

// 图片缩略图
function img_thumbnail() {
	$random_img = explode(',' , zm_get_option('random_image_url'));
	$random_img_array = array_rand($random_img);
	$src = $random_img[$random_img_array];
	global $post;
	if ( get_post_meta($post->ID, 'thumbnail', true) ) {
		$image = get_post_meta($post->ID, 'thumbnail', true);
		echo '<a href="'.get_permalink().'"><img src=';
		if (zm_get_option('manual_thumbnail')) {
			echo get_template_directory_uri().'/thumbnail.php?src='.$image.'&w='.zm_get_option('img_i_w').'&h='.zm_get_option('img_i_h').'&a='.zm_get_option('crop_top').'&zc=1';
		} else {
			echo $image;
		}
		echo ' alt="'.$post->post_title .'" /></a>';
	} else {
		if ( has_post_thumbnail() ) {
			echo '<a href="'.get_permalink().'">';
			if (zm_get_option('clipping_thumbnails')) {
				$full_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
				echo '<img src="'.get_template_directory_uri().'/thumbnail.php?src='.$full_image_url[0].'&w='.zm_get_option('img_i_w').'&h='.zm_get_option('img_i_h').'&a='.zm_get_option('crop_top').'&zc=1" alt="'.get_the_title().'">';
			} else {
				the_post_thumbnail('content', array('alt' => get_the_title()));
			}
			echo '</a>';
		} else {
			$content = $post->post_content;
			preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
			$n = count($strResult[1]);
			if($n > 0){
				echo '<a href="'.get_permalink().'"><img src="'.$strResult[1][0].'!/both/'.zm_get_option('img_i_w').'x'.zm_get_option('img_i_h').'/format/webp/lossless/true" alt="'.$post->post_title .'" /></a>';
			} else { 
				if ( zm_get_option('rand_img_n') ) {
					$random = mt_rand(1, zm_get_option('rand_img_n'));
				} else { 
					$random = mt_rand(1, 5);
				}
				if (zm_get_option('clipping_rand_img')) {
					echo '<span class="load"><a href="'.get_permalink().'"><img src="'.get_template_directory_uri().'/thumbnail.php?src='.$src.'&w='.zm_get_option('img_i_w').'&h='.zm_get_option('img_i_h').'&a='.zm_get_option('crop_top').'&zc=1" alt="'.$post->post_title .'" /></a></span>';
				} else { 
					echo '<a href="'.get_permalink().'"><img src="'.$src .'" alt="'.$post->post_title .'" /></a>';
				}
			}
		}
	}
}

function img_thumbnail_h() {
	$random_img = explode(',' , zm_get_option('random_image_url'));
	$random_img_array = array_rand($random_img);
	$src = $random_img[$random_img_array];
	global $post;
	if ( get_post_meta($post->ID, 'thumbnail', true) ) {
		$image = get_post_meta($post->ID, 'thumbnail', true);
		echo '<span class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original=';
		if (zm_get_option('manual_thumbnail')) {
			echo get_template_directory_uri().'/thumbnail.php?src='.$image.'&w='.zm_get_option('img_i_w').'&h='.zm_get_option('img_i_h').'&a='.zm_get_option('crop_top').'&zc=1';
		} else {
			echo $image;
		}
		echo ' alt="'.$post->post_title .'" /></a></span>';
	} else {
		if ( has_post_thumbnail() ) {
			echo '<span class="load"><a href="'.get_permalink().'">';
			if (zm_get_option('clipping_thumbnails')) {
				$full_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
				echo '<img src="' . get_template_directory_uri() . '/img/loading.png" data-original='.get_template_directory_uri().'/thumbnail.php?src='.$full_image_url[0].'&w='.zm_get_option('img_i_w').'&h='.zm_get_option('img_i_h').'&a='.zm_get_option('crop_top').'&zc=1 alt="'.get_the_title().'">';
			} else {
				the_post_thumbnail('content', array('alt' => get_the_title()));
			}
			echo '</a></span>';;
		} else {
			$content = $post->post_content;
			preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
			$n = count($strResult[1]);
			if($n > 0){
				echo '<span class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'.$strResult[1][0].'!/both/'.zm_get_option('img_i_w').'x'.zm_get_option('img_i_h').'/format/webp/lossless/true" alt="'.$post->post_title .'" /></a></span>';
			} else { 
				if ( zm_get_option('rand_img_n') ) {
					$random = mt_rand(1, zm_get_option('rand_img_n'));
				} else { 
					$random = mt_rand(1, 5);
				}
				if (zm_get_option('clipping_rand_img')) {
					echo '<a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'.get_template_directory_uri().'/thumbnail.php?src='.$src.'&w='.zm_get_option('img_i_w').'&h='.zm_get_option('img_i_h').'&a='.zm_get_option('crop_top').'&zc=1" alt="'.$post->post_title .'" /></a>';
				} else { 
					echo '<span class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'. $src .'" alt="'.$post->post_title .'" /></a></span>';
				}
			}
		}
	}
}

// 视频缩略图
function videos_thumbnail_h() {
	$random_img = explode(',' , zm_get_option('random_image_url'));
	$random_img_array = array_rand($random_img);
	$src = $random_img[$random_img_array];
	global $post;
	if ( get_post_meta($post->ID, 'small', true) ) {
		$image = get_post_meta($post->ID, 'small', true);
		echo '<span class="load"><a class="videos" href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original=';
		if (zm_get_option('manual_thumbnail')) {
			echo get_template_directory_uri().'/thumbnail.php?src='.$image.'&w='.zm_get_option('img_v_w').'&h='.zm_get_option('img_v_h').'&a='.zm_get_option('crop_top').'&zc=1';
		} else {
			echo $image;
		}
		echo ' alt="'.$post->post_title .'" /><i class="be be-play"></i></a></span>';
	} else {
		if ( has_post_thumbnail() ) {
			echo '<span class="load"><a class="videos" href="'.get_permalink().'">';
			if (zm_get_option('clipping_thumbnails')) {
				$full_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
				echo '<img src="' . get_template_directory_uri() . '/img/loading.png" data-original='.get_template_directory_uri().'/thumbnail.php?src='.$full_image_url[0].'&w='.zm_get_option('img_v_w').'&h='.zm_get_option('img_v_h').'&a='.zm_get_option('crop_top').'&zc=1 alt="'.get_the_title().'">';
			} else {
				the_post_thumbnail('content', array('alt' => get_the_title()));
			}
			echo '<i class="be be-play"></i></a></span>';

			} else {
				$content = $post->post_content;
				preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
				$n = count($strResult[1]);
				if($n > 0){
					echo '<span class="load"><a class="videos" href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'.$strResult[1][0].'!/both/'.zm_get_option('img_v_w').'x'.zm_get_option('img_v_h').'/format/webp/lossless/true" ';
					echo ' alt="'.$post->post_title .'" /><i class="be be-play"></i></a></span>';
					} else { 
						if ( zm_get_option('rand_img_n') ) {
							$random = mt_rand(1, zm_get_option('rand_img_n'));
						} else { 
							$random = mt_rand(1, 5);
						}
						if (zm_get_option('clipping_rand_img')) {
							echo '<span class="load"><a class="videos" href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'.get_template_directory_uri().'/thumbnail.php?src='.$src.'&w='.zm_get_option('img_v_w').'&h='.zm_get_option('img_v_h').'&a='.zm_get_option('crop_top').'&zc=1';
							echo ' alt="'.$post->post_title .'" /><i class="be be-play"></i></a></span>';
						} else { 
							echo '<span class="load"><a class="videos" href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'. $src .'" ';
							echo ' alt="'.$post->post_title .'" /><i class="be be-play"></i></a></span>';
						}
					}
			}
	}
}

function videos_thumbnail() {
	$random_img = explode(',' , zm_get_option('random_image_url'));
	$random_img_array = array_rand($random_img);
	$src = $random_img[$random_img_array];
	global $post;
	if ( get_post_meta($post->ID, 'small', true) ) {
		$image = get_post_meta($post->ID, 'small', true);
		echo '<a class="videos" href="'.get_permalink().'"><img src=';
		if (zm_get_option('manual_thumbnail')) {
			echo get_template_directory_uri().'/thumbnail.php?src='.$image.'&w='.zm_get_option('img_v_w').'&h='.zm_get_option('img_v_h').'&a='.zm_get_option('crop_top').'&zc=1';
		} else {
			echo $image;
		}
		echo ' alt="'.$post->post_title .'" /><i class="be be-play"></i></a>';
	} else {
		if ( has_post_thumbnail() ) {
			echo '<a class="videos" href="'.get_permalink().'">';
			if (zm_get_option('clipping_thumbnails')) {
				$full_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
				echo '<img src="'.get_template_directory_uri().'/thumbnail.php?src='.$full_image_url[0].'&w='.zm_get_option('img_v_w').'&h='.zm_get_option('img_v_h').'&a='.zm_get_option('crop_top').'&zc=1" alt="'.get_the_title().'">';
			} else {
				the_post_thumbnail('content', array('alt' => get_the_title()));
			}
	        echo '<i class="be be-play"></i></a>';
			} else {
				$content = $post->post_content;
				preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
				$n = count($strResult[1]);
				if($n > 0){
					echo '<a class="videos" href="'.get_permalink().'"><img src="'.$strResult[1][0].'!/both/'.zm_get_option('img_v_w').'x'.zm_get_option('img_v_h').'/format/webp/lossless/true" ';
					echo ' alt="'.$post->post_title .'" /><i class="be be-play"></i></a>';
					} else { 
						if ( zm_get_option('rand_img_n') ) {
							$random = mt_rand(1, zm_get_option('rand_img_n'));
						} else { 
							$random = mt_rand(1, 5);
						}

						if (zm_get_option('clipping_rand_img')) {
							echo '<a class="videos" href="'.get_permalink().'"><img src="'.get_template_directory_uri().'/thumbnail.php?src='.$src.'&w='.zm_get_option('img_v_w').'&h='.zm_get_option('img_v_h').'&a='.zm_get_option('crop_top').'&zc=1';
							echo ' alt="'.$post->post_title .'" /><i class="be be-play"></i></a>';
						} else { 
							echo '<a class="videos" href="'.get_permalink().'"><img src="'. $src .'" ';
							echo ' alt="'.$post->post_title .'" /><i class="be be-play"></i></a>';
						}
					}
			}
	}
}

function videoe_thumbnail() {
	$random_img = explode(',' , zm_get_option('random_image_url'));
	$random_img_array = array_rand($random_img);
	$src = $random_img[$random_img_array];
	global $post;
	if ( get_post_meta($post->ID, 'small', true) ) {
		$image = get_post_meta($post->ID, 'small', true);
		echo '<img src=';
		if (zm_get_option('manual_thumbnail')) {
			echo get_template_directory_uri().'/thumbnail.php?src='.$image.'&w='.zm_get_option('img_v_w').'&h='.zm_get_option('img_v_h').'&a='.zm_get_option('crop_top').'&zc=1';
		} else {
			echo $image;
		}
		echo ' alt="'.$post->post_title .'" />';
	} else {
		$content = $post->post_content;
		preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
		$n = count($strResult[1]);
		if($n > 0){
			echo '<img src="'.$strResult[1][0].'!/both/'.zm_get_option('img_v_w').'x'.zm_get_option('img_v_h').'/format/webp/lossless/true" ';
			echo ' alt="'.$post->post_title .'" />';
		} else { 
			if ( zm_get_option('rand_img_n') ) {
				$random = mt_rand(1, zm_get_option('rand_img_n'));
			} else { 
				$random = mt_rand(1, 5);
			}
			if (zm_get_option('clipping_rand_img')) {
				echo '<img src="'.get_template_directory_uri().'/thumbnail.php?src='.$src.'&w='.zm_get_option('img_v_w').'&h='.zm_get_option('img_v_h').'&a='.zm_get_option('crop_top').'&zc=1" ';
				echo ' alt="'.$post->post_title .'" />';
			} else { 
				echo '<img src="'. $src .'" ';
				echo ' alt="'.$post->post_title .'" />';
			}
		}
	}
}

function videor_thumbnail_h() {
	$random_img = explode(',' , zm_get_option('random_image_url'));
	$random_img_array = array_rand($random_img);
	$src = $random_img[$random_img_array];
	global $post;
	if ( get_post_meta($post->ID, 'small', true) ) {
		$image = get_post_meta($post->ID, 'small', true);
		echo '<span class="load"><a class="videor" href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original=';
		if (zm_get_option('manual_thumbnail')) {
			echo get_template_directory_uri().'/thumbnail.php?src='.$image.'&w='.zm_get_option('img_v_w').'&h='.zm_get_option('img_v_h').'&a='.zm_get_option('crop_top').'&zc=1';
		} else {
			echo $image;
		}
		echo ' alt="'.$post->post_title .'" /><i class="be be-play"></i></a></span>';
	} else {
		if ( has_post_thumbnail() ) {
			echo '<span class="load"><a class="videor" href="'.get_permalink().'">';
			if (zm_get_option('clipping_thumbnails')) {
				$full_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
				echo '<img src="' . get_template_directory_uri() . '/img/loading.png" data-original='.get_template_directory_uri().'/thumbnail.php?src='.$full_image_url[0].'&w='.zm_get_option('img_v_w').'&h='.zm_get_option('img_v_h').'&a='.zm_get_option('crop_top').'&zc=1 alt="'.get_the_title().'">';
			} else {
				the_post_thumbnail('content', array('alt' => get_the_title()));
			}
			echo '<i class="be be-play"></i></a></span>';
			} else {
				$content = $post->post_content;
				preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
				$n = count($strResult[1]);
				if($n > 0){
					echo '<span class="load"><a class="videor" href="'.get_permalink().'"><img src="'.$strResult[1][0].'!/both/'.zm_get_option('img_v_w').'x'.zm_get_option('img_v_h').'/format/webp/lossless/true" ';
					echo ' alt="'.$post->post_title .'" /><i class="be be-play"></i></a></span>';
					} else { 
						if ( zm_get_option('rand_img_n') ) {
							$random = mt_rand(1, zm_get_option('rand_img_n'));
						} else { 
							$random = mt_rand(1, 5);
						}
						if (zm_get_option('clipping_rand_img')) {
							echo '<span class="load"><a class="videor" href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'.get_template_directory_uri().'/thumbnail.php?src='.$src.'&w='.zm_get_option('img_v_w').'&h='.zm_get_option('img_v_h').'&a='.zm_get_option('crop_top').'&zc=1" ';
							echo ' alt="'.$post->post_title .'" /><i class="be be-play"></i></a></span>';
						} else { 
							echo '<span class="load"><a class="videor" href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'. $src .'" ';
							echo ' alt="'.$post->post_title .'" /><i class="be be-play"></i></a></span>';
						}
					}
			}
	}
}

function videor_thumbnail() {
	$random_img = explode(',' , zm_get_option('random_image_url'));
	$random_img_array = array_rand($random_img);
	$src = $random_img[$random_img_array];
	global $post;
	if ( get_post_meta($post->ID, 'small', true) ) {
		$image = get_post_meta($post->ID, 'small', true);
		echo '<a class="videor" href="'.get_permalink().'"><img src=';
		if (zm_get_option('manual_thumbnail')) {
			echo get_template_directory_uri().'/thumbnail.php?src='.$image.'&w='.zm_get_option('img_v_w').'&h='.zm_get_option('img_v_h').'&a='.zm_get_option('crop_top').'&zc=1';
		} else {
			echo $image;
		}
		echo ' alt="'.$post->post_title .'" /><i class="be be-play"></i></a>';
	} else {
		if ( has_post_thumbnail() ) {
			echo '<a class="videor" href="'.get_permalink().'">';
			if (zm_get_option('clipping_thumbnails')) {
				$full_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
				echo '<img src="'.get_template_directory_uri().'/thumbnail.php?src='.$full_image_url[0].'&w='.zm_get_option('img_v_w').'&h='.zm_get_option('img_v_h').'&a='.zm_get_option('crop_top').'&zc=1" alt="'.get_the_title().'">';
			} else {
				the_post_thumbnail('content', array('alt' => get_the_title()));
			}
			echo '<i class="be be-play"></i></a>';
			} else {
				$content = $post->post_content;
				preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
				$n = count($strResult[1]);
				if($n > 0){
					echo '<a class="videor" href="'.get_permalink().'"><img src="'.$strResult[1][0].'!/both/'.zm_get_option('img_v_w').'x'.zm_get_option('img_v_h').'/format/webp/lossless/true" ';
					echo ' alt="'.$post->post_title .'" /><i class="be be-play"></i></a>';
					} else { 
						if ( zm_get_option('rand_img_n') ) {
							$random = mt_rand(1, zm_get_option('rand_img_n'));
						} else { 
							$random = mt_rand(1, 5);
						}
						if (zm_get_option('clipping_rand_img')) {
							echo '<a class="videor" href="'.get_permalink().'"><img src="'.get_template_directory_uri().'/thumbnail.php?src='.$src.'&w='.zm_get_option('img_v_w').'&h='.zm_get_option('img_v_h').'&a='.zm_get_option('crop_top').'&zc=1" ';
							echo ' alt="'.$post->post_title .'" /><i class="be be-play"></i></a>';
						} else { 
							echo '<a class="videor" href="'.get_permalink().'"><img src="'. $src .'" ';
							echo ' alt="'.$post->post_title .'" /><i class="be be-play"></i></a>';
						}
					}
			}
	}
}

// 商品缩略图
function tao_thumbnail() {
		global $post;
		$url = get_post_meta($post->ID, 'taourl', true);
		if ( get_post_meta($post->ID, 'thumbnail', true) ) {
		$image = get_post_meta($post->ID, 'thumbnail', true);
		echo '<a href="'.esc_url( get_permalink() ).'"><img src=';
		if (zm_get_option('manual_thumbnail')) {
			echo get_template_directory_uri().'/thumbnail.php?src='.$image.'&w='.zm_get_option('img_t_w').'&h='.zm_get_option('img_t_h').'&a='.zm_get_option('crop_top').'&zc=1';
		} else {
			echo $image;
		}
		echo ' alt="'.$post->post_title .'" /></a>';
	} else {
		if ( has_post_thumbnail() ) {
			echo '<a href="'.get_permalink().'">';
			if (zm_get_option('clipping_thumbnails')) {
				$full_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
				echo '<img src="'.get_template_directory_uri().'/thumbnail.php?src='.$full_image_url[0].'&w='.zm_get_option('img_t_w').'&h='.zm_get_option('img_t_h').'&a='.zm_get_option('crop_top').'&zc=1" alt="'.get_the_title().'">';
			} else {
				the_post_thumbnail('tao', array('alt' => get_the_title()));
			}
			echo '</a>';
			} else {
				$content = $post->post_content;
				preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
				$n = count($strResult[1]);
				if($n > 0){
					echo '<a href="'.get_permalink().'"><img src="'.$strResult[1][0].'!/both/'.zm_get_option('img_t_w').'x'.zm_get_option('img_t_h').'/format/webp/lossless/true" alt="'.$post->post_title .'" /></a>';
				}
			}
	}
}

function tao_thumbnail_h() {
		global $post;
		$url = get_post_meta($post->ID, 'taourl', true);
		if ( get_post_meta($post->ID, 'thumbnail', true) ) {
		$image = get_post_meta($post->ID, 'thumbnail', true);
		echo '<span class="load"><a href="'.esc_url( get_permalink() ).'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original=';
		if (zm_get_option('manual_thumbnail')) {
			echo get_template_directory_uri().'/thumbnail.php?src='.$image.'&w='.zm_get_option('img_t_w').'&h='.zm_get_option('img_t_h').'&a='.zm_get_option('crop_top').'&zc=1';
		} else {
			echo $image;
		}
		echo ' alt="'.$post->post_title .'" /></a></span>';
	} else {
		if ( has_post_thumbnail() ) {
			echo '<span class="load"><a href="'.get_permalink().'">';
			if (zm_get_option('clipping_thumbnails')) {
				$full_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
				echo '<img src="' . get_template_directory_uri() . '/img/loading.png" data-original='.get_template_directory_uri().'/thumbnail.php?src='.$full_image_url[0].'&w='.zm_get_option('img_t_w').'&h='.zm_get_option('img_t_h').'&a='.zm_get_option('crop_top').'&zc=1 alt="'.get_the_title().'">';
			} else {
				the_post_thumbnail('tao', array('alt' => get_the_title()));
			}
			echo '</a></span>';
			} else {
				$content = $post->post_content;
				preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
				$n = count($strResult[1]);
				if($n > 0){
					echo '<span class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'.$strResult[1][0].'!/both/'.zm_get_option('img_t_w').'x'.zm_get_option('img_t_h').'/format/webp/lossless/true" alt="'.$post->post_title .'" /></a></span>';
				}
			}
	}
}

// 图像日志缩略图
function format_image_thumbnail() {
    global $post;
	$content = $post->post_content;
	preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
	echo '<div class="f4"><div class="format-img"><a href="'.get_permalink().'"><img src="'.$strResult[1][0].'!/both/'.zm_get_option('img_w').'x'.zm_get_option('img_h').'/format/webp/lossless/true" alt="'.$post->post_title .'" /></a></div></div>';
	echo '<div class="f4"><div class="format-img"><a href="'.get_permalink().'"><img src="'.$strResult[1][1].'!/both/'.zm_get_option('img_w').'x'.zm_get_option('img_h').'/format/webp/lossless/true" alt="'.$post->post_title .'" /></a></div></div>';
	echo '<div class="f4"><div class="format-img"><a href="'.get_permalink().'"><img src="'.$strResult[1][2].'!/both/'.zm_get_option('img_w').'x'.zm_get_option('img_h').'/format/webp/lossless/true" alt="'.$post->post_title .'" /></a></div></div>';
	echo '<div class="f4"><div class="format-img"><a href="'.get_permalink().'"><img src="'.$strResult[1][3].'!/both/'.zm_get_option('img_w').'x'.zm_get_option('img_h').'/format/webp/lossless/true" alt="'.$post->post_title .'" /></a></div></div>';
}

function format_image_thumbnail_h() {
    global $post;
	$content = $post->post_content;
	preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
	echo '<div class="f4"><div class="format-img"><div class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'.$strResult[1][0].'!/both/'.zm_get_option('img_w').'x'.zm_get_option('img_h').'/format/webp/lossless/true" alt="'.$post->post_title .'" /></a></div></div></div>';
	echo '<div class="f4"><div class="format-img"><div class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'.$strResult[1][1].'!/both/'.zm_get_option('img_w').'x'.zm_get_option('img_h').'/format/webp/lossless/true" alt="'.$post->post_title .'" /></a></div></div></div>';
	echo '<div class="f4"><div class="format-img"><div class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'.$strResult[1][2].'!/both/'.zm_get_option('img_w').'x'.zm_get_option('img_h').'/format/webp/lossless/true" alt="'.$post->post_title .'" /></a></div></div></div>';
	echo '<div class="f4"><div class="format-img"><div class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'.$strResult[1][3].'!/both/'.zm_get_option('img_w').'x'.zm_get_option('img_h').'/format/webp/lossless/true" alt="'.$post->post_title .'" /></a></div></div></div>';
}

function zm_ad_thumbnail() {
	$random_img = explode(',' , zm_get_option('random_image_url'));
	$random_img_array = array_rand($random_img);
	$src = $random_img[$random_img_array];
	global $post;
	$direct = get_post_meta($post->ID, 'direct', true);
	if ( get_post_meta($post->ID, 'thumbnail', true) ) {
		$image = get_post_meta($post->ID, 'thumbnail', true);
		echo '<a href="'.get_permalink().'"><img src=';
		if (zm_get_option('manual_thumbnail')) {
			echo get_template_directory_uri().'/thumbnail.php?src='.$image.'&w='.zm_get_option('img_w').'&h='.zm_get_option('img_h').'&a='.zm_get_option('crop_top').'&zc=1';
		} else {
			echo $image;
		}
		echo ' alt="'.$post->post_title .'" /></a>';
	} else {
		if ( has_post_thumbnail() ) {
			echo '<a href="'.$direct.'" target="_blank" rel="nofollow">';
			if (zm_get_option('clipping_thumbnails')) {
				$full_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
				echo '<img src="'.get_template_directory_uri().'/thumbnail.php?src='.$full_image_url[0].'&w='.zm_get_option('img_w').'&h='.zm_get_option('img_h').'&a='.zm_get_option('crop_top').'&zc=1" alt="'.get_the_title().'">';
			} else {
				the_post_thumbnail('content', array('alt' => get_the_title()));
			}
			echo '</a>';
		} else {
			$content = $post->post_content;
			preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
			$n = count($strResult[1]);
			if($n > 0){
				echo '<a href="'.$direct.'" target="_blank" rel="nofollow"><img src="'.$strResult[1][0].'!/both/'.zm_get_option('img_w').'x'.zm_get_option('img_h').'/format/webp/lossless/true" alt="'.$post->post_title .'" /></a>';
			} else { 
				if ( zm_get_option('rand_img_n') ) {
					$random = mt_rand(1, zm_get_option('rand_img_n'));
				} else { 
					$random = mt_rand(1, 5);
				}
				if (zm_get_option('clipping_rand_img')) {
					echo '<a href="'.$direct.'" target="_blank" rel="nofollow"><img src="'.get_template_directory_uri().'/thumbnail.php?src='.$src.'&w='.zm_get_option('img_w').'&h='.zm_get_option('img_h').'&a='.zm_get_option('crop_top').'&zc=1" alt="'.$post->post_title .'" /></a>';
				} else { 
					echo '<a href="'.$direct.'" target="_blank" rel="nofollow"><img src="'.$src .'" alt="'.$post->post_title .'" /></a>';
				}
			}
		}
	}
}

function zm_ad_thumbnail_h() {
	$random_img = explode(',' , zm_get_option('random_image_url'));
	$random_img_array = array_rand($random_img);
	$src = $random_img[$random_img_array];
	global $post;
	$direct = get_post_meta($post->ID, 'direct', true);
	if ( get_post_meta($post->ID, 'thumbnail', true) ) {
		$image = get_post_meta($post->ID, 'thumbnail', true);
		echo '<span class="load"><a href="'.$direct.'" target="_blank" rel="nofollow"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original=';
		if (zm_get_option('manual_thumbnail')) {
			echo get_template_directory_uri().'/thumbnail.php?src='.$image.'&w='.zm_get_option('img_w').'&h='.zm_get_option('img_h').'&a='.zm_get_option('crop_top').'&zc=1';
		} else {
			echo $image;
		}
		echo ' alt="'.$post->post_title .'" /></a></span>';
	} else {
		if ( has_post_thumbnail() ) {
			echo '<span class="load"><a href="'.$direct.'">';
			if (zm_get_option('clipping_thumbnails')) {
				$full_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
				echo '<img src="' . get_template_directory_uri() . '/img/loading.png" data-original='.get_template_directory_uri().'/thumbnail.php?src='.$full_image_url[0].'&w='.zm_get_option('img_w').'&h='.zm_get_option('img_h').'&a='.zm_get_option('crop_top').'&zc=1 alt="'.get_the_title().'">';
			} else {
				the_post_thumbnail('content', array('alt' => get_the_title()));
			}
			echo '</a></span>';
		} else {
			$content = $post->post_content;
			preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
			$n = count($strResult[1]);
			if($n > 0){
				echo '<span class="load"><a href="'.$direct.'" target="_blank" rel="nofollow"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'.$strResult[1][0].'!/both/'.zm_get_option('img_w').'x'.zm_get_option('img_h').'/format/webp/lossless/true" alt="'.$post->post_title .'" /></a></span>';
			} else { 
				if ( zm_get_option('rand_img_n') ) {
					$random = mt_rand(1, zm_get_option('rand_img_n'));
				} else { 
					$random = mt_rand(1, 5);
				}
				if (zm_get_option('clipping_rand_img')) {
					echo '<span class="load"><a href="'.$direct.'" target="_blank" rel="nofollow"><img src="'.get_template_directory_uri().'/thumbnail.php?src='.$src.'&w='.zm_get_option('img_w').'&h='.zm_get_option('img_h').'&a='.zm_get_option('crop_top').'&zc=1" alt="'.$post->post_title .'" /></a></span>';
				} else { 
					echo '<span class="load"><a href="'.$direct.'" target="_blank" rel="nofollow"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'. $src .'" alt="'.$post->post_title .'" /></a></span>';
				}
	        }
		}
	}
}

// 图片布局缩略图
function zm_grid_thumbnail() {
	$random_img = explode(',' , zm_get_option('random_image_url'));
	$random_img_array = array_rand($random_img);
	$src = $random_img[$random_img_array];
	global $post;
	if ( get_post_meta($post->ID, 'thumbnail', true) ) {
		$image = get_post_meta($post->ID, 'thumbnail', true);
		echo '<a href="'.get_permalink().'"><img src=';
		if (zm_get_option('manual_thumbnail')) {
			echo get_template_directory_uri().'/thumbnail.php?src='.$image.'&w='.zm_get_option('grid_w').'&h='.zm_get_option('grid_h').'&a='.zm_get_option('crop_top').'&zc=1';
		} else {
			echo $image;
		}
		echo ' alt="'.$post->post_title .'" /></a>';
	} else {
		if ( has_post_thumbnail() ) {
			echo '<a href="'.get_permalink().'">';
			if (zm_get_option('clipping_thumbnails')) {
				$full_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
				echo '<img src="'.get_template_directory_uri().'/thumbnail.php?src='.$full_image_url[0].'&w='.zm_get_option('grid_w').'&h='.zm_get_option('grid_h').'&a='.zm_get_option('crop_top').'&zc=1" alt="'.get_the_title().'">';
			} else {
				the_post_thumbnail('content', array('alt' => get_the_title()));
			}
			echo '</a>';
		} else {
			$content = $post->post_content;
			preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
			$n = count($strResult[1]);
			if($n > 0){
				echo '<a href="'.get_permalink().'"><img src="'.$strResult[1][0].'!/both/'.zm_get_option('grid_w').'x'.zm_get_option('grid_h').'/format/webp/lossless/true" alt="'.$post->post_title .'" /></a>';
			} else { 
				if ( zm_get_option('rand_img_n') ) {
					$random = mt_rand(1, zm_get_option('rand_img_n'));
				} else { 
					$random = mt_rand(1, 5);
				}
				if (zm_get_option('clipping_rand_img')) {
					echo '<a href="'.get_permalink().'"><img src="'.get_template_directory_uri().'/thumbnail.php?src='.$src.'&w='.zm_get_option('grid_w').'&h='.zm_get_option('grid_h').'&a='.zm_get_option('crop_top').'&zc=1" alt="'.$post->post_title .'" /></a>';
				} else { 
					echo '<a href="'.get_permalink().'"><img src="'. $src .'" alt="'.$post->post_title .'" /></a>';
				}
			}
		}
	}
}

function zm_grid_thumbnail_h() {
	$random_img = explode(',' , zm_get_option('random_image_url'));
	$random_img_array = array_rand($random_img);
	$src = $random_img[$random_img_array];
	global $post;
	if ( get_post_meta($post->ID, 'thumbnail', true) ) {
		$image = get_post_meta($post->ID, 'thumbnail', true);
		echo '<span class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original=';
		if (zm_get_option('manual_thumbnail')) {
			echo get_template_directory_uri().'/thumbnail.php?src='.$image.'&w='.zm_get_option('grid_w').'&h='.zm_get_option('grid_h').'&a='.zm_get_option('crop_top').'&zc=1';
		} else {
			echo $image;
		}
		echo ' alt="'.$post->post_title .'" /></a></span>';
	} else {
		if ( has_post_thumbnail() ) {
			echo '<span class="load"><a href="'.get_permalink().'">';
			if (zm_get_option('clipping_thumbnails')) {
				$full_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
				echo '<img src="' . get_template_directory_uri() . '/img/loading.png" data-original='.get_template_directory_uri().'/thumbnail.php?src='.$full_image_url[0].'&w='.zm_get_option('grid_w').'&h='.zm_get_option('grid_h').'&a='.zm_get_option('crop_top').'&zc=1 alt="'.get_the_title().'">';
			} else {
				the_post_thumbnail('content', array('alt' => get_the_title()));
			}
			echo '</a></span>';
		} else {
			$content = $post->post_content;
			preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
			$n = count($strResult[1]);
			if($n > 0){
				echo '<span class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'.$strResult[1][0].'!/both/'.zm_get_option('grid_w').'x'.zm_get_option('grid_h').'/format/webp/lossless/true" alt="'.$post->post_title .'" /></a></span>';
			} else { 
				if ( zm_get_option('rand_img_n') ) {
					$random = mt_rand(1, zm_get_option('rand_img_n'));
				} else { 
					$random = mt_rand(1, 5);
				}
				if (zm_get_option('clipping_rand_img')) {
					echo '<span class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'.get_template_directory_uri().'/thumbnail.php?src='.$src.'&w='.zm_get_option('grid_w').'&h='.zm_get_option('grid_h').'&a='.zm_get_option('crop_top').'&zc=1" alt="'.$post->post_title .'" /></a></span>';
				} else { 
					echo '<span class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'.$src .'" alt="'.$post->post_title .'" /></a></span>';
				}
			}
		}
	}
}

// 宽缩略图分类
function zm_full_thumbnail() {
	global $post;
	if ( get_post_meta($post->ID, 'full_thumbnail', true) ) {
		$image = get_post_meta($post->ID, 'full_thumbnail', true);
		echo '<header class="full-header"><figure class="full-thumbnail"><span class="load"><a href="'.get_permalink().'"><img src=';
		echo get_template_directory_uri().'/thumbnail.php?src='.$image.'&w=900&h=350&a=t&zc=1';
		echo ' alt="'.$post->post_title .'" /></a></span></figure>';
		echo ''.the_title( sprintf( '<h2 class="entry-title-img"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' ).'</header>';
	} else {
		$content = $post->post_content;
		preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
		$n = count($strResult[1]);
		if($n > 0){
			echo '<header class="full-header"><figure class="full-thumbnail"><span class="load"><a href="'.get_permalink().'"><img src="'.$strResult[1][0].'!/both/900x350/format/webp/lossless/true" alt="'.$post->post_title .'" /></a></span></figure>';
			echo ''.the_title( sprintf( '<h2 class="entry-title-img"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' ).'</header>';
		} else {
			the_title( sprintf( '<h2 class="entry-title-full"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' );
			echo '<span class="title-l"></span>';
		}
	}
}

function zm_full_thumbnail_h() {
	global $post;
	if ( get_post_meta($post->ID, 'full_thumbnail', true) ) {
		$image = get_post_meta($post->ID, 'full_thumbnail', true);
		echo '<header class="full-header"><figure class="full-thumbnail"><span class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original=';
		echo get_template_directory_uri().'/thumbnail.php?src='.$image.'&w=900&h=350&a=t&zc=1';
		echo ' alt="'.$post->post_title .'" /></span></figure>';
		echo ''.the_title( sprintf( '<h2 class="entry-title-img"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' ).'</header>';
	} else {
		$content = $post->post_content;
		preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
		$n = count($strResult[1]);
		if($n > 0){
			echo '<header class="full-header"><figure class="full-thumbnail"><span class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'.$strResult[1][0].'!/both/900x350/format/webp/lossless/true" alt="'.$post->post_title .'" /></a></span></figure>';
			echo ''.the_title( sprintf( '<h2 class="entry-title-img"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' ).'</header>';
		} else {
			the_title( sprintf( '<h2 class="entry-title-full"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' );
			echo '<span class="title-l"></span>';
		}
	}
}

// 网址缩略图
function zm_sites_thumbnail() {
	global $post;
	$sites_img_link = sites_nofollow( base64_encode(get_post_meta($post->ID, 'sites_img_link', true)));
	if ( get_post_meta($post->ID, 'thumbnail', true) ) {
		$image = get_post_meta($post->ID, 'thumbnail', true);
		echo '<a href="'.$sites_img_link.'" target="_blank" rel="external nofollow"><img src=';
		echo $image;
		echo ' alt="'.$post->post_title .'" /></a>';
	} else {
		$content = $post->post_content;
		preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
		$n = count($strResult[1]);
		if($n > 0){
			echo '<a href="'.$sites_img_link.'" target="_blank" rel="external nofollow"><img src="'.$strResult[1][0].'!/both/'.zm_get_option('img_w').'x'.zm_get_option('img_h').'/format/webp/lossless/true" alt="'.$post->post_title .'" /></a>';
		}
	}
}

function zm_sites_thumbnail_h() {
	global $post;
	$sites_img_link = sites_nofollow( base64_encode(get_post_meta($post->ID, 'sites_img_link', true)));
	if ( get_post_meta($post->ID, 'thumbnail', true) ) {
		$image = get_post_meta($post->ID, 'thumbnail', true);
		echo '<a href="'.$sites_img_link.'" target="_blank" rel="external nofollow"><img src=';
		echo $image;
		echo ' alt="'.$post->post_title .'" /></a>';
	} else {
		$content = $post->post_content;
		preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
		$n = count($strResult[1]);
		if($n > 0){
			echo '<div class="load"><a href="'.$sites_img_link.'" target="_blank" rel="external nofollow"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'.$strResult[1][0].'!/both/'.zm_get_option('img_w').'x'.zm_get_option('img_h').'/format/webp/lossless/true" alt="'.$post->post_title .'" /></a></div>';
		}
	}
}

// wd_img
function gr_wd_thumbnail() {
	global $post;
	if ( get_post_meta($post->ID, 'wd_img', true) ) {
		$image = get_post_meta($post->ID, 'wd_img', true);
		echo '<a href="'.get_permalink().'"><img src=';
		echo $image;
		echo ' alt="'.$post->post_title .'" /></a>';
	} else {
		$content = $post->post_content;
		preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
		echo '<a href="'.get_permalink().'"><img src="'.$strResult[1][0].'!/both/700x380/format/webp/lossless/true" alt="'.$post->post_title .'" /></a>';
	}
}

function gr_wd_thumbnail_h() {
	global $post;
	if ( get_post_meta($post->ID, 'wd_img', true) ) {
		$image = get_post_meta($post->ID, 'wd_img', true);
		echo '<span class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original=';
		echo $image;
		echo ' alt="'.$post->post_title .'" /></a>';
	} else {
		$content = $post->post_content;
		preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
		echo '<span class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'.$strResult[1][0].'!/both/700x300/format/webp/lossless/true" alt="'.$post->post_title .'" /></a></span>';
	}
}

// slider_widge
function zm_widge_thumbnail() {
	global $post;
	if ( get_post_meta($post->ID, 'widge_img', true) ) {
		$image = get_post_meta($post->ID, 'widge_img', true);
		echo '<a href="'.get_permalink().'"><img src=';
		echo get_template_directory_uri().'/thumbnail.php?src='.$image.'&w='.zm_get_option('img_s_w').'&h='.zm_get_option('img_s_h').'&a='.zm_get_option('crop_top').'&zc=1';
		echo ' alt="'.$post->post_title .'" /></a>';
	} else {
		$content = $post->post_content;
		preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
		$n = count($strResult[1]);
		if($n > 0){
			echo '<a href="'.get_permalink().'"><img src="'.$strResult[1][0].'!/both/'.zm_get_option('img_s_w').'x'.zm_get_option('img_s_h').'/format/webp/lossless/true" alt="'.$post->post_title .'" /></a>';
		}
	}
}

// slider_scrolling
function zm_thumbnail_scrolling() {
	$random_img = explode(',' , zm_get_option('random_image_url'));
	$random_img_array = array_rand($random_img);
	$src = $random_img[$random_img_array];
	global $post;
	if ( get_post_meta($post->ID, 'thumbnail', true) ) {
		$image = get_post_meta($post->ID, 'thumbnail', true);
		echo '<a href="'.get_permalink().'"><img class="owl-lazy" data-src=';
		if (zm_get_option('manual_thumbnail')) {
			echo get_template_directory_uri().'/thumbnail.php?src='.$image.'&w='.zm_get_option('img_w').'&h='.zm_get_option('img_h').'&a='.zm_get_option('crop_top').'&zc=1';
		} else {
			echo $image;
		}
		echo ' alt="'.$post->post_title .'" /></a>';
	} else {
		if ( has_post_thumbnail() ) {
			echo '<a href="'.get_permalink().'">';
			if (zm_get_option('clipping_thumbnails')) {
				$full_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
				echo '<img src="'.get_template_directory_uri().'/thumbnail.php?src='.$full_image_url[0].'&w='.zm_get_option('img_w').'&h='.zm_get_option('img_h').'&a='.zm_get_option('crop_top').'&zc=1" alt="'.get_the_title().'">';
			} else {
				the_post_thumbnail('content', array('alt' => get_the_title()));
			}
			echo '</a>';
		} else {
			$content = $post->post_content;
			preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
			$n = count($strResult[1]);
			if($n > 0){
				echo '<a href="'.get_permalink().'"><img class="owl-lazy" data-src="'.$strResult[1][0].'!/both/'.zm_get_option('img_w').'x'.zm_get_option('img_h').'/format/webp/lossless/true" alt="'.$post->post_title .'" /></a>';
			} else { 
				if ( zm_get_option('rand_img_n') ) {
					$random = mt_rand(1, zm_get_option('rand_img_n'));
				} else { 
					$random = mt_rand(1, 5);
				}
				if (zm_get_option('clipping_rand_img')) {
					echo '<a href="'.get_permalink().'"><img class="owl-lazy" data-src="'.get_template_directory_uri().'/thumbnail.php?src='.$src.'&w='.zm_get_option('img_w').'&h='.zm_get_option('img_h').'&a='.zm_get_option('crop_top').'&zc=1" alt="'.$post->post_title .'" /></a>';
				} else { 
					echo '<a href="'.get_permalink().'"><img class="owl-lazy" data-src="'. $src .'" alt="'.$post->post_title .'" /></a>';
				}
			}
		}
	}
}

// waterfall
function zm_waterfall_img() {
	$random_img = explode(',' , zm_get_option('random_image_url'));
	$random_img_array = array_rand($random_img);
	$src = $random_img[$random_img_array];
	global $post;
	if ( get_post_meta($post->ID, 'fall_img', true) ) {
		$image = get_post_meta($post->ID, 'fall_img', true);
		echo '<a href="'.get_permalink().'"><img src=';
		echo $image;
		echo ' alt="'.$post->post_title .'" /></a>';
	} else {
		$content = $post->post_content;
		preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
		$n = count($strResult[1]);
		if($n > 0){
			echo '<a href="'.get_permalink().'"><img src="'.$strResult[1][0].'" alt="'.$post->post_title .'" /></a>';
		} else { 
			if ( zm_get_option('rand_img_n') ) {
				$random = mt_rand(1, zm_get_option('rand_img_n'));
			} else { 
				$random = mt_rand(1, 5);
			}
			if (zm_get_option('clipping_rand_img')) {
				echo '<a href="'.get_permalink().'"><img src="'.get_template_directory_uri().'/thumbnail.php?src='.$src.'&w='.zm_get_option('img_w').'&h='.zm_get_option('img_h').'&a='.zm_get_option('crop_top').'&zc=1" alt="'.$post->post_title .'" /></a>';
			} else { 
				echo '<a href="'.get_permalink().'"><img src="'.$src .'" alt="'.$post->post_title .'" /></a>';
			}
		}
	}
}

function zm_waterfall_img_h() {
	$random_img = explode(',' , zm_get_option('random_image_url'));
	$random_img_array = array_rand($random_img);
	$src = $random_img[$random_img_array];
	global $post;
	if ( get_post_meta($post->ID, 'fall_img', true) ) {
		$image = get_post_meta($post->ID, 'fall_img', true);
		echo '<span class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original=';
		echo $image;
		echo ' alt="'.$post->post_title .'" /></a></span>';
	} else {
		$content = $post->post_content;
		preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
		$n = count($strResult[1]);
		if($n > 0){
			echo '<span class="load"><a href="'.get_permalink().'"><img src="'.$strResult[1][0].'" alt="'.$post->post_title .'" /></a></span>';
		} else { 
			if ( zm_get_option('rand_img_n') ) {
				$random = mt_rand(1, zm_get_option('rand_img_n'));
			} else { 
				$random = mt_rand(1, 5);
			}
			if (zm_get_option('clipping_rand_img')) {
				echo '<span class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'.get_template_directory_uri().'/thumbnail.php?src='.$src.'&w='.zm_get_option('img_w').'&h='.zm_get_option('img_h').'&a='.zm_get_option('crop_top').'&zc=1" alt="'.$post->post_title .'" /></a></span>';
			} else { 
				echo '<span class="load"><a href="'.get_permalink().'"><img src="' . get_template_directory_uri() . '/img/loading.png" data-original="'.$src .'" alt="'.$post->post_title .'" /></a></span>';
			}
		}
	}
}

重定向资源链接

这里我们需要用到CDN启动器插件CDN Enabler,CDN URL为CDN的资源链接,请自行在又拍云CDN控制台获取。

Begin主题完美适配又拍云缩略图,兼容输出webp格式图片 加速网站访问

如果你使用了其他插件或者方法将图片资源定向到CDN,可以不使用CDN Enabler插件。

Begin后台启用又拍云缩略图

按照顺序打开WordPress后台-外观-主题选项-基本设置-缩略图方式,选择阿里云OSS保存即可。

Begin主题完美适配又拍云缩略图,兼容输出webp格式图片 加速网站访问

总结

缩略图外置的作用不言而喻,可以进一步降低服务器的性能损耗,webp图片格式可以达到节省CDN流量,节约成本的效果。这篇文章极大简化了操作流程,为了进一步完善Begin主题的内置功能,自然已经将文件thumbnail-upyun.php推送给明哥了,下一个版本的begin主题中将会内置选项。

如果使用中有什么问题,欢迎下方留言,我们一同学习。

weinxin
网科文媒
快来关注一下,你想要的这里都有喔!
自然

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: