Okay let's say the following has happened successfully:
The user has uploaded an image file to the server.
You have made sure it's an image file and coppied it to it's final resting place.
Then you can do the following:
<?
//get the attributes of the uploaded image
$image_attribs = @getimagesize($im_file_name);
//copy the used attributes into more readable variable names
$width = $image_attribs[0];
$height = $image_attribs[1];
$type = $image_attribs[2];
?>
The previous lines of code read the height and width, in pixels, from the image the person uploaded. I do not know them before hand, I get them from the image file itself.
<?
//calcdulate the ratio to decrease the image size by maintining orientation
$ratio = ($width > $height) ? THUMB_MAX_WIDTH/$width : THUMB_MAX_WIDTH/$height;
//calculate the dimensions of the thumbnail
$th_width = $width * $ratio;
$th_height = $height * $ratio;
?>
The first line of code figures out the orientation of the image. It uses the turnary operator which may be confusing you. What it says is
<?
if ($width > $height) {
$ratio = THUMB_MAX_WIDTH/$width;
} else {
$ratio = THUMB_MAX_WIDTH/$height;
}
?>
This calculates the ratio (between zero and one) to decrease the image by. The larger size (width or height) will calculate to THUMB_MAX_WIDTH and the smaller side will change accordingly to maintain the aspect ratio. In your case THUMB_MAX_WIDTH will be 300.
The next two lines of code simply calculate the nex pixalage of the image. Hence these ($th_width and $th_height) are the dstx and dsty values for the image copy resized code that you posted.