【问题标题】:How do I force a WP_Image_Editor.resize to stretch my image if needed?如果需要,如何强制 WP_Image_Editor.resize 拉伸我的图像?
【发布时间】:2014-06-03 13:47:36
【问题描述】:

当我调用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" );

如何让它拉伸到合适的大小和比例?

EDIT 以下是上述每个函数调用返回的内容:

//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"
}

【问题讨论】:

    标签: php wordpress imagemagick imagick


    【解决方案1】:

    尝试在$image->save(); 调用中添加文件名。

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

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

    【讨论】:

    • 如果你尝试你的命令然后检查它是否没有返回 WP_Error [1]。就像上面的文档一样。 [1]codex.wordpress.org/Class_Reference/WP_Error
    • 我确实检查了错误,它说一切都很好。没有错误。
    • 如果这是我,那么接下来我会检查 WP_DEBUG 是否打开,看看是否导致任何错误,或者是否是权限错误。如果你 var_dump 的结果是 $image->save('new-file.jpg');
    • 我都做到了。没有错误。当我转储返回值时,它会显示没有错误时应该显示的所有内容。相信我,没有错误。我认为当某个设置通过时,imagemagick 没有按比例放大是一个问题(这是由 WordPress 在幕后完成的)
    • 您在什么操作系统/环境中开发?服务器错误日志?
    【解决方案2】:

    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 );
    

    以上将忽略纵横比和诸如此类的东西,只返回您要求的内容并导致图像拉伸。小心点。

    【讨论】:

      猜你喜欢
      • 2017-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-20
      • 1970-01-01
      • 2012-02-02
      • 2017-11-09
      相关资源
      最近更新 更多