Skip to content


How to reorient a picture with EXIF data using PHP GD

Sorry for the briefness, lack of time strikes again.
In this code snippet, $tmpFile is the path to the image file, and for simplicity’s sake here it’s always a JPG.
We define our own function “custImageFlip” to flip the picture (and I put it at the end because it’s secondary, but if you want to use the code as-is you’ll need to put it at the top), but if your target is PHP>=5.5, you can just use PHP’s native imageflip.

Code for the switch is hugely based on code by someone in the comments in PHP’s documentation.
Code for the PHP<5.5 implementation to flip the image is from another comment in PHP’s doc.

Last but not least, a great image pack that will allow you to test all those picture rotations and flips: https://github.com/recurser/exif-orientation-examples

$tmpImage = @imagecreatefromjpeg($tmpFile);
$exif = exif_read_data($tmpFile);
//die(json_encode($exif));
if($exif!==false) {
	$ort=1;
	if(isset($exif['Orientation'])) {
		// orientation is usually here
		$ort=$exif['Orientation'];
	} elseif(isset($exif['IFD0']) && isset($exif['IFD0']['Orientation'])) {
		// but apparently it can be here sometimes?
		$ort=$exif['IFD0']['Orientation'];
	}
	switch($ort) {
		case 1: // nothing
			break;
		case 2: // horizontal flip
			$tmpImage=custImageFlip($tmpImage,2);
			break;
		case 3: // 180 rotate left
			$tmpImage=imagerotate($tmpImage,180,0);
			break;
		case 4: // vertical flip
			$tmpImage=custImageFlip($tmpImage,1);
			break;
		case 5: // vertical flip + 90 rotate right
			$tmpImage=custImageFlip($tmpImage,1);
			$tmpImage=imagerotate($tmpImage,-90,0);
			break;
		case 6: // 90 rotate right
			$tmpImage=imagerotate($tmpImage,-90,0);
			break;
		case 7: // horizontal flip + 90 rotate right
			$tmpImage=custImageFlip($tmpImage,2);
			$tmpImage=imagerotate($tmpImage,-90,0);
			break;
		case 8: // 90 rotate left
			$tmpImage=imagerotate($tmpImage,90,0);
			break;
	}
}

// ImageFlip from https://php.net/manual/en/function.imagecopy.php#89658
function custImageFlip($imgsrc,$mode) {
	$width=imagesx($imgsrc);
	$height=imagesy($imgsrc);

	$src_x=0;
	$src_y=0;
	$src_width=$width;
	$src_height=$height;

	switch ($mode) {
		case '1': //vertical
			$src_y=$height-1;
			$src_height=-$height;
			break;
		case '2': //horizontal
			$src_x=$width-1;
			$src_width=-$width;
			break;
		case '3': //both
			$src_x=$width-1;
			$src_y=$height-1;
			$src_width=-$width;
			$src_height=-$height;
			break;
		default:
			return $imgsrc;
	}
	$imgdest = imagecreatetruecolor ($width,$height);
	if(imagecopyresampled($imgdest, $imgsrc, 0, 0, $src_x, $src_y , $width, $height, $src_width, $src_height)){
		return $imgdest;
	}
	return $imgsrc;
}

Posted in PHP, programming, web development.


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.

Sorry about the CAPTCHA that requires JS. If you really don't want to enable JS and still want to comment, you can send me your comment via e-mail and I'll post it for you.

Please solve the CAPTCHA below in order to fight spamWordPress CAPTCHA