[英]How do I force a WP_Image_Editor.resize to stretch my image if needed?
When I call the function WP_Image_Editor->resize(...)
and the image's height is too small, it ignores the height I pass in. For example:当我调用函数
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" );
How do I make it stretch to the proper size and ratio?我如何使它拉伸到合适的尺寸和比例?
EDIT Here is what's returned by each function call above:编辑这是上面每个函数调用返回的内容:
//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"
}
Try adding a filename into your $image->save();
尝试将文件名添加到您的
$image->save();
call.称呼。
If you refer to the docs [1] you can see they pass in a new filename.如果您参考文档 [1],您可以看到它们传入了一个新文件名。
[1] http://codex.wordpress.org/Function_Reference/wp_get_image_editor [1] http://codex.wordpress.org/Function_Reference/wp_get_image_editor
WordPress has a built-in limit that comes from the image_resize_dimensions
function (in wp-includes/media.php). WordPress 有一个来自
image_resize_dimensions
函数(在 wp-includes/media.php 中)的内置限制。
// Stop if the destination size is larger than the original image dimensions.
There's a filter, though, called image_resize_dimensions
which you can use like so:不过,有一个名为
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 );
The above will ignore aspect ratios and whatnot and just return what you asked for and results in stretched images.以上将忽略纵横比等,只返回您要求的内容并导致拉伸图像。 Be careful.
当心。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.