How to restrict image width or height on upload

人盡茶涼 提交于 2019-12-01 14:09:13

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

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!