我用add\\u image\\u size注册了3个自定义图像大小(双倍大小,用于视网膜显示):thumbnail@2x,则,medium@2x和large@2x.
function retina_image_sizes() {
$retina_sizes = array(
\'thumbnail@2x\' => array(
\'width\' => get_option( \'thumbnail_size_w\' ) * 2,
\'height\' => get_option( \'thumbnail_size_h\' ) * 2,
\'crop\' => get_option(\'thumbnail_crop\'),
),
\'medium@2x\' => array(
\'width\' => get_option( \'medium_size_w\' ) * 2,
\'height\' => get_option( \'medium_size_h\' ) * 2,
\'crop\' => false,
),
\'large@2x\' => array(
\'width\' => get_option( \'large_size_w\' ) * 2,
\'height\' => get_option( \'large_size_h\' ) * 2,
\'crop\' => false,
)
);
foreach ( $retina_sizes as $name => $data ) {
add_image_size( $name, $data[\'width\'], $data[\'height\'], $data[\'crop\'] );
}
}
add_action( \'init\', \'retina_image_sizes\');
Wordpress的命名约定是文件名宽度x高度。jpg。但我需要用另一种方式命名它们(filename originalwidth
[email protected])
例如,如果我上载了一张图片,缩略图的大小为100x100,我希望将这些图像命名为:
example-100x100.jpg
and
[email protected]
我找到了处理图片并用另一个名称保存的方法,但这太多了。这
question 说明如何更改文件名,但不说明如何包含with和height信息。我试图让它工作,但我只得到一个文件,而不是七个,它会抛出一些错误(可能是因为它试图覆盖文件)。
如何以上述方式更改文件名?add\\u附件挂钩是否正确?
最合适的回答,由SO网友:Sebastian Starke 整理而成
我用了不同的方式。我只需要更新Gerasimos Tsiamalos Retina Plugin 至Wordpress 3.5。,使用图像编辑器而不是调整图像大小。
这就是它的样子:
function nothing_image_make_retina_size($file, $width, $height, $crop=false) {
if ( $width || $height ) {
$resized_file = wp_get_image_editor($file);
if ( ! is_wp_error( $resized_file ) ) {
$resized_file->resize( $width*2, $height*2, $crop );
$filename = $resized_file->generate_filename( $width . \'x\' . $height . \'@2x\' );
$resized_file->save($filename);
}
if ( !is_wp_error($resized_file) && $resized_file && $info = getimagesize($filename) ) {
$filename = apply_filters(\'nothing_retina_image_make_intermediate_size\', $filename);
return array(
\'file\' => wp_basename( $filename ),
\'width\' => $info[0],
\'height\' => $info[1],
);
}
}
return false;
}
function nothing_generate_retina_image_metadata( $metadata, $attachment_id ) {
$attachment = get_post( $attachment_id );
$file = get_attached_file($attachment_id);
$old_metadata = $metadata;
foreach ($metadata as $k => $v) {
if (is_array($v)) {
foreach ($v as $key => $val) {
if (is_array($val)) {
nothing_image_make_retina_size($file, $val[\'width\'], $val[\'height\'], true);
}
}
}
}
return $old_metadata;
}
add_filter(\'wp_generate_attachment_metadata\', \'nothing_generate_retina_image_metadata\', 10, 2);
function nothing_delete_retina_images( $attachment_id ) {
$nothing_metas = wp_get_attachment_metadata( $attachment_id );
$nothing_updir = wp_upload_dir();
$nothing_path = pathinfo($nothing_metas[\'file\']);
$nothing_path_name = $nothing_path[\'dirname\'];
$nothing_updir = wp_upload_dir();
foreach ($nothing_metas as $nothing_meta => $nothing_meta_val) {
if ($nothing_meta === "sizes") {
foreach ($nothing_meta_val as $nothing_sizes => $nothing_size) {
$nothing_original_filename = $nothing_updir[\'basedir\'] . "/" . $nothing_path_name . "/" . $nothing_size[\'file\'];
$nothing_x2_filename = substr_replace($nothing_original_filename, "@2x.", strrpos($nothing_original_filename, "."), strlen("."));
if (file_exists($nothing_x2_filename)) {
unlink($nothing_x2_filename);
}
}
}
}
}
add_filter(\'delete_attachment\',\'nothing_delete_retina_images\');