【问题标题】:Remove WordPress auto cropping images completely完全删除 WordPress 自动裁剪图像
【发布时间】:2013-08-14 07:24:13
【问题描述】:

我正在制作一个自定义主题,并且已经完成了该网站。我正在使用自定义字段的类型,用于上传稍后放入轮播的图像。我发现当我上传 1069x1060 的图像时,它没有被裁剪或触摸,并且图像的整个内容都是可见的。现在,当我上传 850x700 的图片时,图像会被裁剪,整个区域不可见

这可以在http://blog.jmrowe.com/portfolio看到

此外,我在设置 -> 媒体下取消选中“将缩略图裁剪为精确尺寸(通常缩略图是成比例的)”选项,这没有帮助。我没有添加任何自定义特征图像尺寸。

奇怪的是,当我选择它在微小的预览屏幕中显示的图像时,图像未被裁剪。但是,当实际访问该站点时,图像会被裁剪。我还选择了使用“全尺寸”

有人可以帮忙吗?

我正在使用类型自定义字段插件在主题中实际显示这些:

<?php if (!((get_post_meta($post->ID, 'wpcf-portfoliopicture1', TRUE))=='')): ?>
<div class="row">
                        <div class="span8">
             <div class="span6 offset1" style="padding-left:20px;">
             <?php
$param['height']=350;
$param['width']=325; 
$param['alt']=types_render_field("portfoliotitle1",$param2);
$param['title']=types_render_field("portfoliotitle1",$param2);
?>
<ul class="round">
<li><?php echo(types_render_field("portfoliopicture1", $param)); ?></li>
<?php
$param['alt']=types_render_field("portfoliotitle2",$param2);
$param['title']=types_render_field("portfoliotitle2",$param2);
 ?>
<li><?php echo(types_render_field("portfoliopicture2", $param)); ?></li>
<?php
$param['alt']=types_render_field("portfoliotitle3",$param2);
$param['title']=types_render_field("portfoliotitle3",$param2);
 ?>
<li><?php echo(types_render_field("portfoliopicture3", $param)); ?></li>
</ul>
</div>



                        </div>
                    </div>
<?php endif; ?>

我注意到如果我进入 wp-includes/media.php 并转到函数 image_resize_dimensions

我可以编辑掉一些功能,它不会自动裁剪图像..但是,它也不会调整它们的大小,所以原生 1000px x 900px 的图片就是这样

我很确定问题出在下面的 wp-includes\media.php 函数中:

function image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop = false) {


    if ($orig_w <= 0 || $orig_h <= 0)
        return false;
    // at least one of dest_w or dest_h must be specific
    if ($dest_w <= 0 && $dest_h <= 0)
        return false;

    // plugins can use this to provide custom resize dimensions
    $output = apply_filters( 'image_resize_dimensions', null, $orig_w, $orig_h, $dest_w, $dest_h, $crop );
    if ( null !== $output )
        return $output;

    if ( $crop ) { 

        // crop the largest possible portion of the original image that we can size to $dest_w x $dest_h
        $aspect_ratio = $orig_w / $orig_h;
        $new_w = min($dest_w, $orig_w);
        $new_h = min($dest_h, $orig_h);

        if ( !$new_w ) {
            $new_w = intval($new_h * $aspect_ratio);
        }

        if ( !$new_h ) {
            $new_h = intval($new_w / $aspect_ratio);
        }

        $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 );
    } else {
        // don't crop, just resize using $dest_w x $dest_h as a maximum bounding box
        $crop_w = $orig_w;
        $crop_h = $orig_h;

        $s_x = 0;
        $s_y = 0;

        list( $new_w, $new_h ) = wp_constrain_dimensions( $orig_w, $orig_h, $dest_w, $dest_h );
    }

    // if the resulting image would be the same size or larger we don't want to resize it
    if ( $new_w >= $orig_w && $new_h >= $orig_h )
        return false;

    // the return array matches the parameters to imagecopyresampled()
    // int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h
    return array( 0, 0, (int) $s_x, (int) $s_y, (int) $new_w, (int) $new_h, (int) $crop_w, (int) $crop_h );

}

我知道我不应该弄乱核心文件,但是当我这样做时会影响我的问题,但我不知道什么会解决它。该函数由插件调用(类型 - 自定义类型和字段),这是进行裁剪的地方

【问题讨论】:

  • 我注意到只有在图像长度低于 1000 像素时才会发生裁剪。有什么想法可以完全防止裁剪吗?
  • 显示这些图像的代码在哪里?您使用哪个 WP 功能来显示它们?
  • @BenRacicot 我编辑了帖子以显示
  • 看起来您正在使用 types_render_field 显示图像。我不知道那个插件,但是使用 get_post_custom 函数访问自定义字段值已经解决了其他人的问题。然后使用所需大小回显本机 get_the_post_thumbnail($id, $size, $attr ) 将确保正确的图像大小。抱歉,我帮不上忙。
  • 我不明白如果我使用 get_post_custom 来获取它适用于 get_the_post_thumbnail 的 img url? get_the_post_thumbnail 不会返回 1 个项目吗?在这种情况下有 3 个。感谢您的帮助

标签: image wordpress crop


【解决方案1】:

我只是 dl'ed 并浏览了这些文件。从您所说的来看,尽管代码正确(已通过 include/fields/image.php 中的说明确认),但它显然抓取了一个错误的图像大小

您可以尝试自定义插件。我找到了wp_handle_upload 来覆盖上传过程。

if ( ! function_exists( 'wp_handle_upload' ) ) require_once( ABSPATH . 'wp-admin/includes/file.php' );
$uploadedfile = $_FILES['file'];
$upload_overrides = array( 'test_form' => false );
$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
if ( $movefile ) {
    echo "File is valid, and was successfully uploaded.\n";
    var_dump( $movefile);
} else {
    echo "Possible file upload attack!\n";
}

除此之外,我还必须与开发人员讨论您的问题。如果没有登录到您的网站,我找不到其他任何帮助。 :) 希望你最好,我会看这个页面。

【讨论】:

  • 我最终编辑了从插件调用 image_resize_dimensions 的类型插件,并手动将 $crop 设置为 false。它停止了图像的裁剪,但现在我正在处理正确调整图像大小的问题。我应该只使用 laravel 而不是 wordpress 但是哦,好吧
  • 呃,对不起。您可以通过 Photoshop 将它们编辑为正确大小的预上传,以便获得一致的结果吗?没用过 Laravel
  • 是的,这就是我正在做的事情。没有我希望的那么精确,但我想这是 wordpress 的陷阱之一。感谢您的帮助@BenRacicot
猜你喜欢
  • 2014-08-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-18
  • 1970-01-01
  • 2017-01-23
  • 2012-07-31
  • 2023-03-10
  • 1970-01-01
相关资源
最近更新 更多