我打算开发一个视网膜就绪的主题。我上传了retina.min.js
在我的主题文件夹中:
http://riehling.mrcoolblog.com/wp-content/themes/riehling/js/retina.min.js
在我的
functions.php
, 我添加了以下代码:
add_action( \'wp_enqueue_scripts\', \'retina_support_enqueue_scripts\' );
function retina_support_enqueue_scripts() {
wp_enqueue_script( \'retina_min_js\', get_template_directory_uri() . \'/js/retina.min.js\', null, \'\', true );
}
add_filter( \'wp_generate_attachment_metadata\', \'retina_support_attachment_meta\', 10, 2 );
function retina_support_attachment_meta( $metadata, $attachment_id ) {
foreach ( $metadata as $key => $value ) {
if ( is_array( $value ) ) {
foreach ( $value as $image => $attr ) {
if ( is_array( $attr ) )
retina_support_create_images( get_attached_file( $attachment_id ), $attr[\'width\'], $attr[\'height\'], true );
}
}
}
return $metadata;
}
function retina_support_create_images( $file, $width, $height, $crop = false ) {
if ( $width || $height ) {
$resized_file = wp_get_image_editor( $file );
if ( ! is_wp_error( $resized_file ) ) {
$filename = $resized_file->generate_filename( $width . \'x\' . $height . \'@2x\' );
$resized_file->resize( $width * 2, $height * 2, $crop );
$resized_file->save( $filename );
$info = $resized_file->get_size();
return array(
\'file\' => wp_basename( $filename ),
\'width\' => $info[\'width\'],
\'height\' => $info[\'height\'],
);
}
}
return false;
}
add_filter( \'delete_attachment\', \'delete_retina_support_images\' );
function delete_retina_support_images( $attachment_id ) {
$meta = wp_get_attachment_metadata( $attachment_id );
$upload_dir = wp_upload_dir();
$path = pathinfo( $meta[\'file\'] );
foreach ( $meta as $key => $value ) {
if ( \'sizes\' === $key ) {
foreach ( $value as $sizes => $size ) {
$original_filename = $upload_dir[\'basedir\'] . \'/\' . $path[\'dirname\'] . \'/\' . $size[\'file\'];
$retina_filename = substr_replace( $original_filename, \'@2x.\', strrpos( $original_filename, \'.\' ), strlen( \'.\' ) );
if ( file_exists( $retina_filename ) )
unlink( $retina_filename );
}
}
}
}
它确实创建了一个@2x文件(尽管我很确定它会拉伸缩略图,而不是使用足够宽的源文件)。
但是,Retina JS脚本没有处理图像:
http://riehling.mrcoolblog.com/projets/scenographie/test-retina/
我正在使用
<?php the_post_thumbnail( \'medium\' ); ?>
要显示图像和输出,如下所示:
<img width="236" height="310" src="http://riehling.mrcoolblog.com/wp-content/uploads/f2-236x310.jpg" class="attachment-medium size-medium wp-post-image" alt="Tiger" srcset="http://riehling.mrcoolblog.com/wp-content/uploads/f2-236x310.jpg 236w, http://riehling.mrcoolblog.com/wp-content/uploads/f2-768x1009.jpg 768w, http://riehling.mrcoolblog.com/wp-content/uploads/f2-472x620.jpg 472w, http://riehling.mrcoolblog.com/wp-content/uploads/f2.jpg 944w" sizes="(max-width: 236px) 100vw, 236px">