繁体   English   中英

如果需要,如何强制 WP_Image_Editor.resize 拉伸我的图像?

[英]How do I force a WP_Image_Editor.resize to stretch my image if needed?

当我调用函数WP_Image_Editor->resize(...)并且图像的高度太小时,它会忽略我传入的高度。例如:

//This image is 300 x 225
$image = wp_get_image_editor( "/var/www/wp-content/uploads/2014/03/test.jpg" );

//I tell it to resize to 290 x 290
$image->resize( 290, 290 ); //Passing "true" to crop doesn't help)

//It saves, but as 290 x 225!
$image->save( "/var/www/wp-content/uploads/2014/03/test-22.jpg" );

我如何使它拉伸到合适的尺寸和比例?

编辑这是上面每个函数调用返回的内容:

//var_dump of wp_get_image_editor( $myImagePath );
object(WP_Image_Editor_Imagick)#109 (6) {
    ["image":protected]=>
    object(Imagick)#108 (0) {
    }
    ["file":protected]=>
    string(65) "/var/www/wp-content/uploads/2014/03/test.jpg"
    ["size":protected]=>
    array(2) {
        ["width"]=>
        int(300)
        ["height"]=>
        int(225)
    }
    ["mime_type":protected]=>
    string(10) "image/jpeg"
    ["default_mime_type":protected]=>
    string(10) "image/jpeg"
    ["quality":protected]=>
    int(90)
}

//var_dump of $image->resize( 290, 290 );
bool(true)

//var_dump of $image->save( "/var/www/wp-content/uploads/2014/03/test-22.jpg" );
array(5) {
    ["path"]=>
    string(68) "/var/www/wp-content/uploads/2014/03/test-22.jpg"
    ["file"]=>
    string(11) "test-22.jpg"
    ["width"]=>
    int(294)
    ["height"]=>
    int(225)
    ["mime-type"]=>
    string(10) "image/jpeg"
}

尝试将文件名添加到您的$image->save(); 称呼。

如果您参考文档 [1],您可以看到它们传入了一个新文件名。

[1] http://codex.wordpress.org/Function_Reference/wp_get_image_editor

WordPress 有一个来自image_resize_dimensions函数(在 wp-includes/media.php 中)的内置限制。

// Stop if the destination size is larger than the original image dimensions.

不过,有一个名为image_resize_dimensions的过滤器,您可以像这样使用它:

add_filter( 'image_resize_dimensions', function( $output, $orig_w, $orig_h, $dest_w, $dest_h, $crop ) {
    return array( 0, 0, 0, 0, $dest_w, $dest_h, $orig_w, $orig_h );
}, 10, 6 );

以上将忽略纵横比等,只返回您要求的内容并导致拉伸图像。 当心。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM