按需要生成图片缩略图(第二次更新)

编程必须要依据其手册进行,研究WordPress也必然要学习它的技术文档。虽然Google可以解决很多的方法,但Google出来的方法,其作者可能并没有依据技术文档进行改写,仍然在语法,或者函数的使用上面更多的只是凭借其作者的经验进行。

接上次的文章按需要生成图片缩略图(更新),在细细查询,并理解了WordPress的技术文档后,其实很多的语法和函数,都可以直接调用WordPress本身已经存在的函数,这样可以最大可能遵循原来WordPress编写的逻辑,这样最大的好处是:

  • 在后期WordPress新版本更新,保证了兼容性,降低出错的可能
  • 直接调用本身的函数,数据执行效率提高
  • 保持数据输入输出与原逻辑一致
  •  减少使用外部函数,减少安全风险
  • 后期更容易维护和迭代

在使用WordPress中,按实际需要来生成想要的图片缩略图,下面结合上面的逻辑,第二次更新了代码,发现执行效率更高,也保持了与原数据的同步。

function cq_the_post_thumbnail( $size ) {	
	// Declaration
	global $_wp_additional_image_sizes;		
	$width = $height = 0;
	$crop = false;
		
	if ( in_array( $size, array( 'thumbnail', 'medium', 'medium_large', 'large' ) ) ) {
		$width  = get_option( $size . '_size_w' );
		$height = get_option( $size . '_size_h' );
		$crop   = (bool) get_option( $size . '_crop' );

	} elseif ( isset( $_wp_additional_image_sizes ) && isset( $_wp_additional_image_sizes[ $size ] ) ) {
		$width = (int)$_wp_additional_image_sizes[ $size ]['width'];
		$height = (int)$_wp_additional_image_sizes[ $size ]['height'];
		$crop = isset( $_wp_additional_image_sizes[ $size ]['crop'] ) ? $_wp_additional_image_sizes[ $size ]['crop'] : false;

	} else { 
		return false;
	}	

	if ( 0 === absint( $width ) && 0 === absint( $height ) ) {
		return false;
	}
		
	$first_img_url = cq_get_first_image(); // get the first image from post content
	
	if ( $first_img_url ) {
		$attach_id   = attachment_url_to_postid( $first_img_url );		
		$meta = wp_get_attachment_metadata( $attach_id );
		
	} elseif ( get_the_ID() ) {
		$attach_id = get_post_thumbnail_id( get_the_ID() );
		$meta = wp_get_attachment_metadata( $attach_id );
		
	} else {
		return false;
	}

	if ( is_array( $meta ) && isset( $meta['sizes'][$size] ) ) {		
		$intermediate = image_get_intermediate_size( $attach_id, $size );		
		$img_url = wp_get_attachment_url( $attach_id );
		$img_url_basename = wp_basename( $img_url );
		$img_url = str_replace( $img_url_basename, $intermediate['file'], $img_url );
		$result_width = $intermediate['width'];
		$result_height = $intermediate['height'];

		$cq_image = array (
			'url' => $img_url,
			'width' => $result_width,
			'height' => $result_height,
			'mime-type' => get_post_mime_type( $attach_id ),
		);		
		return $cq_image;	
		
	} else {		
		$attachment_path = get_attached_file( $attach_id );
		$image_editor = wp_get_image_editor( $attachment_path );
		
		if ( ! is_wp_error( $image_editor ) ) {	
			$image_editor->set_quality(100);
			$image_editor->resize( $width, $height, $crop );			
			$new_img = $image_editor->get_size();
			$suffix = $image_editor->get_suffix();
			$new_width = $new_img['width'];
			$new_height = $new_img['height'];
			
			$filename = $image_editor->generate_filename( $suffix );			
			$image_editor->save( $filename );
			$new_filename = wp_basename( $filename );

			$meta['sizes'][ $size ] = array(
				'file'      => $new_filename,
				'width'     => $new_width,
				'height'    => $new_height,
				'mime-type' => get_post_mime_type( $attach_id ),
			);
			wp_update_attachment_metadata( $attach_id, $meta );
			
			$img_url = wp_get_attachment_url( $attach_id );
			$img_url_basename = wp_basename( $img_url );
			$size_img_url = str_replace( $img_url_basename, $new_filename, $img_url );

			$cq_image = array (
				'url' => $size_img_url,
				'width' => $new_img['width'],
				'height' => $new_img['height'],
				'mime-type' => get_post_mime_type( $attach_id ),
			);	
			return $cq_image;
			
		} else {
			return false;
		}	
	} 
}

使用方法没有改变,仍然使用如下代码

 * Example use:
 * 
 * $image = cq_the_post_thumbnail( 'image_size' );
 * <img src="<?php echo $image['url']; ?>" width="<?php echo $image['width']; ?>" height="<?php echo $image['height']; ?>" alt="" />
 *
 * image_size = 'thumbnail', 'medium', 'medium_large', 'large', or added by add_image_size()

完毕

Previous/Next

Say Something!

Leave a Reply