//first make a copy of the image you want to work on
//$file is the name of the image you have uploaded and want to resize; $img is the name of the new image
$img = imagecreatefromjpeg($file);
//then make some variables to tell php what size you want
$newwidth = 400;
$newheight =267 ;
//get the images height and width so that we can scale it properly
height = imagesy($img);
$width = imagesx($img);
//work out scale rate
$scale = min($newheight/$height, $newwidth/$width);
//if scale is less than 1 then the image needs to be reduced otherwise just move it. $destination is the filepath to where you want to store it on your server
if($scale>1){
copy($file, $destination);
}else{
//calculate new height and width using scale so that ratio is maintained
$scaledheight = floor($scale*$height);
$scaledwidth = floor($scale*$width);
#Create a new temporary image of the new size
$tmpimg = imagecreatetruecolor($scaledwidth, $scaledheight);
#Copy and resize old image into new image
imagecopyresampled($tmpimg, $img, 0, 0, 0, 0, $scaledwidth, $scaledheight, $width, $height);
//free up some resources
imagedestroy($img);
//save jpeg. 95 refers to the compression level of the jpg but for some reason anything over 95 scrambles your image
imagejpeg($tmpimg,$destination,95);
}