如何调整图片大小以保持约束 [英] How to resize image keeping constraints php

查看:114
本文介绍了如何调整图片大小以保持约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经制作了两个GIF来解释我想要做的事情。灰色边框是我之后的尺寸(700 * 525)。他们在这个问题的底部。



我想要所有大于给定宽度和高度的图像缩小到边界(从中心)和然后裁剪边缘。这里有一些我已经试过的代码:

  if($ heightofimage => 700&& $ widthofimage => 525){
if($ heightofimage> $ widthofimage){

$ widthofimage = 525;
$ heightofimage = //缩放高度。 ($ heightofimage< $ widthofimage){

//作物高度为700.

}

$ widthofimage = //缩放的宽度。
$ heightofimage = 700;

//裁剪宽度为525.

}
} else {
echoimage too small;
}

以下是一些可视化解释我试图实现的GIF: p>

GIF 1:这里 x 方向的图片比例太多





GIF 2:这里,图片比例在 y 方向上过多






@timclutton的图像质量比较



所以我用 PHP(点击这里用php做你自己的测试),然后将它与原始照片进行比较,你可以看到有一个大不同!:



您的 PHP 方法:



http ://tragicclothing.co.uk/video%20images/test/yo.jpg

实际档案:







解决方案

你要。我没有广泛地测试它,但它似乎适用于我制作的少量测试图像。对于我的数学错误的地方存在着一丝怀疑,但它迟到了,我看不到任何明显的东西。



编辑:它已经够了,我又经历了一次,发现了这个bug,那就是作物不在图像的中间。代码替换为工作版本。

简而言之:将此作为起点,而不是生产就绪代码!

 <?php 

//设置图片尺寸限制。
$ target_w = 525;
$ target_h = 700;

//获取图片。
$ in = imagecreatefrompng('<您的路径> .png');

//获取图片尺寸。
$ w = imagesx($ in);
$ h = imagesy($ in);

if($ w> = $ target_w&& $ h> = $ target_h){
//获取比例。
$ x_scale =($ w / $ target_w);
$ y_scale =($ h / $ target_h);

//创建新图片。
$ out = imagecreatetruecolor($ target_w,$ target_h);

$ new_w = $ target_w;
$ new_h = $ target_h;
$ src_x = 0;
$ src_y = 0;

//比较尺度以确保我们裁剪的较小者:顶部/底部或
//左/右。
if($ x_scale> $ y_scale){
$ new_w = $ w / $ y_scale;

//参见下面$ src_y的描述。
$ src_x =(($ new_w - $ target_w)/ 2)* $ y_scale;
} else {
$ new_h = $ h / $ x_scale;

//有点棘手。裁剪是通过指定要从
//源图像中复制的坐标来完成的。所以要计算从新图像中删除多少和
//然后将其缩放到原始大小。结果是出来〜1px,但足够好。
$ src_y =(($ new_h - $ target_h)/ 2)* $ x_scale;
}

//给定正确的输入,这需要处理裁切和调整大小,并给
//返回新图像。请注意imagecopyresized()可能更快,但是
// imagecopyresampled()会提供更好的质量。
imagecopyresampled($ out,$ in,0,0,$ src_x,$ src_y,$ new_w,$ new_h,$ w,$ h);

//输出到浏览器。
header('Content-Type:image / png');
imagepng($ out);
出口;

} else {
echo'image too small';
}

?>


I have made two GIFs to explain what I am trying to do. Where the grey border is the dimensions I am after (700*525). They are at the bottom of this question.

I want for all images that are larger than the given width and height to scale down to the border (from the centre) and then crop off the edges. Here is some code I have put together to attempt this:

if ($heightofimage => 700 && $widthofimage => 525){
    if ($heightofimage > $widthofimage){

        $widthofimage = 525;
        $heightofimage = //scaled height.

        //crop height to 700.

    }

    if ($heightofimage < $widthofimage){

        $widthofimage = //scaled width.
        $heightofimage = 700;

        //crop width to 525.

    }
}else{
    echo "image too small";
}

Here are some GIFs that visually explain what I am trying to achieve:

GIF 1: Here the image proportions are too much in the x direction

GIF 2: Here the image proportions are too much in the y direction


image quality comparison for @timclutton

so I have used your method with PHP (click here to do your own test with the php) and then compared it to the original photo as you can see there is a big difference!:

Your PHP method:

http://tragicclothing.co.uk/video%20images/test/yo.jpg

The actual file:


解决方案

The below code should do what you want. I've not tested it extensively but it seems to work on the few test images I made. There's a niggling doubt at the back of mind that somewhere my math is wrong, but it's late and I can't see anything obvious.

Edit: It niggled enough I went through again and found the bug, which was that the crop wasn't in the middle of the image. Code replaced with working version.

In short: treat this as a starting point, not production-ready code!

<?php

// set image size constraints.
$target_w = 525;
$target_h = 700;

// get image.
$in = imagecreatefrompng('<path to your>.png');

// get image dimensions.
$w = imagesx($in);
$h = imagesy($in);

if ($w >= $target_w && $h >= $target_h) {
    // get scales.
    $x_scale = ($w / $target_w);
    $y_scale = ($h / $target_h);

    // create new image.
    $out = imagecreatetruecolor($target_w, $target_h);

    $new_w = $target_w;
    $new_h = $target_h;
    $src_x = 0;
    $src_y = 0;

    // compare scales to ensure we crop whichever is smaller: top/bottom or
    // left/right.
    if ($x_scale > $y_scale) {
        $new_w = $w / $y_scale;

        // see description of $src_y, below.
        $src_x = (($new_w - $target_w) / 2) * $y_scale;
    } else {
        $new_h = $h / $x_scale;

        // a bit tricky. crop is done by specifying coordinates to copy from in
        // source image. so calculate how much to remove from new image and
        // then scale that up to original. result is out by ~1px but good enough.
        $src_y = (($new_h - $target_h) / 2) * $x_scale;
    }

    // given the right inputs, this takes care of crop and resize and gives
    // back the new image. note that imagecopyresized() is possibly quicker, but
    // imagecopyresampled() gives better quality.
    imagecopyresampled($out, $in, 0, 0, $src_x, $src_y, $new_w, $new_h, $w, $h);

    // output to browser.
    header('Content-Type: image/png');
    imagepng($out);
    exit;

} else {
    echo 'image too small';
}

?>

这篇关于如何调整图片大小以保持约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆