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

相关推荐

  • 法国和德国EPR有什么不同?

    1.分类不同 德国EPR注册涉及三类产品:电子电器、包装、电池;法国涉及七类:电子电器、包装、电池、纸张、纺织品、家具和轮胎。除了电子电器、包装、电池是相同的外,法国又增加了纸张、纺织品、家具和轮胎。 2.申报方式不同 销售前产品必须要有包装,所以包装法是亚马逊每个卖家必须要申请的EPR分类。在德国,包装法的申请是先申请Verpack号码,然后再和回收公司签合同;而法国正好相反,是先和回收公司签合同,然后再由回收公

    2024-04-12 22:42:19
  • 电子口岸卡密码错

    中国电子口岸IC卡累计密码输错五次会被锁住,且密码输错次数是不可恢复的,即第二天及其以后不会重新统计次数

    2023-12-14 22:42:09