php:创建自定义大小的缩略图

发布于 2024-12-21 03:31:16 字数 159 浏览 1 评论 0原文

我熟悉使用 imagecopyresampled 在 php 下调整图像大小和裁剪图像,但现在我遇到了一个特殊问题: 该任务是从例如裁剪大图像。 1600x1200 至 500x120,这意味着将大小调整为 500 像素,并将其高度裁剪为 120 像素。有没有一些简单的方法或者我需要自己计算裁剪值?谢谢

i'm familiar with resizing and cropping images under php using imagecopyresampled but now i'm having a special problem:
the task is cropping a large image from eg. 1600x1200 to 500x120, which means resizing down to 500px and crop its height that it'S 120px. is there some easy way or do i need to calculate the cropping values all on my own? thanks

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

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

发布评论

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

评论(2

我不会写诗 2024-12-28 03:31:16

有一个名为 PHPThumb 的 PHP 库可以帮助您。您可以在这里找到 https://github.com/masterexploder/PHPThumb

他们有一个自适应调整大小方法,可以你在寻找什么。 https://github.com/masterexploder/PHPThumb/wiki/Basic-Usage

There is PHP library that could help you out called PHPThumb. You can find here https://github.com/masterexploder/PHPThumb

They have an adaptive resize method that does what you're looking for. https://github.com/masterexploder/PHPThumb/wiki/Basic-Usage

时光磨忆 2024-12-28 03:31:16

你必须自己做。

我不知道你是否想要裁剪,所以这里是如何计算两者的值:

缩放图像:调整大小以适应新的 wxh 保持纵横比(因此 1 条边可能短于指定)

function calc_scale_dims($width_orig, $height_orig, $max_width, $max_height) { 

    $new_width=$width_orig;
    $new_height=$height_orig;
    $ratioh = $max_height/$new_height;
    $ratiow = $max_width/$new_width;
    $ratio = min($ratioh, $ratiow);
    // New dimensions
    $dims["w"] = intval($ratio*$new_width);
    $dims["h"] = intval($ratio*$new_height); 

    return $dims;
}

调整大小和裁剪:如果新的宽高比不同,则调整图像大小并裁剪它以适合指定的宽x高(例如,如果宽高比不同,图像将调整大小以匹配短尺寸上的指定尺寸)以及较长的尺寸,如果中间被剪掉了)

function calc_crop_resize_dims($width_orig, $height_orig, $new_width, $new_height) { 

    //Calculate scaling
    $ratio_orig = $width_orig/$height_orig;
    $ratio_new = $new_width/$new_height;

    if ($ratio_new < $ratio_orig) {
       $copy_width = $height_orig*$ratio_new;
       $copy_height = $height_orig;
    } else {
       $copy_width = $width_orig;
       $copy_height = $width_orig/$ratio_new;
    }

    //point to start copying from (to copy centre of image if we are cropping)
    $dims["src_x"] = ($width_orig - $copy_width)/2;
    $dims["src_y"] = ($height_orig - $copy_height)/2;
    $dims["copy_width"] = $copy_width;
    $dims["copy_height"] = $copy_height;

    return $dims;
}

You have to do it yourself.

I don't know if you want to crop or not, so here's how to calculate the values for both:

Scale image: resize to fit within new w x h keeping aspect ratio (so 1 side may be shorter than specified)

function calc_scale_dims($width_orig, $height_orig, $max_width, $max_height) { 

    $new_width=$width_orig;
    $new_height=$height_orig;
    $ratioh = $max_height/$new_height;
    $ratiow = $max_width/$new_width;
    $ratio = min($ratioh, $ratiow);
    // New dimensions
    $dims["w"] = intval($ratio*$new_width);
    $dims["h"] = intval($ratio*$new_height); 

    return $dims;
}

Resize and Crop: Resizes image and crops it to fit into the specified w x h if new aspect ratio is different (e.g. if aspect ratios are different, image will be resized to match specified size on the short size and the longer size if cropped in the middle)

function calc_crop_resize_dims($width_orig, $height_orig, $new_width, $new_height) { 

    //Calculate scaling
    $ratio_orig = $width_orig/$height_orig;
    $ratio_new = $new_width/$new_height;

    if ($ratio_new < $ratio_orig) {
       $copy_width = $height_orig*$ratio_new;
       $copy_height = $height_orig;
    } else {
       $copy_width = $width_orig;
       $copy_height = $width_orig/$ratio_new;
    }

    //point to start copying from (to copy centre of image if we are cropping)
    $dims["src_x"] = ($width_orig - $copy_width)/2;
    $dims["src_y"] = ($height_orig - $copy_height)/2;
    $dims["copy_width"] = $copy_width;
    $dims["copy_height"] = $copy_height;

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