调整图像大小以完美适合确定的框

发布于 2024-11-28 22:12:56 字数 329 浏览 5 评论 0原文

我想要执行以下操作:

  • 我需要调整图像大小以适合并填充 100x100 像素框
    并非所有图像都是完美的正方形,这就是我这项任务的问题
    1. 我想确定哪一边(宽度或高度)最小。
    2. 将该边缩小到 100 像素;中后卫
    3. 裁剪两侧的溢出

我已经尝试过宽度/高度:100% 技术,但这并没有给出我想要的结果(它被拉伸并且丑陋)。我还看到了一些方法,但是在 C# 中......我需要在 PHP 中。

我将不胜感激任何建议、指示、输入或准备好的脚本...... 感谢

使用 PHP 5.3.0

I want to do the following :

  • I need to resize images to fit and fill enterly 100x100 pixel box
    not all image are perfect squares, and thats my problem for this task

    1. I want to determine wich side (width or height) is the smallest.
    2. Bring that Side to 100px while scaling down; Center back
    3. Crop the overflow on both side

I've tried the width/height :100% technic but that doesnt give the result i want, (it is streched and ugly). Also i've seen some method but in C# ... I need in PHP.

I would appreciate any advice, directions, input, or ready script...
Thanks

using PHP 5.3.0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

莳間冲淡了誓言ζ 2024-12-05 22:12:56

您必须能够通过事先了解图像尺寸来计算纵横比。更多信息请参见 getimagesize()

$width = 268;
$height = 300;
$MAX_SIZE = 100;
if($width > $MAX_SIZE || $height > $MAX_SIZE) {
    $aspect = $width / $height;
    if($width > $height) {
        $width = $MAX_SIZE;
        $height = intval($MAX_SIZE / $aspect);
    } else {
        $height = $MAX_SIZE;
        $width = intval($MAX_SIZE * $aspect);
    }
}

之后就可以得到缩小后的高度在 $height 中,缩小后的宽度将在 $width 中可用。

You must be able to calculate the aspect ratio by knowing the image dimensions beforehand. For more info, see getimagesize()

$width = 268;
$height = 300;
$MAX_SIZE = 100;
if($width > $MAX_SIZE || $height > $MAX_SIZE) {
    $aspect = $width / $height;
    if($width > $height) {
        $width = $MAX_SIZE;
        $height = intval($MAX_SIZE / $aspect);
    } else {
        $height = $MAX_SIZE;
        $width = intval($MAX_SIZE * $aspect);
    }
}

Afterwards, the scaled down height will be available in $height and the scaled down width will be available in $width.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文