If you’re looking to create a jpg file from raw image data, like from an email attachment, don’t use imagecreatefromstring(); because it strips almost all the header data including a ton of exif info off the file. Simply do this
$filename = 'pic.jpg';
$r = fopen($filename,'x');
fwrite($r,$raw_data); //$raw_data is the data string of the image
Voila you got yourself an image file with all the meta info including the much needed orientation exif value!