【问题标题】:How to calculate where to crop an image - imagecopyresampled如何计算裁剪图像的位置 - imagecopyresampled
【发布时间】:2019-06-29 07:59:22
【问题描述】:

现在我的代码为我提供了一个具有正确按比例缩小的高度和宽度的图像,但使用了原始图像的顶部。我希望它使用图像的中心。但我不知道如何计算 imagecopyresampled 的 dst_y 值来执行此操作。谁能帮我解决:-)

<?php

function ReSize ($source,$destination,$dest_imagex,$dest_imagey,$quality) {

$source_image = imagecreatefromjpeg($source);
// Get dimensions of the original image
$source_imagex = imagesx($source_image);
$source_imagey = imagesy($source_image);

$after_width = $dest_imagex;

    //get the reduced width
    $reduced_width = ($source_imagex - $after_width);
    //now convert the reduced width to a percentage and round it to 2 decimal places
    $reduced_radio = round(($reduced_width / $source_imagex) * 100, 2);

    //ALL GOOD! let's reduce the same percentage from the height and round it to 2 decimal places.
    $reduced_height = round(($source_imagey / 100) * $reduced_radio, 2);
    //reduce the calculated height from the original height
    $after_height = $source_imagey - $reduced_height;

      $dst_y = 0; // The calculation I cannot figure out.....

$dest_image = imagecreatetruecolor($dest_imagex, $dest_imagey);
imagecopyresampled($dest_image, $source_image, 0, $dst_y, 0, 0, $after_width, $after_height, $source_imagex, $source_imagey);

$cache_folder = 'images/cache/';
$new_image = $cache_folder . rawurlencode($destination). '_' . $dest_imagex . 'x' . $dest_imagey . '.jpg';
imagejpeg($dest_image, $new_image,$quality);

echo $new_image;
}
?>

【问题讨论】:

    标签: php image image-processing


    【解决方案1】:

    假设你想要一个 CSS “cover” 类型调整大小,你有两个选择:

    • 垂直适合图像
    • 水平适合的图片

    先找到源和目标的比例:

    $dest_ratio = $dest_imagex / $dest_imagey;
    $source_ratio = $source_imagex / $source_imagey;
    

    如果源比率大于目标比率,则意味着您需要水平拟合比例,否则需要垂直拟合比例。


    水平拟合比例

    imagecopyresampled(
        $dest_image,
        $source_image,
        0,
        0,
        0,
        ($source_imagey - ($source_imagex * $dest_ratio)) / 2,
        $dest_imagex,
        $dest_imagex * $source_ratio,
        $source_imagex,
        $source_imagey
    );
    

    垂直拟合比例

    imagecopyresampled(
        $dest_image,
        $source_image,
        0,
        0,
        ($source_imagex - ($source_imagey / $dest_ratio)) / 2,
        0,
        $dest_imagey / $source_ratio,
        $dest_imagey,
        $source_imagex,
        $source_imagey
    );
    

    一起来:

    function crop($source, $dest, $dest_imagex, $dest_imagey, $quality)
    {
        $source_image = imagecreatefromjpeg($source);
        // Get dimensions of the original image
        $source_imagex = imagesx($source_image);
        $source_imagey = imagesy($source_image);
        $source_ratio = $source_imagey / $source_imagex;
        $dest_ratio = $dest_imagey / $dest_imagex;
    
        $dest_image = imagecreatetruecolor($dest_imagex, $dest_imagey);
        imagefill($dest_image, 0, 0, imagecolorallocate($dest_image, 255, 0, 0));
    
        if ($dest_ratio >= $source_ratio) {
            imagecopyresampled(
                $dest_image,
                $source_image,
                0,
                0,
                ($source_imagex - ($source_imagey / $dest_ratio)) / 2,
                0,
                $dest_imagey / $source_ratio,
                $dest_imagey,
                $source_imagex,
                $source_imagey
            );
        } else {
            imagecopyresampled(
                $dest_image,
                $source_image,
                0,
                0,
                0,
                ($source_imagey - ($source_imagex * $dest_ratio)) / 2,
                $dest_imagex,
                $dest_imagex * $source_ratio,
                $source_imagex,
                $source_imagey
            );
        }
        imagejpeg($dest_image, $dest, $quality);
    }
    

    用法

    crop("image1.jpg", "image1_resized.jpg", 100, 100, 100);
    

    【讨论】:

    • 你是直接测试还是适应你的代码?我也在本地对其进行了测试,它似乎有效..奇怪。我将再次使用非平方尺寸进行测试。
    • 直接使用您的代码。只添加了 echo $dest;在底部获取新文件名的输出。
    • 完美运行! :-) 非常感谢! :-)
    • 我想更正您的回答。这不是 css“覆盖”行为,它实际上是“包含”行为。 “封面”所做的是伸展填充。 CSS“包含”确实会拉伸以适应。
    • Cover 允许图像溢出容器,而 contains 不允许。覆盖并包含按比例拉伸以填充容器。
    猜你喜欢
    • 2011-03-18
    • 2016-05-21
    • 2011-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-05
    • 2013-03-25
    相关资源
    最近更新 更多