Algorithm to resize image and maintain aspect ratio to fit iPhone

后端 未结 5 840
傲寒
傲寒 2020-12-24 04:55

I\'m creating a web service for an iPhone app to interact with.

When my client uploads images server-side, I want my php script to resize the image, whilst m

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-24 05:46

    Maybe a slightly shorter routine would be:

    // Calculate resize ratios for resizing 
    float ratioW = targetWidth / oldWidth; 
    float ratioH = targetHeight / oldHeight;
    
    // smaller ratio will ensure that the image fits in the view
    float ratio = ratioW < ratioH?ratioW:ratioH;
    
    newWidth = oldWidth*ratio;
    newHeight = oldHeight*ratio;
    

    Obviously if the ratio is > 1, then it's enlarging, if < 1 then it's shrinking.

提交回复
热议问题