WordPress 后台图片使用七牛云存储CDN

使用的 PHP 闭包函数来处理,所以要新点的版本的 PHP 才行,具体哪个版本支持 PHP 闭包函数,自行查阅一下。

// 因为使用七牛来缩图,WordPress 默认的缩图就没有必要了。
add_filter('pre_option_thumbnail_size_w',	'__return_zero' );
add_filter('pre_option_thumbnail_size_h',	'__return_zero' );
add_filter('pre_option_medium_size_w',		'__return_zero' );
add_filter('pre_option_medium_size_h',		'__return_zero' );
add_filter('pre_option_large_size_w',		'__return_zero' );
add_filter('pre_option_large_size_h',		'__return_zero' );

// 因为使用七牛来缩图,也不用生成各种尺寸的数组
add_filter('intermediate_image_sizes_advanced', function($sizes){
	if(isset($sizes['full'])){
		return array('full'=>$sizes['full']);
	}else{
		return array();
	}
});

// 因为使用七牛来缩图,后台图片选择只剩下原图
add_filter('image_size_names_choose', function($sizes){
	if(isset($sizes['full'])){
		return array('full'=>$sizes['full']);
	}else{
		return array();
	}
});

add_filter('upload_dir', function($uploads){
	$uploads['url']		= wpjam_get_thumbnail($uploads['url']);
	$uploads['baseurl']	= wpjam_get_thumbnail($uploads['baseurl']);

	return $uploads;
});

add_filter('wp_calculate_image_srcset_meta', '__return_empty_array');

// 因为使用七牛来缩图,根据各种尺寸,使用七牛的缩图API进行缩图
add_filter('wp_get_attachment_image_src', function($image, $attachment_id, $size, $icon){
	return  wpjam_get_attachment_image_src($attachment_id, $size);
}, 10 ,4);


function wpjam_get_attachment_image_src($attachment_id, $size='full'){

	$img_url 	= wp_get_attachment_url($attachment_id);

	if(empty($img_url)){
		return array('', 0, 0, false);
	}

	$image_meta = wp_get_attachment_metadata( $attachment_id );

	$crop	= 0;

	if($size == 'thumbnail'){
		$crop	= 1;
		$width	= $height = 150;
	}elseif($size == 'medium'){
		$width	= $height = 300;
	}elseif($size == 'medium_large'){
		$width	= 768;
		$height = 0;
	}elseif($size == 'large'){
		$width	= $height = 1024;
	}elseif(is_array($size)){
		$width	= $size[0];
		$height = $size[1];
	}

	if(isset($width) && isset($height)){
		$mode		= $crop?'1':'2';
		$img_url	= wpjam_get_thumbnail($img_url, compact('width', 'height', 'mode'));
		$dims		= image_resize_dimensions($image_meta['width'], $image_meta['height'], $width, $height, $crop);
		
		return array( $img_url, $dims[4], $dims[5],false);
	}else{
		$img_url	= wpjam_get_thumbnail($img_url);

		$image_meta_width	= ($image_meta['width'])??0;
		$image_meta_height	= ($image_meta['height'])??0;

		return array($img_url, $image_meta_width, $image_meta_height, false);
	}
}

// 媒体列表页面,也是使用七牛的缩图API进行缩图
add_filter('wp_prepare_attachment_for_js', function($response, $attachment, $meta){

	if(isset($response['sizes'])){
		$orientation	= $response['sizes']['full']['orientation'];

		foreach (array('thumbnail', 'medium', 'medium_large', 'large') as $s) {
			$image_src = wpjam_get_attachment_image_src($attachment->ID, $s);

			$response['sizes'][$s]	= array(
				'url'			=> $image_src[0],
				'width'			=> $image_src[1],
				'height'		=> $image_src[2],
				'orientation'	=> $orientation
			);
		}
	}

	return $response;
}, 10, 3);

想了解更多关于WordPress 后台图片使用七牛云存储CDN的内容,请扫微信
或微信搜索jiemingpan

本文链接:http://www.soufuzi.com/jianzhan/3072

(0)
上一篇 2025-04-21 15:48:16
下一篇 2025-04-21 15:48:16

相关推荐

  • 什么是老字号企业?

    老字号企业是指历史悠久,拥有世代传承的产品、技艺或服务,具有鲜明的中华民族传统文化背景和深厚的文化底蕴,取得社会广泛认同,形成良好信誉品牌的企业。

    2023-03-16 15:18:55
  • 国际物流DDP贸易术语包含了哪些费用?

    DDP(Delivered Duty Paid)是一种国际贸易术语,表示卖方承担货物运输和报关等费用,将货物交付到买方指定地点并完成清关手续。美国海运DDP的计算价格涉及以下几个因素: 1.货物的重量和体积:通常情况下,海运费用是根据货物的重量和体积来计算的。因此,需要计算货物的实际重量和体积,并根据不同的船运公司和运输方式来确定运费。 2.起运港和目的港:不同的起运港和目的港之间的运费也会有所不同。因此,需要确定货物的起运港

    2024-09-03 01:30:27