网站设计中的图像

I allow my user to post images of different resolutions. I then use php to fit the to desired size and to reduce the size taken for these images. The image is 400x360px. But the problem is with the images after resizing. The images shrink like:

enter image description here

If the ratio of the image is same. Then the image is not affected. Is there any strategy or a php function to the image perfectly. I know image with different size can be resized only in this way. I want to know if there is any way for displaying the images?

You can calculate

$factor = $imgheight / $imgwidth;

and, if you want the images allways be, eg 300px width, just take

$widthnew = 300;
$heightnew = $widthnew * $factor;

So you will get a image displayed properly all the time.

as a simple function:

function imageheight($widthnew, $imgheight, $imgwidth) {
   $factor = $imgheight / $imgwidth;
   $heightnew = $widthnew * $factor;
   return $heightnew;
}

What you want is to crop it.

Try resizing the image until the height gets to the height you want it to be. Then, totally ignore the width and instead cut out parts of the width.

For example, shrink

##############################
#                            #
#                            #
#                            #
#                            #
#                            #
#                            #
#                            #
#                            #
##############################

to

###############
#             #
#             #
#             #
###############

then crop:

### ------- ###
#  |  , ,  |  #
#  |     _ |  #
#  |  __/  |  #
### ------- ###