简体   繁体   English

根据javascript中的最小宽度/高度和最大宽度/高度调整图像大小

[英]resize image respecting a min width / height and max width / height in javascript

I have this function that resizes an image (after that I have some other stuff to rotate and crop to a square. The problem here is I want the user be able to post images that are at least 640x640. What happens with this function is that if a user posts a 1080X1920 image (iphone picture), it will resize it to 562 X 1000 and then (with not shown code) it will be cropped to 562 X 562 ... The server will so refuse the image because it's smaller then 640X640. Can anyone help me improve this function so that it will return max width and height 1000 X 1000 but also respect a min width and height 640 X 640 ? By experimenting I found that if I set the Max Width and Height to 1138 the same 1080X1920 px image will be cropped to 640X640 but that is not acceptable solution. Any clue? 我具有此功能来调整图像大小(此后,我还有其他东西可以旋转并裁剪为正方形。这里的问题是我希望用户能够发布至少640x640的图像。此功能会导致如果用户发布1080X1920图像(iPhone图片),它将调整为562 X 1000,然后(未显示代码)将被裁剪为562 X 562 ...服务器将拒绝该图像,因为它较小640X640。谁能帮助我改进此功能,以便它返回最大宽度和高度1000 X 1000,但同时也尊重最小宽度和高度640 X 640吗?通过实验,我发现如果将最大宽度和高度设置为1138, 1080X1920 px图像将被裁剪为640X640,但这不是可接受的解决方案。

img.onload = function () {
            var width = img.width,
                height = img.height,
                canvas = document.createElement('canvas'),
                ctx = canvas.getContext("2d"),
                start_Y,
                start_X;

            console.log( width, "  ", height);

            var MAX_WIDTH = 1000;
            var MAX_HEIGHT = 1000;

            // set proper canvas dimensions before transform & export
            if ([5, 6, 7, 8].indexOf(srcOrientation) > -1) {
                if (width > height) {
                    if (width > MAX_WIDTH) {
                        height *= MAX_WIDTH / width;
                        width = MAX_WIDTH;
                    }
                } else {
                    if (height > MAX_HEIGHT) {
                        width *= MAX_HEIGHT / height;
                        height = MAX_HEIGHT;
                    }
                }
                canvas.width = height;
                canvas.height = width;

                console.log(canvas.width, " ", canvas.height);

            } else {

                if (height > MAX_HEIGHT) {
                    width *= MAX_HEIGHT / height;
                    height = MAX_HEIGHT;
                }
                canvas.width = width;
                canvas.height = height;

                console.log(canvas.width, " ", canvas.height);
                }
}

maybe I found a way, I modified the function like this: 也许我找到了一种方法,我修改了这样的功能:

        img.onload = function () {
            var width = img.width,
                height = img.height,
                canvas = document.createElement('canvas'),
                ctx = canvas.getContext("2d"),
                start_Y,
                start_X;


            var MAX_WIDTH = 1000;
            var MAX_HEIGHT = 1000;

            var MIN_WIDTH = 640;
            var MIN_HEIGHT = 640;

            var ratio;

            // set proper canvas dimensions before transform & export
            if ([5, 6, 7, 8].indexOf(srcOrientation) > -1) {
                if (width > height) {

                    console.log('w > h');

                    if (width > MAX_WIDTH) {
                        height *= MAX_WIDTH / width;

                        if(height < MIN_HEIGHT){

                            ratio = MIN_HEIGHT / height;

                            height = MIN_HEIGHT;

                            width = MAX_WIDTH * ratio;


                        }else{

                            width = MAX_WIDTH;

                        }


                    }
                } else {

                    console.log('h > w');

                    if (height > MAX_HEIGHT) {
                        width *= MAX_HEIGHT / height;

                        if(width < MIN_WIDTH){

                            ratio = MIN_WIDTH / width;

                            width = MIN_WIDTH;

                            height = MIN_HEIGHT * ratio;

                        }else{

                            height = MAX_HEIGHT;
                        }


                    }
                }
                canvas.width = height;
                canvas.height = width;

                console.log(canvas.width, " ", canvas.height);

            } else {

                console.log('other Orientations');

                if (height > MAX_HEIGHT) {
                    width *= MAX_HEIGHT / height;

                    if(width < MIN_WIDTH){

                        ratio = MIN_WIDTH / width;

                        width = MIN_WIDTH;

                        height = MAX_HEIGHT * ratio;


                    }else{

                        height = MAX_HEIGHT;

                    }


                }
                canvas.width = width;
                canvas.height = height;

                console.log(canvas.width, " ", canvas.height);
            }
}

You have to calculate a ratio like this when width is bigger than height 当宽度大于高度时,您必须计算出这样的比率

var ratio = 640 / height;

Or 要么

var ratio = 640 / width;

Having this ratio you can calculate the other value 有了这个比率,您可以计算其他值

Height = ratio × height;
Width = ratio × width;

After this you have a redized image / element. 之后,您将拥有一个红色的图像/元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM