我想知道是否有办法将上传的媒体精确缩小到50%。
据我所见add_image_size
只能以像素为单位指定宽度和高度。
其想法是为视网膜显示器上传高分辨率图像,然后让Wordpress为标准显示器生成一个按比例缩小的自定义图像大小。
最好将其应用于原始图像以及生成的所有其他自定义缩略图。
谢谢你的帮助。
我想知道是否有办法将上传的媒体精确缩小到50%。
据我所见add_image_size
只能以像素为单位指定宽度和高度。
其想法是为视网膜显示器上传高分辨率图像,然后让Wordpress为标准显示器生成一个按比例缩小的自定义图像大小。
最好将其应用于原始图像以及生成的所有其他自定义缩略图。
谢谢你的帮助。
您可以使用image_downsize
过滤并捕获WordPress何时需要缩小尺寸的图像而不是原始图像,以及实际上不存在的大小。
add_filter( \'image_downsize\', \'wpse_60890_retina_scale\', 10, 3 );
function wpse_60890_retina_scale( $value, $id, $size ) {
if ( $size == \'wpse_60890_retina_scaled\' ) {
if ( !is_array( $imagedata = wp_get_attachment_metadata( $id ) ) )
return false;
$img_url = wp_get_attachment_url( $id );
$img_url_basename = wp_basename( $img_url );
if ( isset( $imagedata[\'sizes\'][\'wpse_60890_retina_scaled\'] ) ) {
$intermediate = $imagedata[\'sizes\'][\'wpse_60890_retina_scaled\'];
$url = str_replace($img_url_basename, $intermediate[\'file\'], $img_url);
return array( $url, $data[\'width\'], $data[\'height\'] );
}
$file = get_attached_file( $id );
$image = wp_load_image( $file );
if ( !is_resource( $image ) )
return new WP_Error( \'error_loading_image\', $image, $file );
$size = @getimagesize( $file );
if ( !$size )
return new WP_Error( \'invalid_image\', __(\'Could not read image size\'), $file );
list( $orig_w, $orig_h, $orig_type ) = $size;
$max_w = round( $orig_w / 2 );
$max_h = round( $orig_h / 2 );
$resized = image_make_intermediate_size( $file, $max_w, $max_h );
$imagedata[\'sizes\'][\'wpse_60890_retina_scaled\'] = $resized;
wp_update_attachment_metadata( $id, $imagedata );
$url = str_replace($img_url_basename, $resized[\'file\'], $img_url);
return array( $url, $resized[\'width\'], $resized[\'height\'] );
}
return $value;
}
现在,您必须确定需要全尺寸或按50%缩放的位置。例如,您有
echo \'full size:\' . wp_get_attachment_image( $attachment->ID, \'full\' );
echo \'scaled by 50%: \' . wp_get_attachment_image( $attachment->ID, \'wpse_60890_retina_scaled\' );
我无法按原来的方式使用@nvartolomei解决方案。这不是功能性的,但仍然是一个很好的起点。
这是我的最终解决方案:
我添加3个新image sizes
将原来的3种尺寸翻倍。
然后我在PHP中调用它们时放入过滤器,以替换with
和height
非视网膜等效物(因为它并不总是除以2)。
原件:src=image.jpg
宽度=4800
高度=4000
image-600x500.jpg 宽度=600
高度=500
image-1200x1000.jpg 宽度=600
高度=500
原文:src=image.jpg
宽度=900
高度=750
大型(600x600):src=image-600x500.jpg
宽度=600
高度=500
image.jpg 宽度=600
高度=500
功能。php:
add_action( \'init\', \'cjg_register_sizes\' );
add_filter( \'image_downsize\', \'cjg_retina_scale\', 10, 3 );
function cjg_register_sizes(){
add_image_size( \'large_retina\', get_option( \'large_size_w\' ) * 2 , get_option( \'large_size_h\' ) * 2 );
add_image_size( \'medium_retina\', get_option( \'medium_size_w\' ) * 2 , get_option( \'medium_size_h\' ) * 2 );
add_image_size( \'thumbnail_retina\', get_option( \'thumbnail_size_w\' ) * 2 , get_option( \'thumbnail_size_h\' ) * 2 );
}
function cjg_retina_scale( $value, $id, $size ) {
if ( $size == \'large_retina\' OR $size == \'medium_retina\' OR $size == \'thumbnail_retina\' ) {
if ( !is_array( $imagedata = wp_get_attachment_metadata( $id ) ) )
return false;
if ( $size == \'large_retina\') $regular_size = \'large\';
elseif ( $size == \'medium_retina\') $regular_size = \'medium\';
else $regular_size = \'thumbnail\';
$img_url = wp_get_attachment_url( $id );
$img_url_basename = wp_basename( $img_url );
if ( isset( $imagedata[\'sizes\'][$regular_size] ) ) {
$width = $imagedata[\'sizes\'][$regular_size][\'width\'];
$height = $imagedata[\'sizes\'][$regular_size][\'height\'];
if ( isset( $imagedata[\'sizes\'][$size] ) )
$url = str_replace($img_url_basename, $imagedata[\'sizes\'][$size][\'file\'], $img_url);
else
$url = $img_url;
}else{
$url = $img_url;
$width = $imagedata[\'width\'];
$height = $imagedata[\'height\'];
}
return array( $url, $width, $height );
}
return $value;
}
现在你可以这样称呼你的图片wp_get_attachment_image( $attachment->ID, \'thumbnail_retina\' );
wp_get_attachment_image( $attachment->ID, \'medium_retina\' );
wp_get_attachment_image( $attachment->ID, \'large_retina\' );
重要提示:添加3个新尺寸后,我用the regenerate-thumbnails plugin我有一个博客,所有媒体都存储在一个名为upload (organize media已禁用),我已激活它以按日期组织新的上载。But how can i organize old files?我想要这个,因为我需要将站点迁移到Wordpress MU站点,并且需要组织文件。