wordpress - 如何 add_image_size 处理较小的图像?

标签 wordpress

我在functions.php中添加了add_image_size,当图像足够大时,它会调整图像的大小,但当图像小于该大小时,它不会调整图像的大小。

例如,我添加了 add_image_size( 'category', 255, 150, true );。 但是用户上传一些尺寸为 150x150 的图像,它不够大。

这是我的代码,用于在图像小于我需要的尺寸时调整图像大小:

if ( !function_exists( 'mit_resize') ) {
    function mit_resize( $attachment, $width, $height, $url = false, $crop = true ) {

    $siteurl = get_option( 'siteurl' );

    if ( $url === false ) {
        $file_path = get_attached_file( $attachment );
    } else if ( $attachment ) {
        $file_path = get_attached_file(mit_get_attachment_id($attachment));
    }
    if ( empty($file_path) )
        return false;

    $file_info = pathinfo( $file_path );
    $extension = '.'. $file_info['extension'];
    $base_file = $file_info['dirname'].'/'.$file_info['filename'].$extension;

    $sizes = mit_get_image_sizes();
    $general = getimagesize($base_file);
    foreach ($sizes as $key => $size) {
        if($size['width'] == $width && $size['height'] == $height && $general[0] >= $size['width'] && $general[1] >= $size['height']) {
            $path = explode('wp-content',$file_info['dirname'].'/'.$file_info['filename'].'-'.$width.'x'.$height.$extension);
            return $siteurl . '/wp-content' . $path[1];
        }

    }

    if (file_exists($base_file))
    {
        $no_ext_path = $file_info['dirname'].'/'.$file_info['filename'];

        $img_resize = wp_get_image_editor( $no_ext_path.$extension);
        if ( ! is_wp_error( $img_resize ) ) {
            $cropped_img_path = $no_ext_path.'-'.$width.'x'.$height.$extension;
            $img_resize->resize( $width, $height, $crop );
            $img_resize->save($cropped_img_path);
            if (preg_match("/wp-content/", $cropped_img_path) != false) {
                $path = explode('wp-content',$cropped_img_path);
            }
            else
                return 'http://placehold.it/' . $width .'x' . $height . '?text=Img';
            $url_file = $siteurl . '/wp-content' .$path[1];
        }
    }

    if ( !file_exists($cropped_img_path) )
    {
        return 'http://placehold.it/' . $width .'x' . $height . '?text=Img';
    }
    else
        return $url_file;
}

}

我可以将此函数挂接到add_image_size吗?

最佳答案

我为任何有需要的人找到了答案:

if(!function_exists('mit_thumbnail_upscale')) {
function mit_thumbnail_upscale( $default, $orig_w, $orig_h, $new_w, $new_h, $crop ){

    if ( !$crop ) return null; // let the wordpress default function handle this

    $aspect_ratio = $orig_w / $orig_h;
    $size_ratio = max($new_w / $orig_w, $new_h / $orig_h);

    $crop_w = round($new_w / $size_ratio);
    $crop_h = round($new_h / $size_ratio);

    $s_x = floor( ($orig_w - $crop_w) / 2 );
    $s_y = floor( ($orig_h - $crop_h) / 2 );

    return array( 0, 0, (int) $s_x, (int) $s_y, (int) $new_w, (int) $new_h, (int) $crop_w, (int) $crop_h );
}
}
add_filter( 'image_resize_dimensions', 'mit_thumbnail_upscale', 10, 6 );

add_image_size之后,您只需将此代码放入函数中即可。如果客户端上传的图片小于您的尺寸,请不要担心,mit_thumbnail_upscale 将为您提供帮助。

关于wordpress - 如何 add_image_size 处理较小的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36396008/

相关文章:

php - 第二个查询没有在 php mysql 中执行

wordpress 列出用户角色

javascript - 使用 Bootstrap 模式作为成人内容警告的问题

mysql - 使用 WordPress get_results() 数据库函数是否可以防止 sql 注入(inject)

mysql - 如何按 ID 过滤行,然后更新具有相同 ID 的其他行

html - 更改为 block 并添加 &nbsp 后 Wordpress 跨度宽度不变

javascript - 如何在 WordPress header 中加载自定义 jQuery?

php - 如何自定义 WordPress 中的 Subscribe2 按钮外观?

php - 使用过滤器将 html 添加到 WooCommerce 商店通知

php - 修复 Wordpress 插件代码中 CodeSniffer 问题的工具