$thumbAspect) { // Source is wider $newHeight = $targetHeight; $newWidth = (int)($targetHeight * $sourceAspect); } else { // Source is taller or equal $newWidth = $targetWidth; $newHeight = (int)($targetWidth / $sourceAspect); } // Center the image $xOffset = (int)(($newWidth - $targetWidth) / 2); $yOffset = (int)(($newHeight - $targetHeight) / 2); imagecopyresampled($thumb, $sourceImage, -$xOffset, -$yOffset, 0, 0, $newWidth, $newHeight, $width, $height); // Save as jpg imagejpeg($thumb, $dest, 85); imagedestroy($sourceImage); imagedestroy($thumb); return true; }