How to restrict image width or height on upload

后端 未结 3 1064
一生所求
一生所求 2021-01-16 05:03

I would like manipulate/resize images in a similar way to Pinterest but I am not sure what is the best way to approach it. The goal is to allow a mix of both portrait and la

相关标签:
3条回答
  • 2021-01-16 05:44

    You just need to understand which of the two edges of the image is longer, and compute the other dimension proportionally. If the maximum long-egde is 1024, then if one of the two edges is larger you will set that to 1024, and compute the other to fit the proportions. Then you will pass those two values to your image management functions.

    Like here: http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/

    Or here: http://www.9lessons.info/2009/03/upload-and-resize-image-with-php.html

    0 讨论(0)
  • 2021-01-16 05:58

    Check for a max size and then resize based on a ratio. Here's a pseudo code example:

    if($imageHeight > $maxHeight) {
        $newHeight = $maxHeight;
        $newWidth = $imageWidth * ($maxHeight / $imageHeight);
    }
    if($imageWidth > $maxWidth) {
        $newWidth = $maxWidth;
        $newHeight = $imageHeight * ($maxWidth / $imageWidth);
    }
    resize($image, $newWidth, $newHeight);
    

    It first checks the height and if the height is greater, it scales it down. Then it checks the width. If the width is too big, it scales it down again. The end result, both height and width will be with in your bounds. It uses the ratio to do the scaling.

    Note, this is pseudocodish. The actual resize function call will depend on your image manipulation library -- same goes for calls to obtain image size.

    0 讨论(0)
  • 2021-01-16 05:59

    try with this

           $needheight = 1000;
           $needwidth = 1000;
    
           $arrtest = getimagesize($upload_image_physical_path);
    
            $actualwidth = $arrtest[0];
            $actualheight = $arrtest[1];
    
            if($needwidth > $actualwidth || $needheight > $actualheight){
                 //uplaod code  
            }
    

    cheers

    0 讨论(0)
提交回复
热议问题