簡體   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